https://github.com/figsoda/one-of
Rust library that introduces macros to represent and use types that can be converted either From or TryInto the given types
https://github.com/figsoda/one-of
data-structure data-structures enum macro macros rust type
Last synced: 5 months ago
JSON representation
Rust library that introduces macros to represent and use types that can be converted either From or TryInto the given types
- Host: GitHub
- URL: https://github.com/figsoda/one-of
- Owner: figsoda
- License: mpl-2.0
- Created: 2020-11-21T23:26:35.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-09-05T00:34:59.000Z (over 2 years ago)
- Last Synced: 2025-09-20T12:44:38.309Z (9 months ago)
- Topics: data-structure, data-structures, enum, macro, macros, rust, type
- Language: Rust
- Homepage: https://docs.rs/one-of
- Size: 31.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# one-of
[](https://crates.io/crates/one-of)
[](https://www.mozilla.org/en-US/MPL/2.0)
[](https://github.com/figsoda/one-of/actions?query=workflow:ci)
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
[Documentation](https://docs.rs/one-of)
## Usage
```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::::None, x.into());
// some type of integer
let x: one_of!(i8, i16, i32, i64, u8, u16, u32, u64) = 42.into();
assert_eq!(Option::::None, x.into());
assert_eq!(Option::::None, x.into());
assert_eq!(Some(42i32), x.into());
assert_eq!(Option::::None, x.into());
assert_eq!(Option::::None, x.into());
assert_eq!(Option::::None, x.into());
assert_eq!(Option::::None, x.into());
assert_eq!(Option::::None, x.into());
// case macro is the `match` keyword for `one_of` types
case!(::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");
};
);
```
## Changelog
See [CHANGELOG.md](CHANGELOG.md)