| Crates.io | tagged_delegate |
| lib.rs | tagged_delegate |
| version | 0.2.0 |
| created_at | 2025-12-06 22:09:15.356426+00 |
| updated_at | 2025-12-07 04:51:32.820692+00 |
| description | Builds a delegate macro on enums where each variant contains a value that implements the same trait |
| homepage | |
| repository | https://github.com/amazr/tagged_delegate |
| max_upload_size | |
| id | 1970837 |
| size | 19,856 |
Automatically generate delegation macros for enums without requiring traits.
tagged_delegate generates delegation macros when applied to enums. Unlike enum_dispatch which requires annotating traits, tagged_delegate:
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;
}
}
Apache-2.0