Struct Constrained

Source
pub struct Constrained<T, C: Constraint<T>> { /* private fields */ }
Expand description

A wrapper enforcing a numeric constraint at construction time.

Combine this with one of the provided marker types (such as NonNegative) or your own Constraint<T> implementation.

See the module documentation for details and usage patterns.

§Example

use twine_core::constraint::{Constrained, StrictlyPositive};

let n = Constrained::<_, StrictlyPositive>::new(42).unwrap();
assert_eq!(n.into_inner(), 42);

Implementations§

Source§

impl<T, C: Constraint<T>> Constrained<T, C>

Source

pub fn new(value: T) -> Result<Self, ConstraintError>

Constructs a new constrained value.

§Errors

Returns an error if the value does not satisfy the constraint.

Source

pub fn into_inner(self) -> T

Consumes the wrapper and returns the inner value.

Trait Implementations§

Source§

impl<T> Add for Constrained<T, NonNegative>
where T: Add<Output = T> + PartialOrd + Zero,

Adds two Constrained<T, NonNegative> values.

Assumes that summing two non-negative values yields a non-negative result. This holds for most numeric types (i32, f64, uom::Quantity, etc.), but may not for all possible T. The invariant is checked in debug builds.

§Panics

Panics in debug builds if the sum is unexpectedly negative.

Source§

type Output = Constrained<T, NonNegative>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl<T> Add for Constrained<T, NonPositive>
where T: Add<Output = T> + PartialOrd + Zero,

Adds two Constrained<T, NonPositive> values.

Assumes that summing two non-positive values yields a non-positive result. This holds for most numeric types (i32, f64, uom::Quantity, etc.), but may not for all possible T. The invariant is checked in debug builds.

§Panics

Panics in debug builds if the sum is unexpectedly positive.

Source§

type Output = Constrained<T, NonPositive>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl<T> Add for Constrained<T, StrictlyNegative>
where T: Add<Output = T> + PartialOrd + Zero,

Adds two Constrained<T, StrictlyNegative> values.

Assumes that summing two negative values yields a negative result. This holds for most numeric types (i32, f64, uom::Quantity, etc.), but may not for all possible T. The invariant is checked in debug builds.

§Panics

Panics in debug builds if the sum is unexpectedly non-negative.

Source§

type Output = Constrained<T, StrictlyNegative>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl<T> Add for Constrained<T, StrictlyPositive>
where T: Add<Output = T> + PartialOrd + Zero,

Adds two Constrained<T, StrictlyPositive> values.

Assumes that summing two positive values yields a positive result. This holds for most numeric types (i32, f64, uom::Quantity, etc.), but may not for all possible T. The invariant is checked in debug builds.

§Panics

Panics in debug builds if the sum is unexpectedly non-positive.

Source§

type Output = Constrained<T, StrictlyPositive>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl<T, C: Constraint<T>> AsRef<T> for Constrained<T, C>

Returns a reference to the inner unconstrained value.

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Clone, C: Clone + Constraint<T>> Clone for Constrained<T, C>

Source§

fn clone(&self) -> Constrained<T, C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, C: Debug + Constraint<T>> Debug for Constrained<T, C>

Source§

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

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

impl<T: Default, C: Default + Constraint<T>> Default for Constrained<T, C>

Source§

fn default() -> Constrained<T, C>

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

impl<T: Ord, C: Ord + Constraint<T>> Ord for Constrained<T, C>

Source§

fn cmp(&self, other: &Constrained<T, C>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq, C: PartialEq + Constraint<T>> PartialEq for Constrained<T, C>

Source§

fn eq(&self, other: &Constrained<T, C>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd, C: PartialOrd + Constraint<T>> PartialOrd for Constrained<T, C>

Source§

fn partial_cmp(&self, other: &Constrained<T, C>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, C> Sum for Constrained<T, C>
where C: Constraint<T>, Constrained<T, C>: Add<Output = Self> + Zero,

Sums constrained values for which addition is valid.

Applies to all constraints that are preserved under addition.

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<T> Zero for Constrained<T, NonNegative>
where T: PartialOrd + Zero,

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl<T> Zero for Constrained<T, NonPositive>
where T: PartialOrd + Zero,

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl<T: Copy, C: Copy + Constraint<T>> Copy for Constrained<T, C>

Source§

impl<T: Eq, C: Eq + Constraint<T>> Eq for Constrained<T, C>

Source§

impl<T, C: Constraint<T>> StructuralPartialEq for Constrained<T, C>

Auto Trait Implementations§

§

impl<T, C> Freeze for Constrained<T, C>
where T: Freeze,

§

impl<T, C> RefUnwindSafe for Constrained<T, C>

§

impl<T, C> Send for Constrained<T, C>
where T: Send, C: Send,

§

impl<T, C> Sync for Constrained<T, C>
where T: Sync, C: Sync,

§

impl<T, C> Unpin for Constrained<T, C>
where T: Unpin, C: Unpin,

§

impl<T, C> UnwindSafe for Constrained<T, C>
where T: UnwindSafe, C: 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> 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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Same for T

Source§

type Output = T

Should always be Self
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 = 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>,

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

impl<M> Measure for M
where M: Debug + PartialOrd + Add<Output = M> + Default + Clone,