tagged_delegate

Crates.iotagged_delegate
lib.rstagged_delegate
version0.2.0
created_at2025-12-06 22:09:15.356426+00
updated_at2025-12-07 04:51:32.820692+00
descriptionBuilds a delegate macro on enums where each variant contains a value that implements the same trait
homepage
repositoryhttps://github.com/amazr/tagged_delegate
max_upload_size
id1970837
size19,856
Alex Mazur (amazr)

documentation

README

tagged_delegate

Automatically generate delegation macros for enums without requiring traits.

Overview

tagged_delegate generates delegation macros when applied to enums. Unlike enum_dispatch which requires annotating traits, tagged_delegate:

  • Works without traits entirely
  • Can delegate to methods on types you don't own
  • Requires the enum to be wrapped in a struct

Example

use tagged_delegate::tagged_delegate;

// You can also use #[tagged_delegate(shape_delegate)], the struct field and generated macro will have that name
#[tagged_delegate]
enum ShapeDelegate {
    Circle(Circle),
    Rectangle(Rectangle),
}

struct Shape {
    delegate: ShapeDelegate
}

impl Shape {
    fn area(&self) -> f64 {
        delegate!(self, |s| s.area())
    }

    fn zero(&mut self) {
        mut_delegate!(self, |s| s.set_position((0.0, 0.0)));
    }
}

struct Circle {
    radius: f64,
    position: (f64, f64),
}

impl Circle {
    fn area(&self) -> f64 {
        std::f64::consts::PI * self.radius * self.radius
    }

    fn set_position(&mut self, new_position: (f64, f64)) {
        self.position = new_position;
    }
}

struct Rectangle {
    width: f64,
    height: f64,
    position: (f64, f64),
}

impl Rectangle {
    fn area(&self) -> f64 {
        self.width * self.height
    }

    fn set_position(&mut self, new_position: (f64, f64)) {
        self.position = new_position;
    }
}

License

Apache-2.0

Commit count: 0

cargo fmt