Trait core_extensions::option_result_ext::ResultLike
source · [−]pub trait ResultLike: Sized {
type Item;
type Error;
fn into_result_(self) -> Result<Self::Item, Self::Error>;
fn is_item(&self) -> bool;
fn from_item(res: Self::Item) -> Self;
fn from_error(res: Self::Error) -> Self;
fn is_error(&self) -> bool { ... }
fn from_result_(res: Result<Self::Item, Self::Error>) -> Self { ... }
}
option_result
only.Expand description
Trait for types with error and item values.
Types that implement this don’t have to have item and error variants, so long as they have distinct values that represent items and errors.
For more provided methods, you can import the ResultLikeExt
trait.
For Implementors.
There are some things that implementors of this trait must ensure:
-
ResultLike::into_result_
must not panic. -
If
ResultLike::is_item
returnstrue
, thenResultLike::into_result_
must returnOk
. -
If
ResultLike::is_error
returnstrue
, thenResultLike::into_result_
must returnErr
. -
ResultLike::is_error
must not equalResultLike::is_item
for the same value. -
If
ResultLike::from_result_
is overriden, it must return a value equivalent to the default implementation.
Examples
Implementing ResultLike
use core_extensions::{ResultLike, ResultLikeExt};
#[derive(Debug,Clone,Copy,Eq,PartialEq)]
pub struct ShouldBeEven(pub u64);
#[derive(Debug,Clone,Eq,PartialEq)]
pub struct WasOddError(pub u64);
#[derive(Debug,Clone,Eq,PartialEq)]
pub struct Even(pub u64);
impl ResultLike for ShouldBeEven{
type Item = Even;
type Error = WasOddError;
fn is_item (&self) -> bool {
self.into_result_().is_item()
}
fn into_result_(self) -> Result<Self::Item, Self::Error> {
if self.0 % 2 == 0 { Ok(Even(self.0)) } else { Err(WasOddError(self.0)) }
}
fn from_item(x: Even) -> Self {
ShouldBeEven(x.0)
}
fn from_error(x: WasOddError) -> Self {
ShouldBeEven(x.0)
}
}
assert_eq!(ShouldBeEven(0).unwrap_() , Even(0));
assert_eq!(ShouldBeEven(1).unwrap_err_(), WasOddError(1));
assert_eq!(ShouldBeEven(2).unwrap_() , Even(2));
assert_eq!(ShouldBeEven(3).unwrap_err_(), WasOddError(3));
assert_eq!(ShouldBeEven(4).unwrap_() , Even(4));
assert_eq!(ShouldBeEven(5).unwrap_err_(), WasOddError(5));
assert_eq!(ShouldBeEven::from_result_(Ok(Even(10))), ShouldBeEven(10));
assert_eq!(ShouldBeEven::from_result_(Err(WasOddError(3))), ShouldBeEven(3));
and_then
function
use core_extensions::ResultLike;
use core_extensions::option_result_ext::IsNoneError;
fn and_then<R, P, F>(x: R, func: F) -> P
where
R: ResultLike,
P: ResultLike<Error = R::Error>,
F: FnOnce(R::Item) -> P
{
match x.into_result_() {
Ok(x) => func(x),
Err(e) => P::from_error(e),
}
}
assert_eq!(and_then(None, |x: u32| x.checked_sub(10)), None);
assert_eq!(and_then(Some(10), |x: u32| x.checked_sub(10)), Some(0));
assert_eq!(and_then(Some(10), |x: u32| x.checked_sub(11)), None);
assert_eq!(and_then(Ok("100"), |x| x.parse::<i32>() ), Ok(100));
assert_eq!(and_then(Err(()), |x: &str| x.parse::<i32>().map_err(drop) ), Err(()));
// Converting a Result to an Option
assert_eq!(and_then(Ok(10), Some), Some(10));
assert_eq!(and_then(Err(IsNoneError::new()), Some), None::<&str>);
Required Associated Types
Required Methods
sourcefn into_result_(self) -> Result<Self::Item, Self::Error>
fn into_result_(self) -> Result<Self::Item, Self::Error>
Converts self
to a Result.
Panic
Implementors of this method must ensure that it does not panic.
Example
use core_extensions::ResultLike;
use core_extensions::option_result_ext::IsNoneError;
assert_eq!(Some(0) .into_result_(), Ok(0));
assert_eq!(None::<()>.into_result_(), Err(IsNoneError::new()));
assert_eq!(Ok::<i32, ()>(0).into_result_(), Ok(0));
assert_eq!(Err::<(),i32>(3).into_result_(), Err(3));
Queries whether self
is an item value.
Example
use core_extensions::ResultLike;
assert_eq!(Ok ::<i32, ()>(10).is_item(), true);
assert_eq!(Err::<i32, ()>(()).is_item(), false);
assert_eq!(Some(10) .is_item(), true);
assert_eq!(None::<()>.is_item(), false);
sourcefn from_error(res: Self::Error) -> Self
fn from_error(res: Self::Error) -> Self
Provided Methods
Queries whether self
is an error value.
Example
use core_extensions::ResultLike;
assert_eq!(Ok ::<i32, ()>(10).is_error(), false);
assert_eq!(Err::<i32, ()>(()).is_error(), true);
assert_eq!(Some(10) .is_error(), false);
assert_eq!(None::<()>.is_error(), true);
sourcefn from_result_(res: Result<Self::Item, Self::Error>) -> Self
fn from_result_(res: Result<Self::Item, Self::Error>) -> Self
Constructs Self
from a Result
Example
use core_extensions::ResultLike;
use core_extensions::option_result_ext::IsNoneError;
assert_eq!(Option::from_result_(Ok(0)), Some(0));
assert_eq!(Option::<()>::from_result_(Err(IsNoneError::new())), None);
assert_eq!(Result::<i32, ()>::from_result_(Ok(0)), Ok(0));
assert_eq!(Result::<(), i32>::from_result_(Err(3)), Err(3));