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.
§Example
use twine_models::support::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>
impl<T, C: Constraint<T>> Constrained<T, C>
Sourcepub fn new(value: T) -> Result<Self, ConstraintError>
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.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the wrapper and returns the inner value.
Trait Implementations§
Source§impl<T> Add for Constrained<T, NonNegative>
Adds two Constrained<T, NonNegative> values.
impl<T> Add for Constrained<T, NonNegative>
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>
type Output = Constrained<T, NonNegative>
+ operator.Source§impl<T> Add for Constrained<T, NonPositive>
Adds two Constrained<T, NonPositive> values.
impl<T> Add for Constrained<T, NonPositive>
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>
type Output = Constrained<T, NonPositive>
+ operator.Source§impl<T> Add for Constrained<T, StrictlyNegative>
Adds two Constrained<T, StrictlyNegative> values.
impl<T> Add for Constrained<T, StrictlyNegative>
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>
type Output = Constrained<T, StrictlyNegative>
+ operator.Source§impl<T> Add for Constrained<T, StrictlyPositive>
Adds two Constrained<T, StrictlyPositive> values.
impl<T> Add for Constrained<T, StrictlyPositive>
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>
type Output = Constrained<T, StrictlyPositive>
+ operator.Source§impl<T, C: Constraint<T>> AsRef<T> for Constrained<T, C>
Returns a reference to the inner unconstrained value.
impl<T, C: Constraint<T>> AsRef<T> for Constrained<T, C>
Returns a reference to the inner unconstrained value.
Source§impl<T: Clone, C: Clone + Constraint<T>> Clone for Constrained<T, C>
impl<T: Clone, C: Clone + Constraint<T>> Clone for Constrained<T, C>
Source§fn clone(&self) -> Constrained<T, C>
fn clone(&self) -> Constrained<T, C>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug, C: Debug + Constraint<T>> Debug for Constrained<T, C>
impl<T: Debug, C: Debug + Constraint<T>> Debug for Constrained<T, C>
Source§impl<T: Default, C: Default + Constraint<T>> Default for Constrained<T, C>
impl<T: Default, C: Default + Constraint<T>> Default for Constrained<T, C>
Source§fn default() -> Constrained<T, C>
fn default() -> Constrained<T, C>
Source§impl<T: Ord, C: Ord + Constraint<T>> Ord for Constrained<T, C>
impl<T: Ord, C: Ord + Constraint<T>> Ord for Constrained<T, C>
Source§fn cmp(&self, other: &Constrained<T, C>) -> Ordering
fn cmp(&self, other: &Constrained<T, C>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq, C: PartialEq + Constraint<T>> PartialEq for Constrained<T, C>
impl<T: PartialEq, C: PartialEq + Constraint<T>> PartialEq for Constrained<T, C>
Source§impl<T: PartialOrd, C: PartialOrd + Constraint<T>> PartialOrd for Constrained<T, C>
impl<T: PartialOrd, C: PartialOrd + Constraint<T>> PartialOrd for Constrained<T, C>
Source§impl<T, C> Sum for Constrained<T, C>
Sums constrained values for which addition is valid.
impl<T, C> Sum for Constrained<T, C>
Sums constrained values for which addition is valid.
Applies to all constraints that are preserved under addition.