diff options
Diffstat (limited to 'src/prelude.rs')
-rw-r--r-- | src/prelude.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/prelude.rs b/src/prelude.rs index 6789a1f..c647591 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -9,3 +9,40 @@ pub use std::os::unix::ffi::{OsStrExt as _, OsStringExt as _}; pub use std::os::unix::io::{AsRawFd as _, FromRawFd as _, IntoRawFd as _}; pub use std::os::unix::process::ExitStatusExt as _; pub use users::os::unix::UserExt as _; + +pub use ext::Result as _; + +mod ext { + pub trait Result { + type T; + type E; + + fn allow(self, allow_e: Self::E) -> Self; + fn allow_with(self, allow_e: Self::E, default_t: Self::T) -> Self; + } + + impl<T, E> Result for std::result::Result<T, E> + where + T: std::default::Default, + E: std::cmp::PartialEq, + { + type T = T; + type E = E; + + fn allow(self, allow_e: Self::E) -> Self { + self.or_else(|e| { + if e == allow_e { + Ok(std::default::Default::default()) + } else { + Err(e) + } + }) + } + + fn allow_with(self, allow_e: Self::E, default_t: Self::T) -> Self { + self.or_else( + |e| if e == allow_e { Ok(default_t) } else { Err(e) }, + ) + } + } +} |