Skip to main content

ResultExt

Trait ResultExt 

Source
pub trait ResultExt<T, E> {
    // Required methods
    fn context<C>(self, context: C) -> Result<T, C::Destination>
       where C: Contextual<E>;
    fn with_context<F, C>(self, context: F) -> Result<T, C::Destination>
       where F: FnOnce(&E) -> C,
             C: Contextual<E>;
    fn boxed(self) -> Result<T, Box<dyn Error + Send + Sync + 'static>>
       where E: Error + Send + Sync + 'static;
    fn boxed_local(self) -> Result<T, Box<dyn Error + 'static>>
       where E: Error + 'static;
}
Expand description

Extension trait on Result for ergonomic error context wrapping.

Import via use oopsie::prelude::* or use oopsie::ResultExt.

§Example

use oopsie::{Oopsie, ResultExt as _};

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum IoError {
    #[oopsie("IO failed: {source}")]
    Failed { source: std::io::Error },
}

let result: Result<(), std::io::Error> = Err(std::io::Error::other("disk full"));
let err = result.context(Failed).unwrap_err();
assert_eq!(err.to_string(), "IO failed: disk full");

Required Methods§

Source

fn context<C>(self, context: C) -> Result<T, C::Destination>
where C: Contextual<E>,

Wrap the error with an eagerly-evaluated context selector.

Source

fn with_context<F, C>(self, context: F) -> Result<T, C::Destination>
where F: FnOnce(&E) -> C, C: Contextual<E>,

Wrap the error with a lazily-evaluated context selector.

Source

fn boxed(self) -> Result<T, Box<dyn Error + Send + Sync + 'static>>
where E: Error + Send + Sync + 'static,

Convert the error into a boxed trait object (Box<dyn Error + Send + Sync>).

If E is itself a Box<C>, the result nests boxes: downcasting must target Box<C>, not C.

Source

fn boxed_local(self) -> Result<T, Box<dyn Error + 'static>>
where E: Error + 'static,

Convert the error into a boxed trait object (no Send/Sync bounds).

If E is itself a Box<C>, the result nests boxes: downcasting must target Box<C>, not C.

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<T, E> for Result<T, E>

Source§

fn context<C>(self, context: C) -> Result<T, C::Destination>
where C: Contextual<E>,

Source§

fn with_context<F, C>(self, context: F) -> Result<T, C::Destination>
where F: FnOnce(&E) -> C, C: Contextual<E>,

Source§

fn boxed(self) -> Result<T, Box<dyn Error + Send + Sync + 'static>>
where E: Error + Send + Sync + 'static,

Source§

fn boxed_local(self) -> Result<T, Box<dyn Error + 'static>>
where E: Error + 'static,

Implementors§