Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Enums

For enums, place #[moxy(forward(fn ...))] on the enum itself. A match dispatch is generated across all variants.

Tuple Variants

use moxy::Forward;

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

struct Rect { width: f64, height: f64 }
impl Rect { fn area(&self) -> f64 { self.width * self.height } }

#[derive(Forward)]
#[moxy(forward(fn area(&self) -> f64))]
enum Shape {
    Circle(Circle),
    Rect(Rect),
}

let c = Shape::Circle(Circle { radius: 1.0 });
assert!((c.area() - std::f64::consts::PI).abs() < 1e-10);

For single-field tuple variants, the payload is bound and the method is called on it.

Named Variants

For named variants, the first field is used by default. Use #[moxy(forward)] on a specific field to override:

use moxy::Forward;

struct Runner { name: String }
impl Runner { fn run(&self) -> &str { &self.name } }

#[derive(Forward)]
#[moxy(forward(fn run(&self) -> &str))]
enum Task {
    Quick(Runner),
    Full {
        #[moxy(forward)]
        runner: Runner,
        priority: u8,
    },
}

Skipping Variants

Use #[moxy(forward(skip))] to exclude a variant from forwarding. The generated match arm calls unreachable!().

use moxy::Forward;

struct Worker;
impl Worker { fn exec(&self) -> i32 { 42 } }

#[derive(Forward)]
#[moxy(forward(fn exec(&self) -> i32))]
enum Action {
    Do(Worker),
    #[moxy(forward(skip))]
    Noop,
}

Unit variants without skip produce a compile error since there is no payload to forward to.