Expand description
Create your own enum type that behaves like Rust’s Option but with custom names.
§Example
use option_like::option_like;
option_like!(
#[derive(Debug, PartialEq)]
pub enum Cached<T> {
Miss,
Hit(T),
}
is_none => is_miss
is_some => is_hit
);
// Create instances
let c1 = Cached::<u32>::Hit(42);
let c2 = Cached::<u32>::Miss;
// Boolean tests
assert!(c1.is_hit());
assert!(c2.is_miss());
// Convert to Option
assert_eq!(Option::<u32>::from(c1), Some(42));
assert_eq!(Option::<u32>::from(c2), None);
// Convert from Option
assert_eq!(Cached::<u32>::from(Some(42)), Cached::Hit(42));
assert_eq!(Cached::<u32>::from(None), Cached::Miss);Macros§
- option_
like - Creates a new enum type that behaves like Rust’s
Option<T>but with custom names. - option_
like_ from_ into_ option - Creates the
From<Option<T>>andInto<Option<T>>conversions for an option-like enum. - option_
like_ impl - Creates the inherent implementation block for an option-like enum.