GithubHelp home page GithubHelp logo

polymorphic_enum's Introduction

polymorphic_enum

Example:

Input:

polymorphic_enum!(
    trait Move {
        fn execute(&self);
        fn valid_for_state(&self, state: u8) -> bool;
    }

    #[derive(Debug, Clone)]
    enum Moves {
        Attack { enemy_id: u32 },
        Defend,
    }
);

Will expand to:

trait Move {
    fn execute(&self);
    fn valid_for_state(&self, state: u8) -> bool;
}

#[derive(Debug, Clone)]
struct Attack{ enemy_id: u32 }
#[derive(Debug, Clone)]
struct Defend;

#[derive(Debug, Clone)]
enum Moves {
    Attack(Attack),
    Defend(Defend)
}

impl Move for Moves {
    fn execute(&self) {
        match self {
            Moves::Attack(inner) => inner.execute(),
            Moves::Defend(inner) => inner.execute(),
        }
    }

    fn valid_for_state(&self, state: u8) -> bool {
        match self {
            Moves::Attack(inner) => inner.valid_for_state(state),
            Moves::Defend(inner) => inner.valid_for_state(state),
        }
    }
}

impl From<Attack> for Moves {
    fn from(attack: Attack) -> Self {
        Moves::Attack(attack)
    }
}
impl From<Defend> for Moves {
    fn from(defend: Defend) -> Self {
        Moves::Defend(defend)
    }
}
impl Into<Attack> for Moves {
    fn into(self) -> Attack {
        match self {
            Moves::Attack(attack) => attack,
            _ => panic!("Tried to convert a Moves into a Attack but the enum variant was not Attack"),
        }
    }
}
impl Into<Defend> for Moves {
    fn into(self) -> Defend {
        match self {
            Moves::Defend(defend) => defend,
            _ => panic!("Tried to convert a Moves into a Defend but the enum variant was not Defend"),
        }
    }
}

You are now expected to implement the trait for each of the generated structs like so:

impl Move for Attack {
    fn execute(&self) {
        println!("Attack!");
    }

    fn valid_for_state(&self, state: u8) -> bool {
        state == 0
    }
}

impl Move for Defend {
    fn execute(&self) {
        println!("Defend!");
    }

    fn valid_for_state(&self, state: u8) -> bool {
        state == 1
    }
}

Then we can do something like this:

fn test_moves() {
    // Create a list of Moves
    let moves: Vec<Moves> = vec![Attack { enemy_id: 1 }.into(), Defend.into()];

    for m in moves {
        m.execute(); // Prints "Attack!" and "Defend!"
    }
}

polymorphic_enum's People

Contributors

testpersonal avatar

Watchers

retro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.