Trait ResultExt

Source
pub trait ResultExt {
    type T;
    type E;

    // Required methods
    fn expect_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
       where Self::E: Debug,
             M: AsRef<str>;
    fn expect_or_report(self, msg: &str) -> Self::T
       where Self::E: Error;
    fn expect_or_report_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
       where Self::E: Error,
             M: AsRef<str>;
    fn unwrap_or_report(self) -> Self::T
       where Self::E: Error;
}
Available on crate feature result only.
Expand description

std::result::Result extensions.

Methods for the Result type for more descriptive unwrapping and error handling patterns.

Required Associated Types§

Source

type T

Success value

Source

type E

Error value

Required Methods§

Source

fn expect_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
where Self::E: Debug, M: AsRef<str>,

Unwraps the result, yielding the content of an Ok.

The closure f is only evaluated if the result contains an error.

§Panics

Panics if the value is an Err, with a panic message provided by the closure f.

§Examples
let x: Result<u32, &str> = Err("emergency failure");
x.expect_with(|| "Testing expect_with");
Source

fn expect_or_report(self, msg: &str) -> Self::T
where Self::E: Error,

Unwraps the result, yielding the content of an Ok.

§Panics

Panics if the value is an Err, with a panic message given as msg and followed by a report of the full error chain.

§Examples
#[derive(Debug, derive_more::Error, derive_more::Display)]
#[display("Top-level error")]
struct TopError(SubError);

#[derive(Debug, derive_more::Error, derive_more::Display)]
#[display("Sub-level error")]
struct SubError;

let x: Result<u32, TopError> = Err(TopError(SubError));
x.expect_or_report("Failure detected");

The above panics with

Failure detected: Top-level error

Caused by:
      Sub-level error
Source

fn expect_or_report_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
where Self::E: Error, M: AsRef<str>,

Unwraps the result, yielding the content of an Ok.

§Panics

Panics if the value is an Err, with a panic message provided by the closure f and followed by a report of the full error chain.

§Examples
#[derive(Debug, derive_more::Error, derive_more::Display)]
#[display("Top-level error")]
struct TopError(SubError);

#[derive(Debug, derive_more::Error, derive_more::Display)]
#[display("Sub-level error")]
struct SubError;

let x: Result<u32, TopError> = Err(TopError(SubError));
x.expect_or_report_with(|| "Dynamic failure detected");
Source

fn unwrap_or_report(self) -> Self::T
where Self::E: Error,

Unwraps the result, yielding the content of an Ok.

§Panics

Panics if the value is an Err, with a report of the full error chain.

§Examples
#[derive(Debug, derive_more::Error, derive_more::Display)]
#[display("Top-level error")]
struct TopError(SubError);

#[derive(Debug, derive_more::Error, derive_more::Display)]
#[display("Sub-level error")]
struct SubError;

let x: Result<u32, TopError> = Err(TopError(SubError));
x.unwrap_or_report();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, E> ResultExt for Result<T, E>

Source§

type T = T

Source§

type E = E

Source§

fn expect_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
where Self::E: Debug, M: AsRef<str>,

Source§

fn expect_or_report(self, msg: &str) -> Self::T
where Self::E: Error,

Source§

fn expect_or_report_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
where Self::E: Error, M: AsRef<str>,

Source§

fn unwrap_or_report(self) -> Self::T
where Self::E: Error,

Implementors§