Derive Macro bit_roles::BitRole

source ·
#[derive(BitRole)]
Expand description

Bit role manager with compile-time value checking. Useful when you have a simple role enum definition and do not wish to work with raw integer role values. Each variant of your role enum must return a valid role value that is either zero or a power of two. Your role enum must also derive the [Copy] and [Clone] traits.

Check the BitRoleUnchecked variant if you need to work with raw integer role values or you have a complex role enum definition.

§Examples

Using simple role enum definitions.

use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum Permission {
    None = 0,
    SendMessage = 1,
    EditMessage = 2,
}

let mut roles = Permission::empty();

// Add a single role to the manager.
roles.add_one(Permission::SendMessage);

assert!(roles.has_one(Permission::SendMessage));

A compile-time error will be generated if any of the enum variant returns value that is neither zero nor a power of two.

use bit_roles::BitRole;

// This should not compile.
#[derive(Debug, BitRole, Copy, Clone)]
enum Permission {
    None = 0,
    InvalidRole = 5,
}