hobofan / ambassador

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

Delegating traits with associated types #62

Closed vigna closed 4 weeks ago

vigna commented 3 months ago

Our last manually delegated trait is std::ops::Index<usize, Output = bool>. It appears that ambassador presently does not support associated types. Is this the case?

dewert99 commented 3 months ago

Ambassador does support associated types, but they do not need to be specified, e.g.:

use ambassador::{delegatable_trait_remote, Delegate};
use core::ops::Index;

#[derive(Delegate)]
#[delegate(Index<usize>)]
struct Struct(Vec<bool>);

#[delegatable_trait_remote]
pub trait Index<Idx> {
    type Output: ?Sized;

    fn index(&self, index: Idx) -> &Self::Output;
}

fn main() {
    let s = Struct(vec![true, false, true]);
    dbg!(&s[1]);
}
vigna commented 3 months ago

I think it would be immensely useful to have these examples in the docs.

dewert99 commented 3 months ago

Would it work to add a link to https://github.com/hobofan/ambassador/tree/master/ambassador/tests/run-pass somewhere? It includes a lot of examples including associated_types.rs?

vigna commented 3 months ago

Yes, because the tests directory is not where people usually look for documentation. They may look there for examples of usage of a feature once they know about it, but they won't look for features there.