#value-type #enums #proc-macro #struct #procedural #structure #attributes

nightly macro value_from_type_macros

Implement enum (and cast methods) for a set of struct types

7 stable releases

Uses old Rust 2015

1.0.6 Apr 27, 2018
1.0.5 Apr 4, 2018
1.0.3 Apr 3, 2018
1.0.2 Feb 4, 2018
1.0.1 Jan 26, 2018

#74 in #value-type

MIT license

16KB
270 lines

Value from Type -Macros

Latest Version Rust Documentation

Procedural macro attribute to match structure types with an enum variant.

This macro can be applied on a module to make a connection between each defined struct and a newly created enum type. This enum is built into the same module as the macro is invocated upon. The macro will also implement value_from_type_traits::FromType on the enum for each struct (within the module) as generic argument.

Examples

See the documentation or value_from_type_macros/src/lib.rs for usage example(s).


lib.rs:

Procedural macro attribute to match structure types with an enum variant.

This macro can be applied on a module to make a connection between each defined struct and a newly created enum type. This enum is built into the same module as the macro is invocated upon. The macro will also implement value_from_type_traits::FromType on the enum for each struct (within the module) as generic argument.

Examples

// Attribute macro must be imported through a use statement.
use value_from_type_macros::value_from_type;
// Implemented trait on `EnumName`
use value_from_type_traits::IntoEnum;

mod temp {
    // The parameter indicates the enum identifier.
    #![value_from_type(EnumName)]

    #[derive(Debug)]
    pub struct X();
}

// Explicit import for sake of example.
use self::temp::{EnumName, X};
// use self::temp::*;

assert_eq!(EnumName::X, X::into_enum());

Dependencies

~2.5MB
~54K SLoC