Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

V0.0.5 Super traits

Choose a tag to compare

@not-elm not-elm released this 29 May 16:43
· 36 commits to master since this release

Supported Supertrait.

use auto_delegate_macros::{delegate, Delegate};

#[delegate]
trait Calc {
    fn calc(&self, x1: usize, x2: usize) -> usize;
}


#[delegate]
trait Label {
    fn label(&self) -> String;
}


#[delegate]
trait Increment: Calc + Label {
    fn increment(&mut self) -> usize;
}


#[derive(Default)]
struct Child {
    num: usize,
}


impl Increment for Child {
    fn increment(&mut self) -> usize {
        self.num += 1;
        self.num
    }
}


impl Calc for Child {
    fn calc(&self, x1: usize, x2: usize) -> usize {
        x1 + x2
    }
}


impl Label for Child {
    fn label(&self) -> String {
        String::from("add")
    }
}

#[derive(Default, Delegate)]
struct Parent {
    #[to(Calc, Increment, Label)]
    child: Child,
}


fn main() {
    let mut parent = Parent::default();
    assert_eq!(parent.increment(), 1);
    assert_eq!(parent.increment(), 2);
    assert_eq!(parent.calc(3, 2), 5);
    assert_eq!(parent.label(), "add");
}