macro_rules! ident {
(
$(#[$meta:meta])*
pub <$ty_a:ty, $ty_b:ty>: [ $( $a:expr => $b:expr ),+ ]
) => { ... };
(
$(#[$meta:meta])*
<$ty_a:ty, $ty_b:ty>: [ $( $a:expr => $b:expr ),+ ]
) => { ... };
}
Expand description
Create identity functions for Eq types
generates two functions
ⓘ
fn ident_to(a: &A) -> Option<&'static B> { /* ... */ }
fn ident_from(b: &B) -> Option<&'static A> { /* ... */ }
§Examples
struct Scopes();
impl Scopes {
ident!(pub <usize, str>: [
00 => "read",
01 => "write",
02 => "execute"
]);
}
// Go from A to B
assert_eq!(
Scopes::ident_to(&0),
Some("read")
);
// Go from B to A
assert_eq!(
Scopes::ident_from("write"),
Some(&1)
);
// Do nothing for undefined values
assert_eq!(
Scopes::ident_to(&45),
None
);