Skip to content

How to box the error enum? #372

@kosta

Description

@kosta

I have a library where my error enum variants are getting too big (in struct size, > 128 bytes) and clippy complains.

I wanted to explore how to Box the error variant, but I'm not sure this is easily possible:

The opaque example almost looks like what I want (but with pub struct Error(pub Box<InnerError>);). This does not translate 1:1 to Boxed: E.g. it only implements From<Box<InnerError>> for Error but no From<InnerError> (but I can implement that manually).

Also, while the example shows that this compiles:

fn validate_user(user_id: i32) -> Result<(), InnerError> {
    InvalidUserSnafu { user_id }.fail()
}

I would actually like a -> Result<(), Error> which breaks the example.

Is there an easy way to do this? (i.e. without littering the already-verbose context(...) with an additional map_err(BoxedError::from))

Ideally, I would just define my Result type as pub type Result<T> = std::result::Result<T, Box<Error>>; and tell derive(snafu) to derive impl IntoError<Box<Error>> for XxxSnafu automatically (or just make the enum boxed when being told to). Do you think that is a good idea? Do you see better ways to achieve this?

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