hobofan / ambassador

Delegation of trait implementations via procedural macros
Apache License 2.0
245 stars 13 forks source link

trying to delegate `std::ops::Add` #48

Closed yatesco closed 1 year ago

yatesco commented 1 year ago

Hi there! I'm sure this is me not understanding something, but I have a new type struct pub struct MyType(#[serde(serialize_with = "serialize_date_time")] pub NaiveDateTime); that manually implements the inner type's traits. For example:

 impl std::ops::Add<chrono::Duration> for MyType {
    type Output = MyType;

    fn add(self, rhs: chrono::Duration) -> MyType {
        MyType(self.0 + rhs)
    }
}

I tried to replace it with the following:

#[derive(Delegate, Serialize, Debug, Copy, Clone)]
#[delegate(std::ops::Add<X>, generics = "X")]
pub struct MyType(#[serde(serialize_with = "serialize_date_time")] pub NaiveDateTime);

but I get the following error:

Error[E0046]: not all trait items implemented, missing: `Output`, `add`
  --> XXXXXXXmod.rs:27:14
   |
27 |     #[derive(Delegate, Serialize, Debug, Copy, Clone)]
   |              ^^^^^^^^ missing `Output`, `add` in implementation
   |
   = note: this error originates in the derive macro `Delegate` (in Nightly builds, run with -Z macro-backtrace for more info)
   = help: implement the missing item: `type Output = Type;`
   = help: implement the missing item: `fn add(self, _: Rhs) -> <Self as Add<Rhs>>::Output { todo!() }`

For more information about this error, try `rustc --explain E0046`.
warning: `domain` (lib) generated 4 warnings
error: could not compile `domain` due to 2 previous errors; 4 warnings emitted

First, is what I am trying to achieve possible, and secondly, what am I doing wrong :-).

Thanks!

P.S> issue asked https://users.rust-lang.org/t/newtype-and-traits-of-the-inner-type/92664/2

hobofan commented 1 year ago

I think this is most likely due to std::ops::Add being a remote trait that needs its definition to be duplicated together with #[delegatable_trait_remote].

For reference see the README here or this test: https://github.com/hobofan/ambassador/blob/a6703fed5e3c3672af0ec76e8767c78f23a0a218/ambassador/tests/run-pass/associated_types.rs

(might also been a something completely different; haven't used the crate in years myself 😬 )

yatesco commented 1 year ago

Thanks @hobofan.