Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 26 days 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 (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-09-05T00:34:59.000Z (over 1 year ago)
- Last Synced: 2024-11-27T20:39:17.179Z (about 2 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
[![version](https://img.shields.io/crates/v/one-of?logo=rust&style=flat-square)](https://crates.io/crates/one-of)
[![license](https://img.shields.io/badge/license-MPL--2.0-blue?style=flat-square)](https://www.mozilla.org/en-US/MPL/2.0)
[![ci](https://img.shields.io/github/actions/workflow/status/figsoda/one-of/ci.yml?label=ci&logo=github-actions&style=flat-square)](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)