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);§Algebraic and Ordering Operations
NonNegativeRealScalar supports additive composition, the Zero trait,
and ordering helpers from Max and Min:
use num::Zero;
use num_valid::{
functions::{Max, Min},
scalars::NonNegativeRealScalar,
};
use try_create::TryNew;
let a = NonNegativeRealScalar::try_new(1.25_f64).unwrap();
let b = NonNegativeRealScalar::try_new(2.75_f64).unwrap();
// Add preserves non-negativity for valid operands.
let sum = a + b;
assert_eq!(*sum.as_ref(), 4.0);
// Zero is the additive identity.
let z = NonNegativeRealScalar::<f64>::zero();
assert!(z.is_zero());
assert_eq!(*(sum + z).as_ref(), 4.0);
let x = NonNegativeRealScalar::try_new(1.0_f64).unwrap();
let y = NonNegativeRealScalar::try_new(3.0_f64).unwrap();
assert_eq!(*Max::max_by_ref(&x, &y).as_ref(), 3.0);
assert_eq!(*Min::min_by_value(x, y).as_ref(), 1.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> Add for NonNegativeRealScalar<RealType>
Implements additive composition for NonNegativeRealScalar.
impl<RealType: RealScalar> Add for NonNegativeRealScalar<RealType>
Implements additive composition for NonNegativeRealScalar.
Since both operands are guaranteed to be non-negative, their sum is also non-negative.
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>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
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>,
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: RealScalar> Max for NonNegativeRealScalar<RealType>
Implements Max for NonNegativeRealScalar.
impl<RealType: RealScalar> Max for NonNegativeRealScalar<RealType>
Implements Max for NonNegativeRealScalar.
Maximum selection is delegated to the default trait behavior based on PartialOrd.
Source§fn max_by_ref<'a>(&'a self, other: &'a Self) -> &'a Self
fn max_by_ref<'a>(&'a self, other: &'a Self) -> &'a Self
Source§fn max_by_value(self, other: Self) -> Self
fn max_by_value(self, other: Self) -> Self
Source§impl<RealType: RealScalar> Min for NonNegativeRealScalar<RealType>
Implements Min for NonNegativeRealScalar.
impl<RealType: RealScalar> Min for NonNegativeRealScalar<RealType>
Implements Min for NonNegativeRealScalar.
Minimum selection is delegated to the default trait behavior based on PartialOrd.
Source§fn min_by_ref<'a>(&'a self, other: &'a Self) -> &'a Self
fn min_by_ref<'a>(&'a self, other: &'a Self) -> &'a Self
Source§fn min_by_value(self, other: Self) -> Self
fn min_by_value(self, other: Self) -> Self
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
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,
impl<RealType: PartialEq + RealScalar> StructuralPartialEq for NonNegativeRealScalar<RealType>
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>
try_new method.Source§impl<RealType: RealScalar> Zero for NonNegativeRealScalar<RealType>
Implements Zero for NonNegativeRealScalar.
impl<RealType: RealScalar> Zero for NonNegativeRealScalar<RealType>
Implements Zero for NonNegativeRealScalar.
The zero value is always valid, and acts as the additive identity.
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> UnsafeUnpin 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
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>,
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>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more