Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/therustmonk/match_cast
Rust match_cast macro implementation
https://github.com/therustmonk/match_cast
Last synced: 2 days ago
JSON representation
Rust match_cast macro implementation
- Host: GitHub
- URL: https://github.com/therustmonk/match_cast
- Owner: therustmonk
- Created: 2016-09-23T13:10:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-09-23T13:37:12.000Z (over 8 years ago)
- Last Synced: 2025-01-02T21:12:39.573Z (6 days ago)
- Language: Rust
- Size: 2.93 KB
- Stars: 24
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# match_cast
This is a minimal crate which implements match through different types.
Usage:
```rust
#[macro_use]
extern crate match_cast;
use std::panic;fn main() {
let res = panic::catch_unwind(|| {
panic!("Oh no!");
});let any = res.unwrap_err();
let result = match_cast!( any {
val as Option => {
format!("Option = {:?}", val)
},
val as String => {
format!("String = {:?}", val)
},
val as &'static str => {
format!("&'static str = {:?}", val)
},
});assert_eq!(result.unwrap(), "&'static str = \"Oh no!\"");
}
```To use pattern there is `match_down` macro:
```rust
#[macro_use]
extern crate match_cast;
use std::any::Any;struct Bar {
x: u8,
}struct Foo {
x: u8,
}fn main() {
let any: Box = Box::new(Foo { x: 45 });
let result = match_down!( any {
Bar { x } => { x },
Foo { x } => { x },
});assert_eq!(result.unwrap(), 45);
}
```