Skip to content

Attribute like from for Boxing large error variants #424

@clbarnes

Description

@clbarnes

Some recent clippy update warns for large enum error variants, and recommends boxing them. This is probably reasonable. It would be convenient if there was an annotation like from which would create the box itself rather than having to manually implement an increasingly common use case.

Looks like thiserror_ext has a related utility already: https://docs.rs/thiserror-ext/latest/thiserror_ext/derive.Box.html but could probably be cleaner. Something like

use thiserror::Error;

#[derive(Error, Debug)]
pub enum MyError {
    Small(#[from] small::Error),
    Big(#[from(autobox)] Box<big::Error>)
}

which would generate something like

use thiserror::Error;

#[derive(Error, Debug)]
pub enum MyError {
    Small(small::Error),
    Big(Box<big::Error>)
}

impl From<small::Error> for MyError {
    fn from(value: small::Error) -> Self {
        Self::Small(value)
    }
}

impl From<big::Error> for MyError {
    fn from(value: big::Error) -> Self {
        Self::Big(Box::new(value))
    }
}

Maybe with an impl From<Box<big::Error>> for MyError too, for completeness.

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