dtolnay / dtolnay/thiserror

Attribute like `from` for `Box`ing large error variants

Open
#424 2 comments 12 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
5.5k
Forks
213
PR merge metrics
No merged PRs in 30d

Description

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

```rust
use thiserror::Error;

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

which would generate something like

```rust
use thiserror::Error;

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

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

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

Maybe with an `impl From> for MyError` too, for completeness.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.