pub struct UnitIntervalUpperOpen;Expand description
Marker type enforcing that a value lies in the right-open 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 UnitIntervalUpperOpen::new
associated function.
§Examples
Using with f64:
use twine_models::support::constraint::{Constrained, UnitIntervalUpperOpen};
// Generic constructor:
let a = Constrained::<_, UnitIntervalUpperOpen>::new(0.25).unwrap();
assert_eq!(a.into_inner(), 0.25);
// Associated constructor:
let b = UnitIntervalUpperOpen::new(0.0).unwrap();
assert_eq!(b.as_ref(), &0.0);
// Endpoint:
let z = UnitIntervalUpperOpen::zero::<f64>();
assert_eq!(z.into_inner(), 0.0);
// Error cases:
assert!(UnitIntervalUpperOpen::new(1.0).is_err());
assert!(UnitIntervalUpperOpen::new(1.5).is_err());
assert!(UnitIntervalUpperOpen::new(f64::NAN).is_err());Using with uom::si::f64::Ratio:
use twine_models::support::constraint::{Constrained, UnitIntervalUpperOpen};
use uom::si::{f64::Ratio, ratio::{ratio, percent}};
let r = Constrained::<Ratio, UnitIntervalUpperOpen>::new(Ratio::new::<ratio>(0.42)).unwrap();
assert_eq!(r.as_ref().get::<percent>(), 42.0);
let z = UnitIntervalUpperOpen::zero::<Ratio>();
assert_eq!(z.into_inner().get::<percent>(), 0.0);Implementations§
Source§impl UnitIntervalUpperOpen
impl UnitIntervalUpperOpen
Sourcepub fn new<T: UnitBounds>(
value: T,
) -> Result<Constrained<T, UnitIntervalUpperOpen>, ConstraintError>
pub fn new<T: UnitBounds>( value: T, ) -> Result<Constrained<T, UnitIntervalUpperOpen>, ConstraintError>
Constructs Constrained<T, UnitIntervalUpperOpen> if 0 ≤ value < 1.
§Errors
Fails if the value is outside the upper-open unit interval:
ConstraintError::BelowMinimumif less than zero.ConstraintError::AboveMaximumif greater than or equal to one.ConstraintError::NotANumberif comparison is undefined (e.g., NaN).
Sourcepub fn zero<T: UnitBounds>() -> Constrained<T, UnitIntervalUpperOpen>
pub fn zero<T: UnitBounds>() -> Constrained<T, UnitIntervalUpperOpen>
Returns the lower bound (zero) as a constrained value.
Trait Implementations§
Source§impl Clone for UnitIntervalUpperOpen
impl Clone for UnitIntervalUpperOpen
Source§fn clone(&self) -> UnitIntervalUpperOpen
fn clone(&self) -> UnitIntervalUpperOpen
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 UnitIntervalUpperOpen
impl<T: UnitBounds> Constraint<T> for UnitIntervalUpperOpen
Source§impl Debug for UnitIntervalUpperOpen
impl Debug for UnitIntervalUpperOpen
Source§impl Ord for UnitIntervalUpperOpen
impl Ord for UnitIntervalUpperOpen
Source§fn cmp(&self, other: &UnitIntervalUpperOpen) -> Ordering
fn cmp(&self, other: &UnitIntervalUpperOpen) -> 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 UnitIntervalUpperOpen
impl PartialEq for UnitIntervalUpperOpen
Source§impl PartialOrd for UnitIntervalUpperOpen
impl PartialOrd for UnitIntervalUpperOpen
impl Copy for UnitIntervalUpperOpen
impl Eq for UnitIntervalUpperOpen
impl StructuralPartialEq for UnitIntervalUpperOpen
Auto Trait Implementations§
impl Freeze for UnitIntervalUpperOpen
impl RefUnwindSafe for UnitIntervalUpperOpen
impl Send for UnitIntervalUpperOpen
impl Sync for UnitIntervalUpperOpen
impl Unpin for UnitIntervalUpperOpen
impl UnsafeUnpin for UnitIntervalUpperOpen
impl UnwindSafe for UnitIntervalUpperOpen
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