Skip to main content

NonNegativeRealScalar

Struct NonNegativeRealScalar 

Source
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>

Source

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.

Since both operands are guaranteed to be non-negative, their sum is also non-negative.

Source§

type Output = NonNegativeRealScalar<RealType>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl<RealType: RealScalar> AsRef<RealType> for NonNegativeRealScalar<RealType>

Source§

fn as_ref(&self) -> &RealType

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<RealType: Clone + RealScalar> Clone for NonNegativeRealScalar<RealType>

Source§

fn clone(&self) -> NonNegativeRealScalar<RealType>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<RealType: Debug + RealScalar> Debug for NonNegativeRealScalar<RealType>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<RealType> Display for NonNegativeRealScalar<RealType>
where RealType: Display + RealScalar,

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<RealType: RealScalar> IntoInner for NonNegativeRealScalar<RealType>

Source§

fn into_inner(self) -> Self::InnerType

Consumes the struct and returns the inner value.

Source§

type InnerType = RealType

The type of the inner value that will be extracted.
Source§

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

Returns a reference to the maximum of self and other. Read more
Source§

fn max_by_value(self, other: Self) -> Self

Returns the maximum of self and other, consuming both values. Read more
Source§

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

Returns a reference to the minimum of self and other. Read more
Source§

fn min_by_value(self, other: Self) -> Self

Returns the minimum of self and other, consuming both values. Read more
Source§

impl<RealType: PartialEq + RealScalar> PartialEq for NonNegativeRealScalar<RealType>

Source§

fn eq(&self, other: &NonNegativeRealScalar<RealType>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<RealType: PartialOrd + RealScalar> PartialOrd for NonNegativeRealScalar<RealType>

Source§

fn partial_cmp( &self, other: &NonNegativeRealScalar<RealType>, ) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<RealType> Serialize for NonNegativeRealScalar<RealType>
where RealType: Serialize + RealScalar,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<RealType: PartialEq + RealScalar> StructuralPartialEq for NonNegativeRealScalar<RealType>

Source§

impl<RealType: RealScalar> TryNew for NonNegativeRealScalar<RealType>

Source§

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>

The error type that can be returned by the try_new method.
Source§

impl<RealType: RealScalar> Zero for NonNegativeRealScalar<RealType>

Implements Zero for NonNegativeRealScalar.

The zero value is always valid, and acts as the additive identity.

Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.

Auto Trait Implementations§

§

impl<RealType> Freeze for NonNegativeRealScalar<RealType>
where <RealType as RealScalar>::RawReal: Sized, RealType: Freeze,

§

impl<RealType> RefUnwindSafe for NonNegativeRealScalar<RealType>
where <RealType as RealScalar>::RawReal: Sized, RealType: RefUnwindSafe,

§

impl<RealType> Send for NonNegativeRealScalar<RealType>
where <RealType as RealScalar>::RawReal: Sized,

§

impl<RealType> Sync for NonNegativeRealScalar<RealType>
where <RealType as RealScalar>::RawReal: Sized,

§

impl<RealType> Unpin for NonNegativeRealScalar<RealType>
where <RealType as RealScalar>::RawReal: Sized, RealType: Unpin,

§

impl<RealType> UnsafeUnpin for NonNegativeRealScalar<RealType>
where <RealType as RealScalar>::RawReal: Sized, RealType: UnsafeUnpin,

§

impl<RealType> UnwindSafe for NonNegativeRealScalar<RealType>
where <RealType as RealScalar>::RawReal: Sized, RealType: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.