[][src]Trait core_extensions::ResultLike

pub trait ResultLike: Sized {
    type Item;
    type Error;
    pub fn to_result_(self) -> Result<Self::Item, Self::Error>;
pub fn is_item(&self) -> bool; pub fn is_error(&self) -> bool { ... }
pub fn unwrap_(self) -> Self::Item
    where
        Self::Error: Debug
, { ... }
pub fn unwrap_err_(self) -> Self::Error
    where
        Self::Item: Debug
, { ... }
pub fn unwrap_or_abort(self) -> Self::Item
    where
        Self::Error: Debug
, { ... }
pub unsafe fn unwrap_unchecked(self) -> Self::Item { ... }
pub unsafe fn unwrap_err_unchecked(self) -> Self::Error { ... }
pub fn unwrap_safe(self) -> Self::Item
    where
        Self: ResultLike<Error = Void>
, { ... }
pub fn unwrap_err_safe(self) -> Self::Error
    where
        Self: ResultLike<Item = Void>
, { ... } }

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:

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

type Item[src]

The type of the item values

type Error[src]

The type of the error values

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);
Loading content...

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]

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]

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]

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]

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]

Unwraps the error knowing that the item is impossible.

Example

use core_extensions::ResultLike;

assert_eq!(Err(100).unwrap_err_safe(),100);
Loading content...

Implementations on Foreign Types

impl<T> ResultLike for Option<T>[src]

type Item = T

type Error = IsNoneError

impl<E, T> ResultLike for Result<T, E>[src]

type Item = T

type Error = E

Loading content...

Implementors

Loading content...