Permissions

Struct Permissions 

Source
pub struct Permissions { /* private fields */ }
Expand description

A collection of permissions with efficient storage and fast operations.

Uses compressed bitmap storage internally for optimal memory usage and O(1) permission checks. Designed for high-performance authorization systems that need to handle thousands of permissions per user efficiently.

§Examples

use axum_gate::permissions::Permissions;

// Create and populate permissions
let mut permissions = Permissions::new();
permissions
    .grant("read:profile")
    .grant("write:profile")
    .grant("delete:profile");

// Check individual permissions
assert!(permissions.has("read:profile"));
assert!(!permissions.has("admin:users"));

// Check multiple permissions
assert!(permissions.has_all(["read:profile", "write:profile"]));
assert!(permissions.has_any(["read:profile", "admin:users"]));

§Builder Pattern

use axum_gate::permissions::Permissions;

let permissions = Permissions::new()
    .with("read:api")
    .with("write:api")
    .build();

Implementations§

Source§

impl Permissions

Source

pub fn new() -> Self

Creates a new empty permission set.

§Examples
use axum_gate::permissions::Permissions;

let permissions = Permissions::new();
assert!(permissions.is_empty());
Source

pub fn grant<P>(&mut self, permission: P) -> &mut Self
where P: Into<PermissionId>,

Grants a permission to this permission set.

Returns a mutable reference to self for method chaining.

§Examples
use axum_gate::permissions::{Permissions, PermissionId};

let mut permissions = Permissions::new();
permissions
    .grant("read:profile")
    .grant(PermissionId::from("write:profile"));

assert!(permissions.has("read:profile"));
assert!(permissions.has(PermissionId::from("write:profile")));
Source

pub fn revoke<P>(&mut self, permission: P) -> &mut Self
where P: Into<PermissionId>,

Revokes a permission from this permission set.

Returns a mutable reference to self for method chaining.

§Examples
use axum_gate::permissions::{Permissions, PermissionId};

let mut permissions: Permissions = ["read:profile", "write:profile"].into_iter().collect();
permissions.revoke(PermissionId::from("write:profile"));

assert!(permissions.has("read:profile"));
assert!(!permissions.has("write:profile"));
Source

pub fn has<P>(&self, permission: P) -> bool
where P: Into<PermissionId>,

Checks if a specific permission is granted.

§Examples
use axum_gate::permissions::{Permissions, PermissionId};

let permissions: Permissions = ["read:profile"].into_iter().collect();

assert!(permissions.has("read:profile"));
assert!(permissions.has(PermissionId::from("read:profile")));
assert!(!permissions.has("write:profile"));
Source

pub fn has_all<I, P>(&self, permissions: I) -> bool
where I: IntoIterator<Item = P>, P: Into<PermissionId>,

Checks if all of the specified permissions are granted.

§Examples
use axum_gate::permissions::{Permissions, PermissionId};

let permissions: Permissions = [
    "read:profile",
    "write:profile",
    "read:posts",
].into_iter().collect();

assert!(permissions.has_all(["read:profile", "write:profile"]));
assert!(permissions.has_all([PermissionId::from("read:profile")]));
assert!(!permissions.has_all(["read:profile", "admin:users"]));
Source

pub fn has_any<I, P>(&self, permissions: I) -> bool
where I: IntoIterator<Item = P>, P: Into<PermissionId>,

Checks if any of the specified permissions are granted.

§Examples
use axum_gate::permissions::{Permissions, PermissionId};

let permissions: Permissions = ["read:profile"].into_iter().collect();

assert!(permissions.has_any(["read:profile", "write:profile"]));
assert!(permissions.has_any([PermissionId::from("read:profile")]));
assert!(!permissions.has_any(["write:profile", "admin:users"]));
Source

pub fn len(&self) -> usize

Returns the number of permissions in this set.

§Examples
use axum_gate::permissions::Permissions;

let permissions: Permissions = ["read:profile", "write:profile"].into_iter().collect();
assert_eq!(permissions.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the permission set contains no permissions.

§Examples
use axum_gate::permissions::Permissions;

let permissions = Permissions::new();
assert!(permissions.is_empty());

let mut permissions = Permissions::new();
permissions.grant("read:profile");
assert!(!permissions.is_empty());
Source

pub fn clear(&mut self)

Removes all permissions from this set.

§Examples
use axum_gate::permissions::Permissions;

let mut permissions: Permissions = ["read:profile", "write:profile"].into_iter().collect();
assert!(!permissions.is_empty());

permissions.clear();
assert!(permissions.is_empty());
Source

pub fn union(&mut self, other: &Permissions) -> &mut Self

Computes the union of this permission set with another.

This grants all permissions that exist in either set.

§Examples
use axum_gate::permissions::Permissions;

let mut permissions1: Permissions = ["read:profile"].into_iter().collect();
let permissions2: Permissions = ["write:profile"].into_iter().collect();

permissions1.union(&permissions2);

assert!(permissions1.has("read:profile"));
assert!(permissions1.has("write:profile"));
Source

pub fn intersection(&mut self, other: &Permissions) -> &mut Self

Computes the intersection of this permission set with another.

This keeps only permissions that exist in both sets.

§Examples
use axum_gate::permissions::Permissions;

let mut permissions1: Permissions = ["read:profile", "write:profile"].into_iter().collect();
let permissions2: Permissions = ["read:profile", "admin:users"].into_iter().collect();

permissions1.intersection(&permissions2);

assert!(permissions1.has("read:profile"));
assert!(!permissions1.has("write:profile"));
assert!(!permissions1.has("admin:users"));
Source

pub fn difference(&mut self, other: &Permissions) -> &mut Self

Computes the difference of this permission set with another.

This removes all permissions that exist in the other set.

§Examples
use axum_gate::permissions::Permissions;

let mut permissions1: Permissions = ["read:profile", "write:profile"].into_iter().collect();
let permissions2: Permissions = ["write:profile"].into_iter().collect();

permissions1.difference(&permissions2);

assert!(permissions1.has("read:profile"));
assert!(!permissions1.has("write:profile"));
Source

pub fn with<P>(self, permission: P) -> Self
where P: Into<PermissionId>,

Builder method for granting a permission (immutable version).

Use this when building permissions in a functional style or when you need to create permissions without mutable access. Prefer grant() for performance-critical code where you’re modifying existing permission sets.

§Examples
use axum_gate::permissions::{Permissions, PermissionId};

let permissions = Permissions::new()
    .with("read:profile")
    .with(PermissionId::from("write:profile"))
    .build();

assert!(permissions.has("read:profile"));
assert!(permissions.has("write:profile"));
Source

pub fn build(self) -> Self

Finalizes the builder pattern.

This method returns self unchanged, providing a clean conclusion to the builder pattern. Use this when you want to clearly signal the end of permission configuration.

§Examples
use axum_gate::permissions::Permissions;

let permissions = Permissions::new()
    .with("read:profile")
    .with("write:profile")
    .build();
Source

pub fn iter(&self) -> impl Iterator<Item = u64> + '_

Returns an iterator over the permission IDs in this collection.

Use this when you need to examine all granted permissions or integrate with external systems that work with permission IDs directly.

§Examples
use axum_gate::permissions::Permissions;

let permissions: Permissions = ["read:profile", "write:profile"].into_iter().collect();
let ids: Vec<u64> = permissions.iter().collect();
assert_eq!(ids.len(), 2);

Trait Implementations§

Source§

impl AsRef<RoaringTreemap> for Permissions

Source§

fn as_ref(&self) -> &RoaringTreemap

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Permissions

Source§

fn clone(&self) -> Permissions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Permissions

Source§

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

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

impl Default for Permissions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Permissions

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Permissions

Source§

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

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

impl From<Permissions> for RoaringTreemap

Source§

fn from(permissions: Permissions) -> Self

Converts to this type from the input type.
Source§

impl From<RoaringTreemap> for Permissions

Source§

fn from(bitmap: RoaringTreemap) -> Self

Converts to this type from the input type.
Source§

impl<P> FromIterator<P> for Permissions
where P: Into<PermissionId>,

Source§

fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self

Creates a permission set from an iterator of permission names.

§Examples
use axum_gate::permissions::Permissions;

let permissions: Permissions = ["read:profile", "write:profile", "read:posts"]
    .into_iter()
    .collect();

assert!(permissions.has("read:profile"));
assert!(permissions.has("write:profile"));
assert!(permissions.has("read:posts"));
Source§

impl PartialEq for Permissions

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Permissions

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Permissions

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

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

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

Source§

fn is_within(&self, b: &G2) -> bool

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,