use std::{fmt::Debug, marker::PhantomData}; use fast_delegate::{delegate, Delegate}; #[delegate] pub trait Delegate { fn run(&self); } #[delegate] pub trait Delegate2 { fn run2(&self, value: usize) -> usize; fn run2_mut(&mut self, value: usize) -> usize; } #[delegate] pub trait Delegate3 { fn run3(&mut self, value: C, value2: i32) -> &usize; } #[delegate] pub trait Delegate4: Delegate { fn qwerqwer(&self); } #[derive(Delegate)] pub struct Delegated { #[to(Delegate, AnotherTrait, SomeTrait, Delegate2, Delegate4)] entity: DelegateImpl, entity2: Delegate2Impl, #[to(Delegate3)] entity3: Delegate3Impl, _marker: PhantomData, } #[delegate] pub trait SomeTrait { fn qwer(&self); } impl SomeTrait for DelegateImpl { fn qwer(&self) { println!("some trait "); } } #[delegate] pub trait AnotherTrait { fn asdf(&self); } impl AnotherTrait for DelegateImpl { fn asdf(&self) { println!("another trait"); } } pub struct DelegateImpl; impl Delegate for DelegateImpl { fn run(&self) { println!("Delegate"); } } impl Delegate4 for DelegateImpl { fn qwerqwer(&self) { println!("qwerqwer"); } } impl Delegate2 for DelegateImpl { fn run2(&self, value: usize) -> usize { println!("run2"); 2 } fn run2_mut(&mut self, value: usize) -> usize { println!("run2 mut"); 1 } } pub struct Delegate2Impl; impl Delegate2 for Delegate2Impl { fn run2(&self, value: usize) -> usize { println!("Delegate2"); 1 } fn run2_mut(&mut self, value: usize) -> usize { println!("hi"); 1123 } } pub struct Delegate3Impl(PhantomData); impl Delegate3 for Delegate3Impl where C: Default, { fn run3(&mut self, value: C, value2: i32) -> &usize { println!("hi"); &1 } } #[cfg(test)] mod test { use std::marker::PhantomData; use crate::{Delegate, Delegate2, Delegate3}; use crate::{Delegate2Impl, Delegate3Impl, DelegateImpl, Delegated}; #[test] fn delegate_test() { let mut player = Delegated { entity: DelegateImpl {}, entity2: Delegate2Impl {}, _marker: PhantomData::, entity3: Delegate3Impl(PhantomData), }; player.run(); player.run2(123); player.run3(123, 1); crate::AnotherTrait::::asdf(&player); } } #[delegate] pub trait Read { fn read(&self) -> &usize; } #[derive(Delegate)] struct Wrapper where R: Read, { #[to(Read)] inner: R, } #[derive(Delegate)] struct WrapperWrapper where R: Read, { #[to(Read)] inner: Wrapper, } struct Io { value: usize, } impl Read for Io { fn read(&self) -> &usize { &self.value } } fn main() { let wrapper = WrapperWrapper { inner: Wrapper { inner: Io { value: 1 }, }, }; wrapper.read(); }