Skip to content

Auto-generated fn accepts_…(&self) -> bool methods #11

@regexident

Description

@regexident

Given a state-machine like traffic_light.rs, i.e. …

transitions!(Traffic,
  [
    (Green, Advance) => Orange,
    (Orange, Advance) => Red,
    (Red, Advance) => Green,
    (Green, PassCar) => [Green, Orange]
  ]
);

… it would be nice if machine would auto-generate helper methods on the outer enum type for checking input acceptance …

impl Traffic {
    pub fn accepts_advance(&self) -> bool {
        match self {
            Traffic::Green(_) => true,
            Traffic::Orange(_) => true,
            Traffic::Red(_) => true,
            _ => false,
        }
    }

    pub fn accepts_pass_car(&self) -> bool {
        match self {
            Traffic::Green(_) => true,
            _ => false,
        }
    }
}

… instead of requiring one to implement them manually, as shown in the example (fn can_pass(&self) -> bool).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions