Skip to main content

ResultExt

Trait ResultExt 

Source
pub trait ResultExt {
    type Success;
    type Error: Error + Send + Sync + 'static;

    // Required methods
    fn or_error(self) -> Result<Self::Success>;
    fn or_raise_typed<A, F>(self, err: F) -> ExnResult<Self::Success, A>
       where A: Error + Send + Sync + 'static,
             F: FnOnce() -> A;
    fn or_erased(self) -> ExnResult<Self::Success>;

    // Provided methods
    fn or_raise<A, F>(self, err: F) -> Result<Self::Success>
       where Self: Sized,
             A: Error + Send + Sync + 'static,
             F: FnOnce() -> A { ... }
    fn or_raise_erased<A, F>(self, err: F) -> ExnResult<Self::Success>
       where Self: Sized,
             A: Error + Send + Sync + 'static,
             F: FnOnce() -> A { ... }
}
Expand description

Convert native errors and exceptions into public Results, or add context lazily on failure.

Required Associated Types§

Source

type Success

The Ok type.

Source

type Error: Error + Send + Sync + 'static

The Err type that would be wrapped in an Exn.

Required Methods§

Source

fn or_error(self) -> Result<Self::Success>

Convert to a public Result without adding context. Native errors are raised at the caller; existing Error values and exceptions retain their locations.

Source

fn or_raise_typed<A, F>(self, err: F) -> ExnResult<Self::Success, A>
where A: Error + Send + Sync + 'static, F: FnOnce() -> A,

Like Self::or_raise(), retaining the context’s type in an exception.

Source

fn or_erased(self) -> ExnResult<Self::Success>

Raise a new exception on the Exn inside the Result, but erase its type. Errors implementing std::error::Error use ErrorExt::raise_erased.

Apply Exn::erased on the Err variant, refer to it for more information.

Provided Methods§

Source

fn or_raise<A, F>(self, err: F) -> Result<Self::Success>
where Self: Sized, A: Error + Send + Sync + 'static, F: FnOnce() -> A,

Add err() as context on failure, returning a public Result.

Reuses existing exception trees and preserves native sources and their caller locations.

Source

fn or_raise_erased<A, F>(self, err: F) -> ExnResult<Self::Success>
where Self: Sized, A: Error + Send + Sync + 'static, F: FnOnce() -> A,

Raise a new exception on the Exn inside the Result, and type-erase the result.

Apply Exn::raise and Exn::erased on the Err variant, refer to it for more information.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T, E> ResultExt for Result<T, E>
where E: Error + Send + Sync + 'static,

Source§

type Success = T

Source§

type Error = E

Source§

fn or_raise_typed<A, F>(self, err: F) -> ExnResult<Self::Success, A>
where A: Error + Send + Sync + 'static, F: FnOnce() -> A,

Source§

fn or_error(self) -> Result<T>

Source§

fn or_erased(self) -> ExnResult<Self::Success>

Implementors§

Source§

impl<T, E> ResultExt for ExnResult<T, E>
where E: Error + Send + Sync + 'static,