7 releases (1 stable)

Uses old Rust 2015

1.0.0 Jun 8, 2016
0.9.5 Apr 25, 2015
0.9.3 Feb 24, 2015
0.9.2 Jan 7, 2015
0.9.0 Nov 11, 2014

#2158 in Rust patterns

MIT/Apache

7KB

phantom-enum 0.9.5

Build Status

A simple macro library for creating phantom enums. Just simple sugar.

A phantom type enum is an enum-like arrangement where the enum is a module and trait and the variants are (uninstantiable) types.

This is very good for the static representation of state machines in which nothing can go wrong.

#[macro_use] #[no_link]
extern crate phantom_enum;

phantom_enum! {
    /// Put things here, of course
    pub enum TableItem {
        /// A bottle with a paper label reading “DRINK ME”.
        Potion,
        /// A cake with the words “EAT ME” marked in currants.
        Cake
    }
}

struct Person<T> {
    name: &'static str,
}

// Note how this restricts the methods to only meaningful types.
// (I look forward to generic bounds in struct definitions!)
impl<T: TableItem::Impl> Person<T> {
    fn new(name: &'static str) -> Person<T> {
        Person {
            name: name,
        }
    }
}

impl Person<TableItem::Potion> {
    fn drink_it(self) -> Person<TableItem::Cake> {
        println!("Shrinking! Oh look, there’s a box down here!");
        Person {
            name: self.name,
        }
    }
}

impl Person<TableItem::Cake> {
    fn eat_it(self) -> () {
        println!("Growing! OK, that’s enough of the story.");
        // Who can remember what comes next, anyway?
    }
}

fn main() {
    let person = Person::new("Alice");
    let person = person.drink_it();
    person.eat_it();
}

As you will observe with this example, if you have a Person<Potion>, you simply cannot call .eat_it(); for that, you must have a Person<Cake>. Similarly, once you have drunk that potion, you can’t drink it again.

Usage

Cargo all the way. http://crates.io/crates/phantom-enum

Author

Chris Morgan (chris-morgan) is the primary author and maintainer of this library.

License

This library is distributed under similar terms to Rust: dual licensed under the MIT license and the Apache license (version 2.0).

See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.

No runtime deps