Skip to main content

First

Struct First 

Source
pub struct First<G> { /* private fields */ }
Expand description

Try policies in order and grant the value paired with the first one that accepts. When none accepts, the last policy’s reason is returned, so put the broadest policy last.

use kunobi_daemon::{
    ProcessId,
    peer::{Credentials, ExpectedProcess, First, Policy, SameUser},
};

#[derive(Clone, Debug, PartialEq)]
enum View { Full, Redacted }

let app = ProcessId::new(42);
let policy = First::new()
    .then(ExpectedProcess::new(move || app), View::Full)
    .then(SameUser, View::Redacted);

assert_eq!(policy.grant(&Credentials::new(app, true)), Ok(View::Full));
assert_eq!(policy.grant(&Credentials::new(ProcessId::new(7), true)), Ok(View::Redacted));
assert!(policy.grant(&Credentials::new(app, false)).is_err());

Implementations§

Source§

impl<G: Clone + Send + Sync + 'static> First<G>

Source

pub fn new() -> Self

An empty list, which rejects every connection with Rejected::NoPolicy.

Source

pub fn then<P>(self, policy: P, grant: G) -> Self
where P: Policy + Send + Sync + 'static,

Grant grant when policy accepts and no earlier policy did.

Trait Implementations§

Source§

impl<G> Debug for First<G>

Source§

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

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

impl<G> Default for First<G>

Source§

fn default() -> Self

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

impl<G> Policy for First<G>

Source§

type Grant = G

What an accepted connection is allowed, in the consumer’s own terms.
Source§

fn grant(&self, credentials: &Credentials) -> Result<G, Rejected>

Grant access or say why not. Runs on the accepting task, so it must not block or perform I/O: read state the consumer keeps current elsewhere.

Auto Trait Implementations§

§

impl<G> !RefUnwindSafe for First<G>

§

impl<G> !UnwindSafe for First<G>

§

impl<G> Freeze for First<G>
where Vec<Box<dyn Fn(&Credentials) -> Result<G, Rejected> + Sync + Send>>: Freeze,

§

impl<G> Send for First<G>
where Vec<Box<dyn Fn(&Credentials) -> Result<G, Rejected> + Sync + Send>>: Send,

§

impl<G> Sync for First<G>
where Vec<Box<dyn Fn(&Credentials) -> Result<G, Rejected> + Sync + Send>>: Sync,

§

impl<G> Unpin for First<G>
where Vec<Box<dyn Fn(&Credentials) -> Result<G, Rejected> + Sync + Send>>: Unpin,

§

impl<G> UnsafeUnpin for First<G>
where Vec<Box<dyn Fn(&Credentials) -> Result<G, Rejected> + Sync + Send>>: UnsafeUnpin,

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.