[−][src]Trait core_extensions::ResultLike
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 values that represent item and error.
For Implementors.
There are some things that implementors of this trait must ensure:
- to_result_ can't panic ,
- that if ResultLike::is_item ==true then to_result_ returns Ok (Self::Item)).
- that if ResultLike::is_error==true then to_result_ returns Err(Self::Error).
- that ResultLike::is_error(&this) != ResultLike::is_item(&this)
Example
use core_extensions::ResultLike; #[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.to_result_().is_item() } fn to_result_(self)->Result<Self::Item,Self::Error>{ if self.0 % 2 ==0 { Ok(Even(self.0)) }else{ Err(WasOddError(self.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) );
Associated Types
Loading content...Required methods
pub fn to_result_(self) -> Result<Self::Item, Self::Error>
[src]
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) .to_result_(),Ok(0)); assert_eq!(None::<()>.to_result_(),Err(IsNoneError)); assert_eq!(Ok::<i32,()>(0).to_result_(),Ok(0)); assert_eq!(Err::<(),i32>(3).to_result_(),Err(3));
pub fn is_item(&self) -> bool
[src]
Queries whether self
is an item value.
Note that self.is_item() != self.is_error() must always be true.
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);
Provided methods
pub fn is_error(&self) -> bool
[src]
Queries whether self
is an error value.
Note that self.is_item() != self.is_error() must always be true.
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);
pub fn unwrap_(self) -> Self::Item where
Self::Error: Debug,
[src]
Self::Error: Debug,
Unwraps the item variant.
Panic
Panics if it's the error variant
Example
use core_extensions::ResultLike; assert_eq!(Ok::<i32 ,()>(0 ).unwrap_(),0 ); assert_eq!(Ok::<&str,()>("hello").unwrap_(),"hello");
Example,panicking
use core_extensions::ResultLike; Err::<(),i32 >(0 ).unwrap_(); Err::<(),&str>("hello").unwrap_();
pub fn unwrap_err_(self) -> Self::Error where
Self::Item: Debug,
[src]
Self::Item: Debug,
Unwraps the error variant.
Panic
Panics if it's the item variant
Example
use core_extensions::ResultLike; assert_eq!(Err::<(),i32 >(0 ).unwrap_err(),0 ); assert_eq!(Err::<(),&str>("hello").unwrap_err(),"hello");
Example,panicking
use core_extensions::ResultLike; Ok::<i32 ,()>(0 ).unwrap_err(); Ok::<&str,()>("hello").unwrap_err();
pub fn unwrap_or_abort(self) -> Self::Item where
Self::Error: Debug,
[src]
Self::Error: Debug,
Unwraps the item if it is the item value, otherwise it prints the Error and aborts the process.
Panic-safety
This method can only panic if ResultLike::to_result_
panics.
Example
use core_extensions::ResultLike; let string_="what \"is\" this"; assert_eq!(Ok::<&str,()>(string_).unwrap_or_abort(),string_);
pub unsafe fn unwrap_unchecked(self) -> Self::Item
[src]
Unwraps the item variant of the type without checking whether this is the current variant.
Safety
You must ensure that it's impossible for this to be the error variant.
Example
use core_extensions::ResultLike; unsafe{ assert_eq!(Ok::<_,()>(100).unwrap_unchecked(),100); }
pub unsafe fn unwrap_err_unchecked(self) -> Self::Error
[src]
Unwraps the error variant of the type without checking whether this is the current variant.
Safety
You must ensure that it's impossible for this to be the item variant.
Example
use core_extensions::ResultLike; unsafe{ assert_eq!(Err::<(),_>(100).unwrap_err_unchecked(),100); }
pub fn unwrap_safe(self) -> Self::Item where
Self: ResultLike<Error = Void>,
[src]
Self: ResultLike<Error = Void>,
Unwraps the item knowing that the error is impossible.
Example
use core_extensions::ResultLike; assert_eq!(Ok(100).unwrap_safe(),100);
pub fn unwrap_err_safe(self) -> Self::Error where
Self: ResultLike<Item = Void>,
[src]
Self: ResultLike<Item = Void>,
Unwraps the error knowing that the item is impossible.
Example
use core_extensions::ResultLike; assert_eq!(Err(100).unwrap_err_safe(),100);