Skip to content

RFC: From for boxed errors #57

Closed
Closed
@nrc

Description

@nrc

In TiKV we often have very large error types (mostly due to including protobuf data with errors) and these errors appear on hot paths (because we try to recover from them). We found that boxing our errors improves our performance by a non-trivial amount.

A small tweak that makes our error handling much more ergonomic is to generate From impls from the unboxed type. Since we often convert error types from external crates into boxed errors, this lets us use a simple ?, rather than mapping the error.

E.g.,

#[derive(Error, Debug)]
pub enum Error {
    #[error(transparent)]
    ProtoErr(#[from] Box<protobuf::Error>),
    #[error(transparent)]
    Other(anyhow::Error),
}

impl From<protobuf::Error> for Error {
    fn from(e: protobuf::Error) -> Error {
        Error::ProtoErr(Box::new(e))
    }
}

I suppose that for backwards compatibility such a thing should be opt-in, e.g., using #[from(unbox)] or using a Cargo feature.

I have a prototype implementation which was straightforward (though I didn't implement any kind of opt-in).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions