//! Utilities to make development easier. #![no_std] /// An extension trait for [`Result`]s. /// /// [`Result`]: core::result::Result pub trait ResultExt { /// Convert the result into another result type. fn map_auto(self) -> Result where O2: From, E2: From; } /// An extension trait for [`Option`]s. /// /// [`Option`]: core::option::Option pub trait OptionExt { /// Unwraps the option, or returns a default value (converted to the option's type). fn unwrap_or_auto(self, default: T2) -> T2 where T2: From; /// Maps the option to another option type. fn map_auto(self) -> Option where T2: From; /// Unwraps the option, or returns an error. fn ok_or_auto(self, err: E) -> Result where O: From; } impl ResultExt for Result { #[inline] fn map_auto(self) -> Result where O2: From, E2: From { match self { Err(e) => Err(E2::from(e)), Ok(o) => Ok(O2::from(o)), } } } impl OptionExt for Option { #[inline] fn unwrap_or_auto(self, default: T2) -> T2 where T2: From { match self { None => default, Some(t) => T2::from(t), } } #[inline] fn map_auto(self) -> Option where T2: From { match self { None => None, Some(t) => Some(T2::from(t)), } } #[inline] fn ok_or_auto(self, err: E) -> Result where O: From { match self { None => Err(err), Some(t) => Ok(O::from(t)), } } }