pub struct Positive(pub Decimal);Expand description
A wrapper type that represents a guaranteed positive decimal value.
This type encapsulates a Decimal value and ensures through its API that
the contained value is always positive (greater than or equal to zero).
Tuple Fields§
§0: DecimalImplementations§
Source§impl Positive
impl Positive
Sourcepub const ZERO: Positive = crate::constants::ZERO
pub const ZERO: Positive = crate::constants::ZERO
A zero value represented as a Positive value.
Sourcepub const ONE: Positive = crate::constants::ONE
pub const ONE: Positive = crate::constants::ONE
A value of one represented as a Positive value.
Sourcepub const TWO: Positive = crate::constants::TWO
pub const TWO: Positive = crate::constants::TWO
A value of two represented as a Positive value.
Sourcepub const THREE: Positive = crate::constants::THREE
pub const THREE: Positive = crate::constants::THREE
A value of three represented as a Positive value.
Sourcepub const FOUR: Positive = crate::constants::FOUR
pub const FOUR: Positive = crate::constants::FOUR
A value of four represented as a Positive value.
Sourcepub const FIVE: Positive = crate::constants::FIVE
pub const FIVE: Positive = crate::constants::FIVE
A value of five represented as a Positive value.
Sourcepub const SIX: Positive = crate::constants::SIX
pub const SIX: Positive = crate::constants::SIX
A value of six represented as a Positive value.
Sourcepub const SEVEN: Positive = crate::constants::SEVEN
pub const SEVEN: Positive = crate::constants::SEVEN
A value of seven represented as a Positive value.
Sourcepub const EIGHT: Positive = crate::constants::EIGHT
pub const EIGHT: Positive = crate::constants::EIGHT
A value of eight represented as a Positive value.
Sourcepub const NINE: Positive = crate::constants::NINE
pub const NINE: Positive = crate::constants::NINE
A value of nine represented as a Positive value.
Sourcepub const TEN: Positive = crate::constants::TEN
pub const TEN: Positive = crate::constants::TEN
A value of ten represented as a Positive value.
Sourcepub const FIFTEEN: Positive = crate::constants::FIFTEEN
pub const FIFTEEN: Positive = crate::constants::FIFTEEN
A value of fifteen represented as a Positive value.
Sourcepub const TWENTY: Positive = crate::constants::TWENTY
pub const TWENTY: Positive = crate::constants::TWENTY
A value of twenty represented as a Positive value.
Sourcepub const TWENTY_FIVE: Positive = crate::constants::TWENTY_FIVE
pub const TWENTY_FIVE: Positive = crate::constants::TWENTY_FIVE
A value of twenty-five represented as a Positive value.
Sourcepub const THIRTY: Positive = crate::constants::THIRTY
pub const THIRTY: Positive = crate::constants::THIRTY
A value of thirty represented as a Positive value.
Sourcepub const THIRTY_FIVE: Positive = crate::constants::THIRTY_FIVE
pub const THIRTY_FIVE: Positive = crate::constants::THIRTY_FIVE
A value of thirty-five represented as a Positive value.
Sourcepub const FORTY: Positive = crate::constants::FORTY
pub const FORTY: Positive = crate::constants::FORTY
A value of forty represented as a Positive value.
Sourcepub const FORTY_FIVE: Positive = crate::constants::FORTY_FIVE
pub const FORTY_FIVE: Positive = crate::constants::FORTY_FIVE
A value of forty-five represented as a Positive value.
Sourcepub const FIFTY: Positive = crate::constants::FIFTY
pub const FIFTY: Positive = crate::constants::FIFTY
A value of fifty represented as a Positive value.
Sourcepub const FIFTY_FIVE: Positive = crate::constants::FIFTY_FIVE
pub const FIFTY_FIVE: Positive = crate::constants::FIFTY_FIVE
A value of fifty-five represented as a Positive value.
Sourcepub const SIXTY: Positive = crate::constants::SIXTY
pub const SIXTY: Positive = crate::constants::SIXTY
A value of sixty represented as a Positive value.
Sourcepub const SIXTY_FIVE: Positive = crate::constants::SIXTY_FIVE
pub const SIXTY_FIVE: Positive = crate::constants::SIXTY_FIVE
A value of sixty-five represented as a Positive value.
Sourcepub const SEVENTY: Positive = crate::constants::SEVENTY
pub const SEVENTY: Positive = crate::constants::SEVENTY
A value of seventy represented as a Positive value.
Sourcepub const SEVENTY_FIVE: Positive = crate::constants::SEVENTY_FIVE
pub const SEVENTY_FIVE: Positive = crate::constants::SEVENTY_FIVE
A value of seventy-five represented as a Positive value.
Sourcepub const EIGHTY: Positive = crate::constants::EIGHTY
pub const EIGHTY: Positive = crate::constants::EIGHTY
A value of eighty represented as a Positive value.
Sourcepub const EIGHTY_FIVE: Positive = crate::constants::EIGHTY_FIVE
pub const EIGHTY_FIVE: Positive = crate::constants::EIGHTY_FIVE
A value of eighty-five represented as a Positive value.
Sourcepub const NINETY: Positive = crate::constants::NINETY
pub const NINETY: Positive = crate::constants::NINETY
A value of ninety represented as a Positive value.
Sourcepub const NINETY_FIVE: Positive = crate::constants::NINETY_FIVE
pub const NINETY_FIVE: Positive = crate::constants::NINETY_FIVE
A value of ninety-five represented as a Positive value.
Sourcepub const HUNDRED: Positive = crate::constants::HUNDRED
pub const HUNDRED: Positive = crate::constants::HUNDRED
A value of one hundred represented as a Positive value.
Sourcepub const TWO_HUNDRED: Positive = crate::constants::TWO_HUNDRED
pub const TWO_HUNDRED: Positive = crate::constants::TWO_HUNDRED
A value of two hundred represented as a Positive value.
Sourcepub const THREE_HUNDRED: Positive = crate::constants::THREE_HUNDRED
pub const THREE_HUNDRED: Positive = crate::constants::THREE_HUNDRED
A value of three hundred represented as a Positive value.
Sourcepub const FOUR_HUNDRED: Positive = crate::constants::FOUR_HUNDRED
pub const FOUR_HUNDRED: Positive = crate::constants::FOUR_HUNDRED
A value of four hundred represented as a Positive value.
Sourcepub const FIVE_HUNDRED: Positive = crate::constants::FIVE_HUNDRED
pub const FIVE_HUNDRED: Positive = crate::constants::FIVE_HUNDRED
A value of five hundred represented as a Positive value.
Sourcepub const SIX_HUNDRED: Positive = crate::constants::SIX_HUNDRED
pub const SIX_HUNDRED: Positive = crate::constants::SIX_HUNDRED
A value of six hundred represented as a Positive value.
Sourcepub const SEVEN_HUNDRED: Positive = crate::constants::SEVEN_HUNDRED
pub const SEVEN_HUNDRED: Positive = crate::constants::SEVEN_HUNDRED
A value of seven hundred represented as a Positive value.
Sourcepub const EIGHT_HUNDRED: Positive = crate::constants::EIGHT_HUNDRED
pub const EIGHT_HUNDRED: Positive = crate::constants::EIGHT_HUNDRED
A value of eight hundred represented as a Positive value.
Sourcepub const NINE_HUNDRED: Positive = crate::constants::NINE_HUNDRED
pub const NINE_HUNDRED: Positive = crate::constants::NINE_HUNDRED
A value of nine hundred represented as a Positive value.
Sourcepub const THOUSAND: Positive = crate::constants::THOUSAND
pub const THOUSAND: Positive = crate::constants::THOUSAND
A value of one thousand represented as a Positive value.
Sourcepub const TWO_THOUSAND: Positive = crate::constants::TWO_THOUSAND
pub const TWO_THOUSAND: Positive = crate::constants::TWO_THOUSAND
A value of two thousand represented as a Positive value.
Sourcepub const THREE_THOUSAND: Positive = crate::constants::THREE_THOUSAND
pub const THREE_THOUSAND: Positive = crate::constants::THREE_THOUSAND
A value of three thousand represented as a Positive value.
Sourcepub const FOUR_THOUSAND: Positive = crate::constants::FOUR_THOUSAND
pub const FOUR_THOUSAND: Positive = crate::constants::FOUR_THOUSAND
A value of four thousand represented as a Positive value.
Sourcepub const FIVE_THOUSAND: Positive = crate::constants::FIVE_THOUSAND
pub const FIVE_THOUSAND: Positive = crate::constants::FIVE_THOUSAND
A value of five thousand represented as a Positive value.
Sourcepub const SIX_THOUSAND: Positive = crate::constants::SIX_THOUSAND
pub const SIX_THOUSAND: Positive = crate::constants::SIX_THOUSAND
A value of six thousand represented as a Positive value.
Sourcepub const SEVEN_THOUSAND: Positive = crate::constants::SEVEN_THOUSAND
pub const SEVEN_THOUSAND: Positive = crate::constants::SEVEN_THOUSAND
A value of seven thousand represented as a Positive value.
Sourcepub const EIGHT_THOUSAND: Positive = crate::constants::EIGHT_THOUSAND
pub const EIGHT_THOUSAND: Positive = crate::constants::EIGHT_THOUSAND
A value of eight thousand represented as a Positive value.
Sourcepub const NINE_THOUSAND: Positive = crate::constants::NINE_THOUSAND
pub const NINE_THOUSAND: Positive = crate::constants::NINE_THOUSAND
A value of nine thousand represented as a Positive value.
Sourcepub const TEN_THOUSAND: Positive = crate::constants::TEN_THOUSAND
pub const TEN_THOUSAND: Positive = crate::constants::TEN_THOUSAND
A value of ten thousand represented as a Positive value.
Sourcepub const PI: Positive = crate::constants::PI
pub const PI: Positive = crate::constants::PI
The mathematical constant π (pi) represented as a Positive value.
Sourcepub const E: Positive = crate::constants::E
pub const E: Positive = crate::constants::E
The mathematical constant e (Euler’s number) represented as a Positive value.
Sourcepub const INFINITY: Positive = crate::constants::INFINITY
pub const INFINITY: Positive = crate::constants::INFINITY
Represents the maximum positive value possible (effectively infinity).
Sourcepub fn new(value: f64) -> Result<Self, PositiveError>
pub fn new(value: f64) -> Result<Self, PositiveError>
Creates a new Positive value from a 64-bit floating-point number.
Sourcepub fn new_decimal(value: Decimal) -> Result<Self, PositiveError>
pub fn new_decimal(value: Decimal) -> Result<Self, PositiveError>
Creates a new Positive value directly from a Decimal.
Sourcepub fn to_dec_ref(&self) -> &Decimal
pub fn to_dec_ref(&self) -> &Decimal
Returns the inner Decimal ref.
Sourcepub fn to_f64(&self) -> f64
pub fn to_f64(&self) -> f64
Converts the value to a 64-bit floating-point number.
§Panics
This method will panic if the conversion fails. Use to_f64_checked()
or to_f64_lossy() for non-panicking alternatives.
Sourcepub fn to_f64_checked(&self) -> Option<f64>
pub fn to_f64_checked(&self) -> Option<f64>
Converts the value to f64, returning None if conversion fails.
Sourcepub fn to_f64_lossy(&self) -> f64
pub fn to_f64_lossy(&self) -> f64
Converts the value to f64 with lossy conversion (returns 0.0 on failure).
Sourcepub fn to_i64(&self) -> i64
pub fn to_i64(&self) -> i64
Converts the value to a 64-bit signed integer.
§Panics
This method will panic if the conversion fails. Use to_i64_checked()
for a non-panicking alternative.
Sourcepub fn to_i64_checked(&self) -> Option<i64>
pub fn to_i64_checked(&self) -> Option<i64>
Converts the value to i64, returning None if conversion fails.
Sourcepub fn to_u64(&self) -> u64
pub fn to_u64(&self) -> u64
Converts the inner value to a u64.
§Panics
This method will panic if the conversion fails. Use to_u64_checked()
for a non-panicking alternative.
Sourcepub fn to_u64_checked(&self) -> Option<u64>
pub fn to_u64_checked(&self) -> Option<u64>
Converts the value to u64, returning None if conversion fails.
Sourcepub fn to_usize(&self) -> usize
pub fn to_usize(&self) -> usize
Converts the value to a usize.
§Panics
This method will panic if the conversion fails. Use to_usize_checked()
for a non-panicking alternative.
Sourcepub fn to_usize_checked(&self) -> Option<usize>
pub fn to_usize_checked(&self) -> Option<usize>
Converts the value to usize, returning None if conversion fails.
Sourcepub fn pow(&self, n: Positive) -> Positive
pub fn pow(&self, n: Positive) -> Positive
Computes the result of raising the current value to the power of the given exponent.
Sourcepub fn powu(&self, n: u64) -> Positive
pub fn powu(&self, n: u64) -> Positive
Raises the current value to the power of n using unsigned integer exponentiation.
Sourcepub fn round_to_nice_number(&self) -> Positive
pub fn round_to_nice_number(&self) -> Positive
Rounds the current value to a “nice” number, based on its magnitude.
Sourcepub fn sqrt(&self) -> Positive
pub fn sqrt(&self) -> Positive
Calculates the square root of the value.
§Panics
This method will panic if the square root calculation fails.
Use sqrt_checked() for a non-panicking alternative.
Sourcepub fn sqrt_checked(&self) -> Result<Positive, PositiveError>
pub fn sqrt_checked(&self) -> Result<Positive, PositiveError>
Calculates the square root, returning an error if it fails.
Sourcepub fn round_to(&self, decimal_places: u32) -> Positive
pub fn round_to(&self, decimal_places: u32) -> Positive
Rounds the value to a specified number of decimal places.
Sourcepub fn format_fixed_places(&self, decimal_places: u32) -> String
pub fn format_fixed_places(&self, decimal_places: u32) -> String
Formats the value with a fixed number of decimal places.
Sourcepub fn clamp(&self, min: Positive, max: Positive) -> Positive
pub fn clamp(&self, min: Positive, max: Positive) -> Positive
Clamps the value between a minimum and maximum.
Sourcepub fn ceiling(&self) -> Positive
pub fn ceiling(&self) -> Positive
Returns the smallest integer greater than or equal to the value.
Sourcepub fn sub_or_zero(&self, other: &Decimal) -> Positive
pub fn sub_or_zero(&self, other: &Decimal) -> Positive
Subtracts a decimal value, returning zero if the result would be negative.
Sourcepub fn sub_or_none(&self, other: &Decimal) -> Option<Positive>
pub fn sub_or_none(&self, other: &Decimal) -> Option<Positive>
Subtracts a decimal value, returning None if the result would be negative.
Sourcepub fn checked_sub(&self, rhs: &Self) -> Result<Self, PositiveError>
pub fn checked_sub(&self, rhs: &Self) -> Result<Self, PositiveError>
Checked subtraction that returns Result instead of panicking.
Sourcepub fn saturating_sub(&self, rhs: &Self) -> Self
pub fn saturating_sub(&self, rhs: &Self) -> Self
Saturating subtraction that returns ZERO instead of negative.
Sourcepub fn checked_div(&self, rhs: &Self) -> Result<Self, PositiveError>
pub fn checked_div(&self, rhs: &Self) -> Result<Self, PositiveError>
Checked division that returns Result instead of panicking.
Sourcepub fn is_multiple(&self, other: f64) -> bool
pub fn is_multiple(&self, other: f64) -> bool
Checks whether the value is a multiple of another f64 value.
Sourcepub fn is_multiple_of(&self, other: &Positive) -> bool
pub fn is_multiple_of(&self, other: &Positive) -> bool
Checks whether the value is a multiple of another Positive value.
Sourcepub const unsafe fn new_unchecked(value: Decimal) -> Self
pub const unsafe fn new_unchecked(value: Decimal) -> Self
Creates a new Positive value without checking if the value is non-negative.
§Safety
The caller must ensure that value >= 0. Using this with a negative value
will violate the invariant of the Positive type and may cause undefined
behavior in code that relies on the positivity guarantee.
§Example
use positive::Positive;
use rust_decimal_macros::dec;
// SAFETY: We know 5.0 is positive
let value = unsafe { Positive::new_unchecked(dec!(5.0)) };
assert_eq!(value.to_f64(), 5.0);Trait Implementations§
Source§impl AbsDiffEq for Positive
impl AbsDiffEq for Positive
Source§fn default_epsilon() -> Self::Epsilon
fn default_epsilon() -> Self::Epsilon
Source§fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
Source§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq.Source§impl AddAssign<&Positive> for Decimal
impl AddAssign<&Positive> for Decimal
Source§fn add_assign(&mut self, rhs: &Positive)
fn add_assign(&mut self, rhs: &Positive)
+= operation. Read moreSource§impl AddAssign<Decimal> for Positive
impl AddAssign<Decimal> for Positive
Source§fn add_assign(&mut self, rhs: Decimal)
fn add_assign(&mut self, rhs: Decimal)
+= operation. Read moreSource§impl AddAssign<Positive> for Decimal
impl AddAssign<Positive> for Decimal
Source§fn add_assign(&mut self, rhs: Positive)
fn add_assign(&mut self, rhs: Positive)
+= operation. Read moreSource§impl AddAssign for Positive
impl AddAssign for Positive
Source§fn add_assign(&mut self, other: Positive)
fn add_assign(&mut self, other: Positive)
+= operation. Read moreSource§impl<'de> Deserialize<'de> for Positive
impl<'de> Deserialize<'de> for Positive
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 MulAssign<&Positive> for Decimal
impl MulAssign<&Positive> for Decimal
Source§fn mul_assign(&mut self, rhs: &Positive)
fn mul_assign(&mut self, rhs: &Positive)
*= operation. Read moreSource§impl MulAssign<Decimal> for Positive
impl MulAssign<Decimal> for Positive
Source§fn mul_assign(&mut self, rhs: Decimal)
fn mul_assign(&mut self, rhs: Decimal)
*= operation. Read moreSource§impl MulAssign<Positive> for Decimal
impl MulAssign<Positive> for Decimal
Source§fn mul_assign(&mut self, rhs: Positive)
fn mul_assign(&mut self, rhs: Positive)
*= operation. Read moreSource§impl Ord for Positive
impl Ord for Positive
Source§impl PartialOrd<&Positive> for f64
impl PartialOrd<&Positive> for f64
Source§impl PartialOrd<Decimal> for Positive
impl PartialOrd<Decimal> for Positive
Source§impl PartialOrd<Positive> for f64
impl PartialOrd<Positive> for f64
Source§impl PartialOrd<f64> for &Positive
impl PartialOrd<f64> for &Positive
Source§impl PartialOrd<f64> for Positive
impl PartialOrd<f64> for Positive
Source§impl PartialOrd for Positive
impl PartialOrd for Positive
Source§impl RelativeEq for Positive
impl RelativeEq for Positive
Source§fn default_max_relative() -> Self::Epsilon
fn default_max_relative() -> Self::Epsilon
Source§fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
Source§fn relative_ne(
&self,
other: &Rhs,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
RelativeEq::relative_eq.