pub struct PositiveRealScalar<RealType: RealScalar>(/* private fields */);Expand description
Type-safe wrapper for strictly positive real scalar values.
PositiveRealScalar<T> guarantees that wrapped values are strictly greater than zero.
Zero is not valid for this type. For values that can be zero, use NonNegativeRealScalar.
§Mathematical Definition
PositiveRealScalar<T> = { x ∈ T : x > 0 }This is the set of positive real numbers ℝ⁺, which excludes zero.
§Use Cases
- Lengths and distances that cannot be zero
- Positive tolerances
- Scaling factors that must be positive
- Any quantity that is mathematically required to be > 0
§Examples
§Basic Usage
use num_valid::scalars::{PositiveRealScalar, ErrorsPositiveRealScalar};
use try_create::TryNew;
// Valid positive values
let length = PositiveRealScalar::try_new(2.5_f64).unwrap();
let tiny = PositiveRealScalar::try_new(f64::MIN_POSITIVE).unwrap();
// Zero is NOT positive
assert!(matches!(
PositiveRealScalar::try_new(0.0_f64),
Err(ErrorsPositiveRealScalar::ZeroValue { .. })
));
// Negative values rejected
assert!(matches!(
PositiveRealScalar::try_new(-1.0_f64),
Err(ErrorsPositiveRealScalar::NegativeValue { .. })
));§Difference from NonNegativeRealScalar
use num_valid::scalars::{PositiveRealScalar, NonNegativeRealScalar};
use try_create::TryNew;
// Zero is INVALID for PositiveRealScalar (x > 0)
assert!(PositiveRealScalar::try_new(0.0_f64).is_err());
// Zero is VALID for NonNegativeRealScalar (x ≥ 0)
assert!(NonNegativeRealScalar::try_new(0.0_f64).is_ok());Implementations§
Source§impl<RealType: RealScalar> PositiveRealScalar<RealType>
impl<RealType: RealScalar> PositiveRealScalar<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 PositiveRealScalar<RealType>
impl<RealType: RealScalar> AsRef<RealType> for PositiveRealScalar<RealType>
Source§impl<RealType: Clone + RealScalar> Clone for PositiveRealScalar<RealType>
impl<RealType: Clone + RealScalar> Clone for PositiveRealScalar<RealType>
Source§fn clone(&self) -> PositiveRealScalar<RealType>
fn clone(&self) -> PositiveRealScalar<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 PositiveRealScalar<RealType>
impl<RealType: Debug + RealScalar> Debug for PositiveRealScalar<RealType>
Source§impl<'de, RealType> Deserialize<'de> for PositiveRealScalar<RealType>where
RealType: for<'a> Deserialize<'a> + RealScalar,
impl<'de, RealType> Deserialize<'de> for PositiveRealScalar<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 PositiveRealScalar<RealType>where
RealType: Display + RealScalar,
impl<RealType> Display for PositiveRealScalar<RealType>where
RealType: Display + RealScalar,
Source§impl<RealType: RealScalar> IntoInner for PositiveRealScalar<RealType>
impl<RealType: RealScalar> IntoInner for PositiveRealScalar<RealType>
Source§impl<RealType: PartialEq + RealScalar> PartialEq for PositiveRealScalar<RealType>
impl<RealType: PartialEq + RealScalar> PartialEq for PositiveRealScalar<RealType>
Source§fn eq(&self, other: &PositiveRealScalar<RealType>) -> bool
fn eq(&self, other: &PositiveRealScalar<RealType>) -> bool
Tests for
self and other values to be equal, and is used by ==.Source§impl<RealType: PartialOrd + RealScalar> PartialOrd for PositiveRealScalar<RealType>
impl<RealType: PartialOrd + RealScalar> PartialOrd for PositiveRealScalar<RealType>
Source§impl<RealType> Serialize for PositiveRealScalar<RealType>where
RealType: Serialize + RealScalar,
impl<RealType> Serialize for PositiveRealScalar<RealType>where
RealType: Serialize + RealScalar,
Source§impl<RealType: RealScalar> TryNew for PositiveRealScalar<RealType>
impl<RealType: RealScalar> TryNew for PositiveRealScalar<RealType>
Source§fn try_new(value: RealType) -> Result<Self, Self::Error>
fn try_new(value: RealType) -> Result<Self, Self::Error>
Attempts to create a PositiveRealScalar from a value.
§Errors
ErrorsPositiveRealScalar::NegativeValue: If the input is negative (< 0).ErrorsPositiveRealScalar::ZeroValue: If the input is zero.
§Panics (Debug Mode Only)
In debug builds, panics if the input is not finite (NaN or infinity).
§Examples
use num_valid::scalars::{PositiveRealScalar, ErrorsPositiveRealScalar};
use try_create::TryNew;
// Positive value succeeds
assert!(PositiveRealScalar::try_new(1.0_f64).is_ok());
// Zero fails
assert!(matches!(
PositiveRealScalar::try_new(0.0_f64),
Err(ErrorsPositiveRealScalar::ZeroValue { .. })
));
// Negative fails
assert!(matches!(
PositiveRealScalar::try_new(-1.0_f64),
Err(ErrorsPositiveRealScalar::NegativeValue { value: v, .. }) if v == -1.0
));Source§type Error = ErrorsPositiveRealScalar<RealType>
type Error = ErrorsPositiveRealScalar<RealType>
The error type that can be returned by the
try_new method.impl<RealType: RealScalar> StructuralPartialEq for PositiveRealScalar<RealType>
Auto Trait Implementations§
impl<RealType> Freeze for PositiveRealScalar<RealType>
impl<RealType> RefUnwindSafe for PositiveRealScalar<RealType>
impl<RealType> Send for PositiveRealScalar<RealType>
impl<RealType> Sync for PositiveRealScalar<RealType>
impl<RealType> Unpin for PositiveRealScalar<RealType>
impl<RealType> UnwindSafe for PositiveRealScalar<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.