Skip to main content

Policy

Struct Policy 

Source
pub struct Policy<T> { /* private fields */ }
Expand description

A cloneable, thread-safe acceptance rule for values of type T.

Implementations§

Source§

impl<T: 'static> Policy<T>

Source

pub fn allow_if( name: impl Into<String>, expectation: impl Into<String>, predicate: impl Fn(&T) -> bool + Send + Sync + 'static, ) -> Result<Self, Error>

Creates a leaf policy from a predicate and a fixed expectation message.

Source

pub fn explain_with( name: impl Into<String>, evaluator: impl Fn(&T) -> (bool, String) + Send + Sync + 'static, ) -> Result<Self, Error>

Creates a leaf policy whose predicate also explains each result.

Source

pub fn all( name: impl Into<String>, policies: impl IntoIterator<Item = Self>, ) -> Result<Self, Error>

Creates a policy that accepts only when every child accepts.

All children are evaluated so the returned tree contains a complete explanation rather than stopping at the first rejection.

Source

pub fn any( name: impl Into<String>, policies: impl IntoIterator<Item = Self>, ) -> Result<Self, Error>

Creates a policy that accepts when at least one child accepts. All children are evaluated for a complete explanation.

Source

pub fn not(self, name: impl Into<String>) -> Result<Self, Error>

Creates a policy with the opposite result of this policy.

Source

pub fn evaluate(&self, value: &T) -> Decision

Evaluates a value and returns an explainable decision tree.

Source

pub fn name(&self) -> &str

Returns the policy name.

Source§

impl<T> Policy<T>
where T: PartialOrd + Display + Send + Sync + Copy + 'static,

Source

pub fn inclusive_range( name: impl Into<String>, minimum: T, maximum: T, ) -> Result<Self, Error>

Accepts values in the inclusive interval [minimum, maximum].

Source

pub fn exclusive_range( name: impl Into<String>, minimum: T, maximum: T, ) -> Result<Self, Error>

Accepts values in the exclusive interval (minimum, maximum).

Source§

impl Policy<f64>

Source

pub fn absolute_tolerance( name: impl Into<String>, target: f64, tolerance: f64, ) -> Result<Self, Error>

Accepts finite values within an absolute distance of a finite target.

Source

pub fn relative_tolerance( name: impl Into<String>, target: f64, fraction: f64, absolute_floor: f64, ) -> Result<Self, Error>

Accepts finite values within a fraction of a finite target.

absolute_floor prevents a zero or tiny target from creating an unusably small tolerance. The effective tolerance is the greater of abs(target) * fraction and absolute_floor.

Trait Implementations§

Source§

impl<T> Clone for Policy<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Policy<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Policy<T>

§

impl<T> !UnwindSafe for Policy<T>

§

impl<T> Freeze for Policy<T>
where Arc<dyn Fn(&T) -> Decision + Sync + Send>: Freeze,

§

impl<T> Send for Policy<T>
where Arc<dyn Fn(&T) -> Decision + Sync + Send>: Send,

§

impl<T> Sync for Policy<T>
where Arc<dyn Fn(&T) -> Decision + Sync + Send>: Sync,

§

impl<T> Unpin for Policy<T>
where Arc<dyn Fn(&T) -> Decision + Sync + Send>: Unpin,

§

impl<T> UnsafeUnpin for Policy<T>
where Arc<dyn Fn(&T) -> Decision + 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> 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, 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> 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, 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.