pub trait ResultLikeExt: ResultLike {
    fn unwrap_or_else_<F>(self, func: F) -> Self::Item
    where
        F: FnOnce(Self::Error) -> Self::Item
, { ... } fn unwrap_err_or_else_<F>(self, func: F) -> Self::Error
    where
        F: FnOnce(Self::Item) -> Self::Error
, { ... } fn unwrap_or_(self, default: Self::Item) -> Self::Item { ... } fn unwrap_err_or_(self, default: Self::Error) -> Self::Error { ... } fn unwrap_(self) -> Self::Item
    where
        Self::Error: Debug
, { ... } fn unwrap_err_(self) -> Self::Error
    where
        Self::Item: Debug
, { ... } fn unwrap_or_abort(self) -> Self::Item
    where
        Self::Error: Debug
, { ... } unsafe fn unwrap_unchecked_(self) -> Self::Item { ... } unsafe fn unwrap_err_unchecked_(self) -> Self::Error { ... } fn into_item(self) -> Self::Item
    where
        Self: ResultLike,
        Infallible: From<Self::Error>
, { ... } fn into_error(self) -> Self::Error
    where
        Self: ResultLike,
        Infallible: From<Self::Item>
, { ... } }
Available on crate feature option_result only.
Expand description

Extension trait for ResultLike implementors.

Provided Methods

Unwraps the item variant, otherwise calls func with the error

Example
use core_extensions::ResultLikeExt;
 
assert_eq!(Some(3).unwrap_or_else_(|_| unreachable!()), 3);
assert_eq!(None.unwrap_or_else_(|_| 5 ), 5);
 
assert_eq!(Ok::<u32, u32>(3).unwrap_or_else_(|_| unreachable!()), 3);
assert_eq!(Err::<u32, u32>(5).unwrap_or_else_(|e| e * 2), 10);
 

Unwraps the error variant, otherwise calls func with the item

Example
use core_extensions::ResultLikeExt;
use core_extensions::option_result_ext::IsNoneError;
 
assert_eq!(Some(3).unwrap_err_or_else_(|_| IsNoneError::new()), IsNoneError::new());
assert_eq!(None.unwrap_err_or_else_(|_: u32| unreachable!()), IsNoneError::new());
 
assert_eq!(Ok::<u32, u32>(3).unwrap_err_or_else_(|e| e *7), 21);
assert_eq!(Err::<u32, u32>(5).unwrap_err_or_else_(|e| unreachable!()), 5);
 

Unwraps the item variant, otherwise returns default.

Example
use core_extensions::ResultLikeExt;
 
assert_eq!(Some(3).unwrap_or_(5), 3);
assert_eq!(None.unwrap_or_(8), 8);
 
assert_eq!(Ok::<u32, ()>(13).unwrap_or_(21), 13);
assert_eq!(Err::<u32, ()>(()).unwrap_or_(55), 55);
 

Unwraps the error variant, otherwise returns default.

Example
use core_extensions::ResultLikeExt;
 
assert_eq!(Ok::<u32, u32>(13).unwrap_err_or_(21), 21);
assert_eq!(Err::<u32, u32>(34).unwrap_err_or_(55), 34);
 

Unwraps the item variant.

Panic

Panics if this is the error variant

Example
use core_extensions::ResultLikeExt;

assert_eq!(Some(13).unwrap_(), 13);

assert_eq!(Ok::<i32, ()>(0).unwrap_(), 0);
assert_eq!(Ok::<&str, ()>("hello").unwrap_(), "hello");
Example, panicking
use core_extensions::ResultLikeExt;

None::<()>.unwrap_();

Err::<(), i32>(0).unwrap_();
Err::<(), &str>("hello").unwrap_();

Unwraps the error variant.

Panic

Panics if this is the item variant

Example
use core_extensions::ResultLikeExt;
use core_extensions::option_result_ext::IsNoneError;

assert_eq!(None::<u32>.unwrap_err_(), IsNoneError::new());

assert_eq!(Err::<(), i32>(0).unwrap_err_(), 0);
assert_eq!(Err::<(), &str>("hello").unwrap_err_(), "hello");
Example, panicking
use core_extensions::ResultLikeExt;

Some(10).unwrap_err_();

Ok::<i32, ()>(0).unwrap_err_();
Ok::<&str, ()>("hello").unwrap_err_();

Unwraps the item variant, otherwise prints the error and aborts the process.

This method also aborts if ResultLike::into_result_ panics.

Example
use core_extensions::ResultLikeExt;

let string = "what \"is\" this";
let res: Result<&str, ()> = Ok(string);
assert_eq!(res.unwrap_or_abort(), string);

Unwraps the item variant of the type without checking whether this is the item variant.

Safety

You must ensure that it’s impossible for this to be the error variant.

Example
use core_extensions::ResultLikeExt;

unsafe{
    assert_eq!(Some(21).unwrap_unchecked_(), 21);
    assert_eq!(Ok::<_, ()>(100).unwrap_unchecked_(), 100);
}

Unwraps the error variant of the type without checking whether this is the error variant.

Safety

You must ensure that it’s impossible for this to be the item variant.

Example
use core_extensions::ResultLikeExt;
use core_extensions::option_result_ext::IsNoneError;

unsafe{
    assert_eq!(None::<u32>.unwrap_err_unchecked_(), IsNoneError::new());
    assert_eq!(Err::<(), _>(100).unwrap_err_unchecked_(), 100);
}

Unwraps the item knowing that the error variant can’t be constructed.

Example

With std::convert::Infallible as the error

use core_extensions::ResultLikeExt;

use std::convert::Infallible;

let res: Result<i32, Infallible> = Ok(100);
assert_eq!(res.into_item(), 100);

With core_extensions::Void as the error

use core_extensions::{ResultLikeExt, Void};

let res: Result<i32, Void> = Ok(100);
assert_eq!(res.into_item(), 100);

Unwraps the error knowing that the item variant can’t be constructed.

Example

With std::convert::Infallible as the item variant.

use core_extensions::ResultLikeExt;
use core_extensions::option_result_ext::IsNoneError;

use std::convert::Infallible;

assert_eq!(None::<Infallible>.into_error(), IsNoneError::new());

let res: Result<Infallible, i32> = Err(100);
assert_eq!(res.into_error(), 100);

With core_extensions::Void as the item variant.

use core_extensions::{ResultLikeExt, Void};
use core_extensions::option_result_ext::IsNoneError;

assert_eq!(None::<Void>.into_error(), IsNoneError::new());

let res: Result<Void, i32> = Err(100);
assert_eq!(res.into_error(), 100);

Implementors