Last active
August 29, 2015 14:23
-
-
Save whipsch/91816144fa39084243e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://crates.io/crates/wrapped_enum