#delegates #traits #macro

macro no-std auto-delegate-impl

Auto delegate allows you that automatic impl of traits and delegate their handling to child members

5 releases

0.1.2 Feb 25, 2024
0.1.1 Feb 25, 2024
0.0.9 Aug 28, 2023
0.0.7 Jul 9, 2023

#13 in #delegate

Download history 5/week @ 2024-09-02 14/week @ 2024-09-16 23/week @ 2024-09-23 4/week @ 2024-09-30 16/week @ 2024-10-14 15/week @ 2024-11-04 3/week @ 2024-11-18 2/week @ 2024-11-25 63/week @ 2024-12-09

68 downloads per month
Used in auto-delegate

MIT/Apache

38KB
848 lines

auto-delegate

Supports no_std

This Library is Created without std crates.

Description

Auto delegate allows you that automatic impl of traits and delegate their handling to children.

Usage

Struct

Give 'delegate' attribute to the trait which to be delegated, and give 'Delegate' to the structure requesting delegation

Example1

use auto_delegate::{Delegate, delegate};

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

#[derive(Default)]
struct CalcAdd;

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

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


fn main() {
    let parent = Parent::default();

    assert_eq!(parent.calc(2, 3), 5);
}

Example2: Multiple traits and fields

use auto_delegate::*;

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


#[derive(Default)]
struct CalcAdd;


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


#[delegate]
trait Movable {
    fn move_to(&mut self, pos: (usize, usize));

    fn pos(&self) -> (usize, usize);
}


#[delegate]
trait Resizable {
    fn resize(&mut self, width: usize, height: usize);

    fn size(&self) -> (usize, usize);
}


#[derive(Default)]
struct Transform2D {
    pos: (usize, usize),
    width: usize,
    height: usize
}


impl Movable for Transform2D {
    fn move_to(&mut self, pos: (usize, usize)) {
        self.pos = pos;
    }

    fn pos(&self) -> (usize, usize) {
        self.pos
    }
}


impl Resizable for Transform2D {
    fn resize(&mut self, width: usize, height: usize) {
        self.width = width;
        self.height = height;
    }

    fn size(&self) -> (usize, usize) {
        (self.width, self.height)
    }
}


#[derive(Default, Delegate)]
struct Parent<T: Default + Calc> {
    #[to(Movable, Resizable)]
    transform: Transform2D,

    #[to(Calc)]
    calculator: T
}


fn main() {
    let mut parent = Parent::<CalcAdd>::default();

    assert_eq!(parent.calc(3, 2), 5);

    parent.move_to((10, 11));
    assert_eq!(parent.pos(), (10, 11));

    parent.resize(100, 120);
    assert_eq!(parent.size(), (100, 120));
}

Enum

Like Struct, Enum can be delegated using Delegate.

Example

use auto_delegate::{delegate, Delegate};

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

#[derive(Default)]
struct CalcAdd;

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

#[derive(Default)]
struct CalcSub;

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


#[derive(Delegate)]
#[to(Calc)]
enum EnumCalc {
    Add(CalcAdd),
    Sub(CalcSub),
}


fn main() {
    let c = EnumCalc::Add(CalcAdd::default());
    assert_eq!(c.calc(3, 5), 8);


    let c = EnumCalc::Sub(CalcSub::default());
    assert_eq!(c.calc(3, 2), 1);
}

Todo

  • support self-receiver(v0.0.8)
  • support method without receiver(v0.1.0)
  • support trait fn which returns impl type.

Dependencies

~230–680KB
~16K SLoC