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