Struct capability::WithCap
source · pub struct WithCap<T> { /* private fields */ }
Expand description
A wrapper that holds a type with a capability.
Implementations§
source§impl<T> WithCap<T>
impl<T> WithCap<T>
sourcepub const fn can_access(&self, cap: Cap) -> bool
pub const fn can_access(&self, cap: Cap) -> bool
Check if the inner data can be accessed with the given capability.
Examples
use capability::{Cap, WithCap};
let data = WithCap::new(42, Cap::READ);
assert!(data.can_access(Cap::READ));
assert!(!data.can_access(Cap::WRITE));
sourcepub unsafe fn access_unchecked(&self) -> &T
pub unsafe fn access_unchecked(&self) -> &T
Access the inner value without capability check.
Safety
Caller must ensure not to violate the capability.
sourcepub const fn access(&self, cap: Cap) -> Result<&T, CapError>
pub const fn access(&self, cap: Cap) -> Result<&T, CapError>
Access the inner value with the given capability, or return CapError
if cannot access.
Examples
use capability::{Cap, CapError, WithCap};
let data = WithCap::new(42, Cap::READ);
assert_eq!(data.access(Cap::READ).unwrap(), &42);
assert_eq!(data.access(Cap::WRITE).err(), Some(CapError::default()));
sourcepub fn access_or_err<E>(&self, cap: Cap, err: E) -> Result<&T, E>
pub fn access_or_err<E>(&self, cap: Cap, err: E) -> Result<&T, E>
Access the inner value with the given capability, or return the given
err
if cannot access.
Examples
use capability::{Cap, WithCap};
let data = WithCap::new(42, Cap::READ);
assert_eq!(data.access_or_err(Cap::READ, "cannot read").unwrap(), &42);
assert_eq!(data.access_or_err(Cap::WRITE, "cannot write").err(), Some("cannot write"));
Trait Implementations§
Auto Trait Implementations§
impl<T> RefUnwindSafe for WithCap<T>where
T: RefUnwindSafe,
impl<T> Send for WithCap<T>where
T: Send,
impl<T> Sync for WithCap<T>where
T: Sync,
impl<T> Unpin for WithCap<T>where
T: Unpin,
impl<T> UnwindSafe for WithCap<T>where
T: UnwindSafe,
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
Mutably borrows from an owned value. Read more