pub struct NonNegativeRealScalar<RealType: RealScalar>(/* private fields */);Expand description
Type-safe wrapper for non-negative real scalar values.
NonNegativeRealScalar<T> guarantees that wrapped values are greater than or equal to zero.
Zero is valid for this type. For values that must be strictly positive, use PositiveRealScalar.
§Mathematical Definition
NonNegativeRealScalar<T> = { x ∈ T : x ≥ 0 }This is the set of non-negative real numbers ℝ₀⁺, which includes zero.
§Use Cases
- Distances (can be zero when comparing a value to itself)
- Absolute values
- Magnitudes
- Any quantity that is mathematically required to be ≥ 0
§Examples
§Basic Usage
use num_valid::scalars::{NonNegativeRealScalar, ErrorsNonNegativeRealScalar};
use try_create::TryNew;
// Zero is valid
let zero = NonNegativeRealScalar::try_new(0.0_f64).unwrap();
assert_eq!(*zero.as_ref(), 0.0);
// Positive values are valid
let positive = NonNegativeRealScalar::try_new(5.0_f64).unwrap();
// Negative values are rejected
assert!(matches!(
NonNegativeRealScalar::try_new(-1.0_f64),
Err(ErrorsNonNegativeRealScalar::NegativeValue { .. })
));§Use Case: Computing Distances
use num_valid::scalars::NonNegativeRealScalar;
use try_create::TryNew;
fn distance(a: f64, b: f64) -> NonNegativeRealScalar<f64> {
NonNegativeRealScalar::try_new((a - b).abs()).unwrap()
}
// Different points
let d1 = distance(0.0, 5.0);
assert_eq!(*d1.as_ref(), 5.0);
// Same point (zero distance is valid!)
let d2 = distance(3.0, 3.0);
assert_eq!(*d2.as_ref(), 0.0);Implementations§
Source§impl<RealType: RealScalar> NonNegativeRealScalar<RealType>
impl<RealType: RealScalar> NonNegativeRealScalar<RealType>
Sourcepub fn into_inner(self) -> RealType
pub fn into_inner(self) -> RealType
Consumes the struct and returns the inner value.
Trait Implementations§
Source§impl<RealType: RealScalar> AsRef<RealType> for NonNegativeRealScalar<RealType>
impl<RealType: RealScalar> AsRef<RealType> for NonNegativeRealScalar<RealType>
Source§impl<RealType: Clone + RealScalar> Clone for NonNegativeRealScalar<RealType>
impl<RealType: Clone + RealScalar> Clone for NonNegativeRealScalar<RealType>
Source§fn clone(&self) -> NonNegativeRealScalar<RealType>
fn clone(&self) -> NonNegativeRealScalar<RealType>
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<RealType: Debug + RealScalar> Debug for NonNegativeRealScalar<RealType>
impl<RealType: Debug + RealScalar> Debug for NonNegativeRealScalar<RealType>
Source§impl<'de, RealType> Deserialize<'de> for NonNegativeRealScalar<RealType>where
RealType: for<'a> Deserialize<'a> + RealScalar,
impl<'de, RealType> Deserialize<'de> for NonNegativeRealScalar<RealType>where
RealType: for<'a> Deserialize<'a> + RealScalar,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<RealType> Display for NonNegativeRealScalar<RealType>where
RealType: Display + RealScalar,
impl<RealType> Display for NonNegativeRealScalar<RealType>where
RealType: Display + RealScalar,
Source§impl<RealType: RealScalar> IntoInner for NonNegativeRealScalar<RealType>
impl<RealType: RealScalar> IntoInner for NonNegativeRealScalar<RealType>
Source§impl<RealType: PartialEq + RealScalar> PartialEq for NonNegativeRealScalar<RealType>
impl<RealType: PartialEq + RealScalar> PartialEq for NonNegativeRealScalar<RealType>
Source§fn eq(&self, other: &NonNegativeRealScalar<RealType>) -> bool
fn eq(&self, other: &NonNegativeRealScalar<RealType>) -> bool
Tests for
self and other values to be equal, and is used by ==.Source§impl<RealType: PartialOrd + RealScalar> PartialOrd for NonNegativeRealScalar<RealType>
impl<RealType: PartialOrd + RealScalar> PartialOrd for NonNegativeRealScalar<RealType>
Source§impl<RealType> Serialize for NonNegativeRealScalar<RealType>where
RealType: Serialize + RealScalar,
impl<RealType> Serialize for NonNegativeRealScalar<RealType>where
RealType: Serialize + RealScalar,
Source§impl<RealType: RealScalar> TryNew for NonNegativeRealScalar<RealType>
impl<RealType: RealScalar> TryNew for NonNegativeRealScalar<RealType>
Source§fn try_new(value: RealType) -> Result<Self, Self::Error>
fn try_new(value: RealType) -> Result<Self, Self::Error>
Attempts to create a NonNegativeRealScalar from a value.
§Errors
Returns ErrorsNonNegativeRealScalar::NegativeValue if the input is negative.
§Panics (Debug Mode Only)
In debug builds, panics if the input is not finite (NaN or infinity).
§Examples
use num_valid::scalars::{NonNegativeRealScalar, ErrorsNonNegativeRealScalar};
use try_create::TryNew;
// Zero is valid
assert!(NonNegativeRealScalar::try_new(0.0_f64).is_ok());
// Positive is valid
assert!(NonNegativeRealScalar::try_new(1.0_f64).is_ok());
// Negative is rejected
assert!(matches!(
NonNegativeRealScalar::try_new(-1.0_f64),
Err(ErrorsNonNegativeRealScalar::NegativeValue { .. })
));Source§type Error = ErrorsNonNegativeRealScalar<RealType>
type Error = ErrorsNonNegativeRealScalar<RealType>
The error type that can be returned by the
try_new method.impl<RealType: RealScalar> StructuralPartialEq for NonNegativeRealScalar<RealType>
Auto Trait Implementations§
impl<RealType> Freeze for NonNegativeRealScalar<RealType>
impl<RealType> RefUnwindSafe for NonNegativeRealScalar<RealType>
impl<RealType> Send for NonNegativeRealScalar<RealType>
impl<RealType> Sync for NonNegativeRealScalar<RealType>
impl<RealType> Unpin for NonNegativeRealScalar<RealType>
impl<RealType> UnwindSafe for NonNegativeRealScalar<RealType>
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
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.