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>

source

pub fn new(inner: T, cap: Cap) -> Self

Create a new instance with the given capability.

source

pub const fn cap(&self) -> Cap

Get the capability.

source

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));
source

pub unsafe fn access_unchecked(&self) -> &T

Access the inner value without capability check.

Safety

Caller must ensure not to violate the capability.

source

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()));
source

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§

source§

impl<T: Clone> Clone for WithCap<T>

source§

fn clone(&self) -> WithCap<T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more

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