derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

A derived trait providing access to the inner type of a newtype

Open fjarri opened this issue 3 years ago • 0 comments
trafficstars

While derived From and AsRef already remove some boilerplate when defining newtypes, it would be convenient to have a more specialized "newtype" trait that would give access to the inner type. That is, derive_more would define

trait Newtype {
    type Inner;
}

And writing

#[derive(Newtype)]
struct Outer(SomeType);

would produce

impl Newtype for Outer {
    type Inner = SomeType;
}

The associated Inner may be bounded:

trait Newtype: Sized {
    type Inner: Into<Self>;
}

In which case #[derive(Newtype)] would also derive From.

Does that sound like something you'd want to add to the crate? I can make a PR.

fjarri avatar Sep 13 '22 23:09 fjarri