Crate enum_properties[][src]

Expand description

A macro with two main purposes:

  • attaching static properties to enum variants
  • reducing the size of pointers to static records

The advantage in both cases is that the enum itself contains no data, and can be as small as a byte.

Example

(More complex enums are also supported. See enum_properties#examples for details.)

use enum_properties::enum_properties;
 
struct SolidProperties {
    verts: i32,
    edges: i32,
    faces: i32,
}
 
enum_properties! {
    #[derive(Clone, Copy, Debug)]
    enum PlatonicSolid: SolidProperties {
        Tetrahedron {
            verts: 4,
            edges: 6,
            faces: 4,
        },
        Cube {
            verts: 8,
            edges: 12,
            faces: 6,
        },
        Octahedron {
            verts: 6,
            edges: 12,
            faces: 8,
        },
        Dodecahedron {
            verts: 20,
            edges: 30,
            faces: 12,
        },
        Icosahedron {
            verts: 12,
            edges: 30,
            faces: 20,
        },
    }
}
 
fn main() {
    let cube = PlatonicSolid::Cube;
    assert_eq!(cube.verts - cube.edges + cube.faces, 2);
}

Macros

enum_properties

Defines a new enum and implements Deref for it.