Crates.io | one-of |
lib.rs | one-of |
version | 0.2.3 |
source | src |
created_at | 2020-11-22 03:36:50.041245 |
updated_at | 2021-03-04 03:44:31.572309 |
description | Macro to represent a type that can be converted either `From` or `TryInto` the given types |
homepage | https://github.com/figsoda/one-of |
repository | https://github.com/figsoda/one-of |
max_upload_size | |
id | 314904 |
size | 37,821 |
Macro to represent a type that can be converted either From
or TryInto
the given types
This crate only works on the nightly version of Rust
use one_of::{case, one_of};
// either `u32` or `char`
let x: one_of!(u32, char) = 42.into();
assert_eq!(Some(42u32), x.into());
assert_eq!(Option::<char>::None, x.into());
// some type of integer
let x: one_of!(i8, i16, i32, i64, u8, u16, u32, u64) = 42.into();
assert_eq!(Option::<i8>::None, x.into());
assert_eq!(Option::<i16>::None, x.into());
assert_eq!(Some(42i32), x.into());
assert_eq!(Option::<i64>::None, x.into());
assert_eq!(Option::<u8>::None, x.into());
assert_eq!(Option::<u16>::None, x.into());
assert_eq!(Option::<u32>::None, x.into());
assert_eq!(Option::<u64>::None, x.into());
// case macro is the `match` keyword for `one_of` types
case!(<one_of!(bool, &str, i64)>::from("Hello, world!"),
// bool
_ => {
panic!("not bool");
};
// &str
s if s.starts_with("Hello, ") => {
assert_eq!(&s[7 ..], "world!");
}
_ => {
panic!("not other strings");
};
// i64
_ => {
panic!("not i64");
};
);
See CHANGELOG.md