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;
}result only.Expand description
std::result::Result extensions.
Methods for the Result type for more descriptive unwrapping and error handling patterns.
Required Associated Types§
Required Methods§
Sourcefn expect_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
fn expect_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
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");Sourcefn expect_or_report(self, msg: &str) -> Self::T
fn expect_or_report(self, msg: &str) -> Self::T
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 errorSourcefn expect_or_report_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
fn expect_or_report_with<M, F: FnOnce() -> M>(self, f: F) -> Self::T
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");Sourcefn unwrap_or_report(self) -> Self::T
fn unwrap_or_report(self) -> Self::T
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.