Expand description
§Bit Roles
This crate enables you to implement granular role and permission management based on bit flags.
You can derive the BitRole trait for your role enum. It ensures compile-time validation for enum discriminants. Ensure you specify a discriminant for every enum variant; it must be either zero or a power of two. Also, remember to derive the Copy and Clone traits for your enum.
If you need a manager without compile-time checks, it’s also exported as
BitRoleUnchecked trait. This is useful if you want to use raw integer
values for roles or have a complex role enum definition. You will need to
implement the Into<usize> trait for your role enum, along with deriving
the Copy and Clone traits for it.
§Examples
use bit_roles::BitRole;
#[derive(Debug)]
struct User {
permissions: usize,
}
#[derive(Debug, BitRole, Copy, Clone)]
enum Permission {
None = 0,
SendMessage = 1,
EditMessage = 2,
}
let mut permissions = Permission::empty();
permissions.add_one(Permission::SendMessage);
let user = User {
permissions: permissions.get_value(),
};
// Checking if the user can edit messages.
let manager = Permission::from_value(user.permissions);
let can_edit_messages = manager.has_one(Permission::EditMessage); // `false`Structs§
- Role
Manager - The default role manager with compile-time value checks.
- Role
Manager Unchecked - The unchecked role manager. Typically used when you need to use raw integer role values or have complex enum definitions.
Enums§
Traits§
- BitRole
Impl - Bit role trait with compile-time value checks. Implements RoleManager for a role enum.
- BitRole
Unchecked Impl - Unchecked bit role trait. Implements RoleManagerUnchecked for a role enum.
- Role
Variant - The role variant trait. All role enums must implement this trait.
Functions§
- is_
valid_ role - Validates a role value.
Derive Macros§
- BitRole
- 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.
- BitRole
Unchecked - Bit role manager without value checking. Useful when you want to use raw
integer role values or you have a complex role enum definition. This
requires you to implement the
Into<usize>trait for your role enum yourself, and each variant 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.