pub struct UnitInterval;Expand description
Marker type enforcing that a value lies in the closed unit interval: 0 ≤ x ≤ 1.
Requires T: UnitBounds.
We provide UnitBounds implementations for f32, f64, and uom::si::f64::Ratio.
You can construct a value constrained to [0, 1] using either the generic
Constrained::new method or the convenient UnitInterval::new
associated function.
Convenience constructors UnitInterval::zero and UnitInterval::one
are also provided for the endpoints.
§Examples
Using with f64:
use twine_models::support::constraint::{Constrained, UnitInterval};
// Generic constructor:
let a = Constrained::<_, UnitInterval>::new(0.25).unwrap();
assert_eq!(a.into_inner(), 0.25);
// Associated constructor:
let b = UnitInterval::new(1.0).unwrap();
assert_eq!(b.as_ref(), &1.0);
// Endpoints:
let z = UnitInterval::zero::<f64>();
let o = UnitInterval::one::<f64>();
assert_eq!((z.into_inner(), o.into_inner()), (0.0, 1.0));
// Error cases:
assert!(UnitInterval::new(-0.0001).is_err());
assert!(UnitInterval::new(1.0001).is_err());
assert!(UnitInterval::new(f64::NAN).is_err());Using with uom::si::f64::Ratio:
use twine_models::support::constraint::{Constrained, UnitInterval};
use uom::si::{f64::Ratio, ratio::{ratio, percent}};
let r = Constrained::<Ratio, UnitInterval>::new(Ratio::new::<ratio>(0.42)).unwrap();
assert_eq!(r.as_ref().get::<percent>(), 42.0);
let z = UnitInterval::zero::<Ratio>();
let o = UnitInterval::one::<Ratio>();
assert_eq!(z.into_inner().get::<ratio>(), 0.0);
assert_eq!(o.into_inner().get::<ratio>(), 1.0);Implementations§
Source§impl UnitInterval
impl UnitInterval
Sourcepub fn new<T: UnitBounds>(
value: T,
) -> Result<Constrained<T, UnitInterval>, ConstraintError>
pub fn new<T: UnitBounds>( value: T, ) -> Result<Constrained<T, UnitInterval>, ConstraintError>
Constructs Constrained<T, UnitInterval> if 0 ≤ value ≤ 1.
§Errors
Fails if the value is outside the closed unit interval:
ConstraintError::BelowMinimumif less than zero.ConstraintError::AboveMaximumif greater than one.ConstraintError::NotANumberif comparison is undefined (e.g., NaN).
Sourcepub fn zero<T: UnitBounds>() -> Constrained<T, UnitInterval>
pub fn zero<T: UnitBounds>() -> Constrained<T, UnitInterval>
Returns the lower bound (zero) as a constrained value.
Sourcepub fn one<T: UnitBounds>() -> Constrained<T, UnitInterval>
pub fn one<T: UnitBounds>() -> Constrained<T, UnitInterval>
Returns the upper bound (one) as a constrained value.
Trait Implementations§
Source§impl Clone for UnitInterval
impl Clone for UnitInterval
Source§fn clone(&self) -> UnitInterval
fn clone(&self) -> UnitInterval
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: UnitBounds> Constraint<T> for UnitInterval
impl<T: UnitBounds> Constraint<T> for UnitInterval
Source§impl Debug for UnitInterval
impl Debug for UnitInterval
Source§impl Ord for UnitInterval
impl Ord for UnitInterval
Source§fn cmp(&self, other: &UnitInterval) -> Ordering
fn cmp(&self, other: &UnitInterval) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for UnitInterval
impl PartialEq for UnitInterval
Source§impl PartialOrd for UnitInterval
impl PartialOrd for UnitInterval
impl Copy for UnitInterval
impl Eq for UnitInterval
impl StructuralPartialEq for UnitInterval
Auto Trait Implementations§
impl Freeze for UnitInterval
impl RefUnwindSafe for UnitInterval
impl Send for UnitInterval
impl Sync for UnitInterval
impl Unpin for UnitInterval
impl UnsafeUnpin for UnitInterval
impl UnwindSafe for UnitInterval
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more