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
impl Permissions
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty permission set.
§Examples
use axum_gate::permissions::Permissions;
let permissions = Permissions::new();
assert!(permissions.is_empty());Sourcepub fn grant<P>(&mut self, permission: P) -> &mut Selfwhere
P: Into<PermissionId>,
pub fn grant<P>(&mut self, permission: P) -> &mut Selfwhere
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")));Sourcepub fn revoke<P>(&mut self, permission: P) -> &mut Selfwhere
P: Into<PermissionId>,
pub fn revoke<P>(&mut self, permission: P) -> &mut Selfwhere
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"));Sourcepub fn has<P>(&self, permission: P) -> boolwhere
P: Into<PermissionId>,
pub fn has<P>(&self, permission: P) -> boolwhere
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"));Sourcepub fn has_all<I, P>(&self, permissions: I) -> bool
pub fn has_all<I, P>(&self, permissions: I) -> bool
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"]));Sourcepub fn has_any<I, P>(&self, permissions: I) -> bool
pub fn has_any<I, P>(&self, permissions: I) -> bool
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"]));Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn clear(&mut self)
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());Sourcepub fn union(&mut self, other: &Permissions) -> &mut Self
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"));Sourcepub fn intersection(&mut self, other: &Permissions) -> &mut Self
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"));Sourcepub fn difference(&mut self, other: &Permissions) -> &mut Self
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"));Sourcepub fn with<P>(self, permission: P) -> Selfwhere
P: Into<PermissionId>,
pub fn with<P>(self, permission: P) -> Selfwhere
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"));Sourcepub fn build(self) -> Self
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();Sourcepub fn iter(&self) -> impl Iterator<Item = u64> + '_
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
impl AsRef<RoaringTreemap> for Permissions
Source§fn as_ref(&self) -> &RoaringTreemap
fn as_ref(&self) -> &RoaringTreemap
Source§impl Clone for Permissions
impl Clone for Permissions
Source§fn clone(&self) -> Permissions
fn clone(&self) -> Permissions
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Permissions
impl Debug for Permissions
Source§impl Default for Permissions
impl Default for Permissions
Source§impl<'de> Deserialize<'de> for Permissions
impl<'de> Deserialize<'de> for Permissions
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for Permissions
impl Display for Permissions
Source§impl From<Permissions> for RoaringTreemap
impl From<Permissions> for RoaringTreemap
Source§fn from(permissions: Permissions) -> Self
fn from(permissions: Permissions) -> Self
Source§impl From<RoaringTreemap> for Permissions
impl From<RoaringTreemap> for Permissions
Source§fn from(bitmap: RoaringTreemap) -> Self
fn from(bitmap: RoaringTreemap) -> Self
Source§impl<P> FromIterator<P> for Permissionswhere
P: Into<PermissionId>,
impl<P> FromIterator<P> for Permissionswhere
P: Into<PermissionId>,
Source§fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self
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
impl PartialEq for Permissions
Source§impl Serialize for Permissions
impl Serialize for Permissions
impl StructuralPartialEq for Permissions
Auto Trait Implementations§
impl Freeze for Permissions
impl RefUnwindSafe for Permissions
impl Send for Permissions
impl Sync for Permissions
impl Unpin for Permissions
impl UnwindSafe for Permissions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.