Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
regexident opened this issue Feb 13, 2019 · 0 comments
Open

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

regexident opened this issue Feb 13, 2019 · 0 comments

Comments

@regexident
Copy link

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant