Struct bit_roles::RoleManager

source ·
pub struct RoleManager<T>(pub usize, pub PhantomData<T>);
Expand description

The default role manager with compile-time value checks.

Tuple Fields§

§0: usize§1: PhantomData<T>

Implementations§

source§

impl<T> RoleManager<T>
where T: RoleVariant,

source

pub fn add_one(&mut self, role: T) -> &mut Self

Adds a single role to the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
}

let mut roles = MyRole::empty();

// Add a new role to the manager.
roles.add_one(MyRole::Staff);

assert!(roles.has_one(MyRole::Staff));
  • role - The role to add to the manager.
source

pub fn add_all(&mut self, roles: Vec<T>) -> &mut Self

Adds multiple roles to the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
    Member = 2,
}

let mut roles = MyRole::empty();

// Add multiple new roles to the manager.
roles.add_all(vec![MyRole::Staff, MyRole::Member]);

assert!(roles.has_all(vec![MyRole::Staff, MyRole::Member]));
  • roles - The roles to add to the manager.
source

pub fn remove_one(&mut self, role: T) -> &mut Self

Removes a single role from the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
}

let mut roles = MyRole::empty();

// Remove a role from the manager.
roles.remove_one(MyRole::Staff);

assert!(roles.not_one(MyRole::Staff));
  • role - The role to remove from the manager.
source

pub fn remove_all(&mut self, roles: Vec<T>) -> &mut Self

Removes multiple roles from the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
    Member = 2,
}

let mut roles = MyRole::empty();

// Remove multiple roles from the manager.
roles.remove_all(vec![MyRole::Staff, MyRole::Member]);

assert!(roles.not_all(vec![MyRole::Staff, MyRole::Member]));
  • roles - The roles to remove from the manager.
source

pub fn has_one(&self, role: T) -> bool

Checks whether a single role is assigned to the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
}

// Create a role manager with initial `Staff` role.
let mut roles = MyRole::from_value(MyRole::Staff.into());

// Check if the manager has a single role.
let has_role = roles.has_one(MyRole::Staff);

assert!(has_role);
  • role - The role to check against the manager.
source

pub fn has_all(&self, roles: Vec<T>) -> bool

Checks whether each of the roles is assigned to the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
    Member = 2,
}

let mut roles = MyRole::empty();

// Add multiple roles to the role manager.
roles.add_all(vec![MyRole::Staff, MyRole::Member]);

// Check if the manager has all the provided roles.
let has_roles = roles.has_all(vec![MyRole::Staff, MyRole::Member]);

assert!(has_roles);
  • roles - The roles to check against the manager.
source

pub fn has_any(&self, roles: Vec<T>) -> bool

Checks whether any one of the roles is assigned to the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
    Member = 2,
}

let mut roles = MyRole::empty();

// Create a role manager with initial `Staff` role.
let mut roles = MyRole::from_value(MyRole::Staff.into());

// Check if the manager has any of the provided roles.
let has_roles = roles.has_any(vec![MyRole::Staff, MyRole::Member]);

assert!(has_roles);
  • roles - The roles to check against the manager.
source

pub fn not_one(&self, role: T) -> bool

Checks whether a single role is not assigned to the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
}

let mut roles = MyRole::empty();

// Check if the manager does not have a single role.
let does_not_have_role = roles.not_one(MyRole::Staff);

assert!(does_not_have_role);
  • role - The role to check against the manager.
source

pub fn not_all(&self, roles: Vec<T>) -> bool

Checks whether each of the roles is not assigned to the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
    Member = 2,
}

let mut roles = MyRole::empty();

// Check if the manager does not have all of the provided roles.
let does_not_have_roles = roles.not_all(vec![MyRole::Staff, MyRole::Member]);

assert!(does_not_have_roles);
  • roles - The roles to check against the manager.
source

pub fn not_any(&self, roles: Vec<T>) -> bool

Checks whether any of the roles is not assigned to the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
    Member = 2,
}

let mut roles = MyRole::empty();

// Check if the manager does not have any of the provided roles.
let does_not_have_roles = roles.not_any(vec![MyRole::Staff, MyRole::Member]);

assert!(does_not_have_roles);
  • roles - The roles to check against the manager.
source

pub fn get_value(&self) -> usize

Returns the value of the manager instance.

§Examples
use bit_roles::BitRole;

#[derive(Debug, BitRole, Copy, Clone)]
enum MyRole {
    None = 0,
    Staff = 1,
}

let roles = MyRole::empty();

// Get value of the manager instance.
let value = roles.get_value();

assert_eq!(value, 0);

Trait Implementations§

source§

impl<T: Debug> Debug for RoleManager<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> PartialEq for RoleManager<T>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> Eq for RoleManager<T>

Auto Trait Implementations§

§

impl<T> Freeze for RoleManager<T>

§

impl<T> RefUnwindSafe for RoleManager<T>
where T: RefUnwindSafe,

§

impl<T> Send for RoleManager<T>
where T: Send,

§

impl<T> Sync for RoleManager<T>
where T: Sync,

§

impl<T> Unpin for RoleManager<T>
where T: Unpin,

§

impl<T> UnwindSafe for RoleManager<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.