derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

#[from(forward)] with multiple `Box` fields generates dupe From impls

Open Sushisource opened this issue 4 years ago • 2 comments

Here's a simple repro:

#[derive(Debug)]
struct Foo {}
#[derive(Debug)]
struct Bar {}

#[derive(Debug, derive_more::From, derive_more::Display)]
pub enum ErrEnum {
    #[display("Blah")]]
    Unknown,

    #[display("Box1: {0:?}")]
    #[from(forward)]
    Box1(Box<Foo>),

    #[display("Box2: {0:?}")]
    #[from(forward)]
    Box2(Box<Bar>),
}

Which ultimately makes sense. Supporting it seemingly would mean necessarily breaking Box::new(Bar).into(), at least until specialization (I think?) exists. That said, the whole point of this for me is to never write Box::new. #[from(forward)] is admittedly not even the right construct here, because we don't actually want to call into on the Foo or Bar instance, but rather hand them to Box::new. Seemingly this calls for a #[from(box)] annotation.

Thoughts? I could look into making a PR for this if it makes sense.

Sushisource avatar Jan 28 '21 03:01 Sushisource

I would prefer the #[from(box)] approach. We actyually have a custom macro in our code base that does just that. If this were merged, we'd be very grateful.

appetrosyan avatar Jun 02 '22 14:06 appetrosyan

@appetrosyan doesn't #[from(types(Foo))] work here?

tyranron avatar Jun 02 '22 14:06 tyranron