shepmaster / snafu

Easily assign underlying errors into domain-specific errors while adding context
https://docs.rs/snafu/
Apache License 2.0
1.4k stars 60 forks source link

Investigate adding `ResultExt::boxed` and `::boxed_local` #400

Closed shepmaster closed 10 months ago

shepmaster commented 1 year ago

In #398, we discussed the possibility of adding ResultExt::boxed to allow boxing errors as part of a chain:

impl<T, E> ResultExt<T, E> for Result<T, E> {
    fn boxed(self) -> Result<T, Box<dyn snafu::Error + 'static>>
    where
        E: snafu::Error + 'static,
    {
        self.map_err(|e| Box::new(e) as Box<dyn snafu::Error>)
    }
}

That would allow people to bin multiple concrete errors into one selector:

makes_err_a().boxed().context(ZetaSnafu)?;
makes_err_b().boxed().context(ZetaSnafu)?;

Although I'm not in love with throwing away error detail, sometimes this is really the best solution, such as when you are dealing with errors from an associated type:

trait Database {
    type Error: std::error::Error;

    fn connect(&self) -> Result<(), Self::Error>;
}