Derive Macro enumset::EnumSetType

source · []
#[derive(EnumSetType)]
{
    // Attributes available to this derive:
    #[enumset]
}
Expand description

The procedural macro used to derive EnumSetType, and allow enums to be used with EnumSet.

It may be used with any enum with no data fields, at most 127 variants, and no variant discriminators larger than 127.

Additional Impls

In addition to the implementation of EnumSetType, this procedural macro creates multiple other impls that are either required for the macro to work, or make the procedural macro more ergonomic to use.

A full list of traits implemented as is follows:

  • Copy, Clone, Eq, PartialEq implementations are created to allow EnumSet to function properly. These automatic implementations may be suppressed using #[enumset(no_super_impls)], but these traits must still be implemented in another way.
  • PartialEq, Sub, BitAnd, BitOr, BitXor, and Not implementations are created to allow the crate to be used more ergonomically in expressions. These automatic implementations may be suppressed using #[enumset(no_ops)].

Options

Options are given with #[enumset(foo)] annotations attached to the same enum as the derive. Multiple options may be given in the same annotation using the #[enumset(foo, bar)] syntax.

A full list of options is as follows:

  • #[enumset(no_super_impls)] prevents the derive from creating implementations required for EnumSet to function. When this attribute is specified, implementations of Copy, Clone, Eq, and PartialEq. This can be useful if you are using a code generator that already derives these traits. These impls should function identically to the automatically derived versions, or unintentional behavior may be a result.
  • #[enumset(no_ops) prevents the derive from implementing any operator traits.
  • #[enumset(crate_name = "enumset2")] may be used to change the name of the enumset crate used in the generated code. When the std feature is enabled, enumset parses Cargo.toml to determine the name of the crate, and this flag is unnecessary.
  • #[enumset(repr = "u8")] may be used to specify the in-memory representation of EnumSets of this enum type. The effects of this are described in the EnumSet documentation under “FFI, Safety and repr. Allowed types are u8, u16, u32, u64 and u128. If this is not used, then the derive macro will choose a type to best fit the enum, but there are no guarantees about which type will be chosen.

When the serde feature is used, the following features may also be specified. These options may be used (with no effect) when building without the feature enabled:

  • #[enumset(serialize_repr = "u8")] may be used to specify the integer type used to serialize the underlying bitset. Any type allowed in the repr option may be used in this option.
  • #[enumset(serialize_as_list)] may be used to serialize the bitset as a list of enum variants instead of an integer. This requires [Deserialize] and [Serialize] be implemented on the enum.
  • #[enumset(serialize_deny_unknown)] causes the generated deserializer to return an error for unknown bits instead of silently ignoring them.

Examples

Deriving a plain EnumSetType:

#[derive(EnumSetType)]
pub enum Enum {
   A, B, C, D, E, F, G,
}

Deriving a sparse EnumSetType:

#[derive(EnumSetType)]
pub enum SparseEnum {
   A = 10, B = 20, C = 30, D = 127,
}

Deriving an EnumSetType without adding ops:

#[derive(EnumSetType)]
#[enumset(no_ops)]
pub enum NoOpsEnum {
   A, B, C, D, E, F, G,
}