Skip to content

Instantly share code, notes, and snippets.

@chrivers
Created September 29, 2016 15:59
Show Gist options
  • Save chrivers/b0812a5b6574307139d0fff3f20ea26c to your computer and use it in GitHub Desktop.
Save chrivers/b0812a5b6574307139d0fff3f20ea26c to your computer and use it in GitHub Desktop.
macro_rules! enum_to_primitive {
($name:ident) => {
impl ToPrimitive for $name {
fn to_i64(&self) -> Option<i64> {
return Some(*self as i64);
}
fn to_u64(&self) -> Option<u64> {
return Some(*self as u64);
}
}
}
}
macro_rules! enum_primitive {
(
$(#[$mt:meta])*
pub enum $name:ident {
$($field:tt)*
}
) =>
{
enum_from_primitive! {
$(#[$mt])*
#[derive(Debug,Clone,Copy)]
pub enum $name {
$($field)*
}
}
enum_to_primitive!($name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment