Skip to content

Instantly share code, notes, and snippets.

@whipsch
Last active August 29, 2015 14:23
Show Gist options
  • Save whipsch/91816144fa39084243e6 to your computer and use it in GitHub Desktop.
Save whipsch/91816144fa39084243e6 to your computer and use it in GitHub Desktop.
macro_rules! wrapped_enum {
($(#[$attr:meta])* pub enum $enum_name:ident, $($enum_variant_name:ident($ty:ty),)+) => (
$(#[$attr])*
pub enum $enum_name { $($enum_variant_name($ty)),+ }
$(impl From<$ty> for $enum_name {
fn from (ty: $ty) -> Self {
$enum_name::$enum_variant_name(ty)
}
})+
);
($(#[$attr:meta])* enum $enum_name:ident, $($enum_variant_name:ident($ty:ty),)+) => (
$(#[$attr])*
enum $enum_name { $($enum_variant_name($ty)),+ }
$(impl From<$ty> for $enum_name {
fn from (ty: $ty) -> Self {
$enum_name::$enum_variant_name(ty)
}
})+
);
}
pub type Result<T> = std::result::Result<T, Error>;
wrapped_enum!{#[derive(Debug)] pub enum Error,
Io(io::Error),
}
// fn f() -> Result<T> {
// let a = try!(...); // a will be defined, else early returning Err(Error::Io(io::Error))
// }
@WildCryptoFox
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment