Struct bit_roles::RoleManagerUnchecked

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

The unchecked role manager. Typically used when you need to use raw integer role values or have complex enum definitions.

Tuple Fields§

§0: usize§1: PhantomData<T>

Implementations§

source§

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

source

pub fn try_add_one( &mut self, role: RoleValue<T>, ) -> Result<&mut Self, RoleError>

Validates and adds a single role value to the manager instance. This is a non-panicking equivalent of the add_one method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

let mut roles = MyRole::empty();

// Add a new role to the manager.
roles
    .try_add_one(RoleValue::Role(MyRole::Staff))
    .expect("invalid role");

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

pub fn try_add_all( &mut self, roles: Vec<RoleValue<T>>, ) -> Result<&mut Self, RoleError>

Validates and adds multiple role values to the manager instance. This is a non-panicking equivalent of the add_all method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

let mut roles = MyRole::empty();

// Add multiple new roles to the manager.
roles
    .try_add_all(vec![
        RoleValue::Role(MyRole::Staff),
        RoleValue::Role(MyRole::Member),
    ])
    .expect("invalid roles");

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

pub fn try_remove_one( &mut self, role: RoleValue<T>, ) -> Result<&mut Self, RoleError>

Validates and removes a single role value from the manager instance. This is a non-panicking equivalent of the remove_one method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

let mut roles = MyRole::empty();

// Remove a role from the manager.
roles
    .try_remove_one(RoleValue::Role(MyRole::Staff))
    .expect("invalid role");

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

pub fn try_remove_all( &mut self, roles: Vec<RoleValue<T>>, ) -> Result<&mut Self, RoleError>

Validates and removes multiple role values from the manager instance. This is a non-panicking equivalent of the remove_all method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

let mut roles = MyRole::empty();

// Remove multiple roles from the manager.
roles
    .try_remove_all(vec![
        RoleValue::Role(MyRole::Staff),
        RoleValue::Role(MyRole::Member),
    ])
    .expect("invalid roles");

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

pub fn try_has_one(&self, role: RoleValue<T>) -> Result<bool, RoleError>

Validates and checks whether a single role is assigned to the manager instance. This is a non-panicking equivalent of the has_one method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

// 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
    .try_has_one(RoleValue::Role(MyRole::Staff))
    .expect("invalid role");

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

pub fn try_has_all(&self, roles: Vec<RoleValue<T>>) -> Result<bool, RoleError>

Validates and checks whether each of the roles is assigned to the manager instance. This is a non-panicking equivalent of the has_all method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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
    .try_has_all(vec![
        RoleValue::Role(MyRole::Staff),
        RoleValue::Role(MyRole::Member),
    ])
    .expect("invalid roles");

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

pub fn try_has_any(&self, roles: Vec<RoleValue<T>>) -> Result<bool, RoleError>

Validates and checks whether any one of the roles is assigned to the manager instance. This is a non-panicking equivalent of the has_any method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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
    .try_has_any(vec![
        RoleValue::Role(MyRole::Staff),
        RoleValue::Role(MyRole::Member),
    ])
    .expect("invalid roles");

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

pub fn try_not_one(&self, role: RoleValue<T>) -> Result<bool, RoleError>

Validates and checks whether a single role is not assigned to the manager instance. This is a non-panicking equivalent of the not_one method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

let mut roles = MyRole::empty();

// Check if the manager does not have a single role.
let does_not_have_role = roles
    .try_not_one(RoleValue::Role(MyRole::Staff))
    .expect("invalid role");

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

pub fn try_not_all(&self, roles: Vec<RoleValue<T>>) -> Result<bool, RoleError>

Validates and checks whether each of the roles is not assigned to the manager instance. This is a non-panicking equivalent of the not_all method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

let mut roles = MyRole::empty();

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

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

pub fn try_not_any(&self, roles: Vec<RoleValue<T>>) -> Result<bool, RoleError>

Validates and checks whether any of the roles is not assigned to the manager instance. This is a non-panicking equivalent of the not_any method.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

let mut roles = MyRole::empty();

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

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

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

Adds a single role to the manager instance. Panics if the role is invalid. Use try_add_one as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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. Panics if any of the roles is invalid. Use try_add_all as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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. Panics if the role is invalid. Use try_remove_one as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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. Panics if any of the roles is invalid. Use try_remove_all as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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. Panics if the role is invalid. Use try_has_one as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

// 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. Panics if any of the roles is invalid. Use try_has_all as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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. Panics if any of the roles is invalid. Use try_has_any as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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. Panics if the role is invalid. Use try_not_one as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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. Panics if any of the roles is invalid. Use try_not_all as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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. Panics if any of the roles is invalid. Use try_not_any as a non-panicking equivalent.

§Examples
use bit_roles::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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::{
    BitRoleUnchecked,
    RoleValue,
};

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

impl Into<usize> for MyRole {
    fn into(self) -> usize {
        self as usize
    }
}

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 RoleManagerUnchecked<T>

source§

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

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

impl<T> PartialEq for RoleManagerUnchecked<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 RoleManagerUnchecked<T>

Auto Trait Implementations§

§

impl<T> Freeze for RoleManagerUnchecked<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for RoleManagerUnchecked<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.