Add Option helper

This commit is contained in:
Matthias Beyer 2016-06-27 15:12:17 +02:00
parent 0d9d96fa6c
commit 320c1e4bd8

View file

@ -151,6 +151,37 @@ macro_rules! generate_result_helper {
}
}
#[macro_export]
macro_rules! generate_option_helper {
(
$name: ident,
$kindname: ident
) => {
/// Trait to replace
///
/// ```ignore
/// foo.ok_or(SomeType::SomeErrorKind.into_error())
/// ```
///
/// with
///
/// ```ignore
/// foo.ok_or_errkind(SomeType::SomeErrorKind)
/// ```
pub trait OkOrErr<T> {
fn ok_or_errkind(self, kind: $kindname) -> Result<T, $name>;
}
impl<T> OkOrErr<T> for Option<T> {
fn ok_or_errkind(self, kind: $kindname) -> Result<T, $name> {
self.ok_or(kind.into_error())
}
}
}
}
#[macro_export]
macro_rules! generate_error_types {
(
@ -165,6 +196,7 @@ macro_rules! generate_error_types {
$($kind => $string),*);
generate_result_helper!($name, $kindname);
generate_option_helper!($name, $kindname);
}
}