#[repr(C)]pub struct Quantity {
pub raw: QuantityRaw,
pub precision: u8,
}Expand description
Represents a quantity with a non-negative value and specified precision.
Capable of storing either a whole number (no decimal places) of ‘contracts’ or ‘shares’ (instruments denominated in whole units) or a decimal value containing decimal places for instruments denominated in fractional units.
Handles up to FIXED_PRECISION decimals of precision.
QUANTITY_MAX- Maximum representable quantity value.QUANTITY_MIN- 0 (non-negative values only).
Fields§
§raw: QuantityRawRepresents the raw fixed-point value, with precision defining the number of decimal places.
precision: u8The number of decimal places, with a maximum of FIXED_PRECISION.
Implementations§
Source§impl Quantity
impl Quantity
Sourcepub fn new_checked(value: f64, precision: u8) -> CorrectnessResult<Self>
pub fn new_checked(value: f64, precision: u8) -> CorrectnessResult<Self>
Creates a new Quantity instance with correctness checking.
§Errors
Returns an error if:
valueis invalid outside the representable range [0,QUANTITY_MAX].precisionis invalid outside the representable range [0,FIXED_PRECISION].
§Notes
PyO3 requires a Result type for proper error handling and stacktrace printing in Python.
Sourcepub fn non_zero_checked(value: f64, precision: u8) -> CorrectnessResult<Self>
pub fn non_zero_checked(value: f64, precision: u8) -> CorrectnessResult<Self>
Creates a new Quantity instance with a guaranteed non zero value.
§Errors
Returns an error if:
valueis zero.valuebecomes zero after rounding toprecision.valueis invalid outside the representable range [0,QUANTITY_MAX].precisionis invalid outside the representable range [0,FIXED_PRECISION].
§Notes
PyO3 requires a Result type for proper error handling and stacktrace printing in Python.
Sourcepub fn new(value: f64, precision: u8) -> Self
pub fn new(value: f64, precision: u8) -> Self
Creates a new Quantity instance.
§Panics
Panics if a correctness check fails. See Quantity::new_checked for more details.
Sourcepub fn non_zero(value: f64, precision: u8) -> Self
pub fn non_zero(value: f64, precision: u8) -> Self
Creates a new Quantity instance with a guaranteed non zero value.
§Panics
Panics if a correctness check fails. See Quantity::non_zero_checked for more details.
Sourcepub fn from_raw(raw: QuantityRaw, precision: u8) -> Self
pub fn from_raw(raw: QuantityRaw, precision: u8) -> Self
Creates a new Quantity instance from the given raw fixed-point value and precision.
§Panics
Panics if raw exceeds QUANTITY_RAW_MAX and is not a sentinel value.
Panics if precision exceeds FIXED_PRECISION.
Sourcepub fn from_raw_checked(
raw: QuantityRaw,
precision: u8,
) -> CorrectnessResult<Self>
pub fn from_raw_checked( raw: QuantityRaw, precision: u8, ) -> CorrectnessResult<Self>
Sourcepub fn checked_add(self, rhs: Self) -> Option<Self>
pub fn checked_add(self, rhs: Self) -> Option<Self>
Performs a checked addition, returning None on raw integer overflow, when the
result exceeds QUANTITY_RAW_MAX, when either operand is QUANTITY_UNDEF, or
when the operands have mixed raw scales (one at FIXED_PRECISION scale, the
other at a defi WEI_PRECISION scale).
Precision follows the Add implementation: uses the maximum precision of both operands.
Sourcepub fn checked_sub(self, rhs: Self) -> Option<Self>
pub fn checked_sub(self, rhs: Self) -> Option<Self>
Performs a checked subtraction, returning None if rhs is greater than self,
when either operand is QUANTITY_UNDEF, or when the operands have mixed raw
scales (one at FIXED_PRECISION scale, the other at a defi WEI_PRECISION scale).
Precision follows the Sub implementation: uses the maximum precision of both operands.
Sourcepub fn saturating_sub(self, rhs: Self) -> Self
pub fn saturating_sub(self, rhs: Self) -> Self
Computes a saturating subtraction between two quantities, logging when clamped.
When rhs is greater than self, the result is clamped to zero and a warning is logged.
Precision follows the Sub implementation: uses the maximum precision of both operands.
Sourcepub fn zero(precision: u8) -> Self
pub fn zero(precision: u8) -> Self
Creates a new Quantity instance with a value of zero with the given precision.
§Panics
Panics if precision exceeds the maximum allowed by check_fixed_precision.
Sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Returns true if the value of this instance is undefined.
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true if the value of this instance is position (> 0).
Sourcepub fn as_f64(&self) -> f64
pub fn as_f64(&self) -> f64
Returns the value of this instance as an f64.
§Panics
Panics if precision is beyond MAX_FLOAT_PRECISION (16).
Sourcepub fn as_decimal(&self) -> Decimal
pub fn as_decimal(&self) -> Decimal
Returns the value of this instance as a Decimal.
Sourcepub fn to_formatted_string(&self) -> String
pub fn to_formatted_string(&self) -> String
Returns a formatted string representation of this instance.
Sourcepub fn from_decimal_dp(
decimal: Decimal,
precision: u8,
) -> CorrectnessResult<Self>
pub fn from_decimal_dp( decimal: Decimal, precision: u8, ) -> CorrectnessResult<Self>
Creates a new Quantity from a Decimal value with specified precision.
Uses pure integer arithmetic on the Decimal’s mantissa and scale for fast conversion. The value is rounded to the specified precision using banker’s rounding (round half to even).
§Errors
Returns an error if:
precisionexceedsFIXED_PRECISION.- The decimal value is negative.
- The decimal value cannot be converted to the raw representation.
- Overflow occurs during scaling.
Sourcepub fn from_decimal(decimal: Decimal) -> CorrectnessResult<Self>
pub fn from_decimal(decimal: Decimal) -> CorrectnessResult<Self>
Creates a new Quantity from a Decimal value with precision inferred from the decimal’s scale.
The precision is determined by the scale of the decimal (number of decimal places). The value is rounded to the inferred precision using banker’s rounding (round half to even).
§Errors
Returns an error if:
- The inferred precision exceeds
FIXED_PRECISION. - The decimal value cannot be converted to the raw representation.
- Overflow occurs during scaling.
Sourcepub fn from_mantissa_exponent(
mantissa: u64,
exponent: i8,
precision: u8,
) -> Self
pub fn from_mantissa_exponent( mantissa: u64, exponent: i8, precision: u8, ) -> Self
Creates a new Quantity from a mantissa/exponent pair using pure integer arithmetic.
The value is mantissa * 10^exponent. This avoids all floating-point and Decimal
operations, making it ideal for exchange data that arrives as mantissa/exponent pairs.
§Panics
Panics if the resulting raw value exceeds QUANTITY_RAW_MAX.
Sourcepub fn from_mantissa_exponent_checked(
mantissa: u64,
exponent: i8,
precision: u8,
) -> CorrectnessResult<Self>
pub fn from_mantissa_exponent_checked( mantissa: u64, exponent: i8, precision: u8, ) -> CorrectnessResult<Self>
Checked variant of Quantity::from_mantissa_exponent.
§Errors
Returns an error if the precision is invalid or the resulting raw value
exceeds QUANTITY_RAW_MAX.
Sourcepub fn from_u256(amount: U256, precision: u8) -> CorrectnessResult<Self>
pub fn from_u256(amount: U256, precision: u8) -> CorrectnessResult<Self>
Creates a new Quantity from a U256 amount with specified precision.
§Errors
Returns an error if:
- Overflow occurs during scaling when precision is less than
FIXED_PRECISION. - The scaled U256 amount exceeds the
QuantityRawrange.
Source§impl Quantity
impl Quantity
Sourcepub fn from_wei<U>(raw_wei: U) -> Self
pub fn from_wei<U>(raw_wei: U) -> Self
Constructs a Quantity from a raw amount expressed in wei (18-decimal fixed-point).
The resulting Quantity will always have precision equal to 18.
§Panics
Panics if the supplied raw_wei cannot fit into an unsigned 128-bit integer (this
would exceed the numeric range of the internal QuantityRaw representation).
Trait Implementations§
impl Copy for Quantity
Source§impl<'de> Deserialize<'de> for Quantity
impl<'de> Deserialize<'de> for Quantity
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>,
impl Eq for Quantity
Source§impl From<&Quantity> for QuantityRaw
impl From<&Quantity> for QuantityRaw
Source§impl From<Quantity> for QuantityRaw
impl From<Quantity> for QuantityRaw
Source§impl Ord for Quantity
impl Ord for Quantity
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Quantity
impl PartialOrd for Quantity
Auto Trait Implementations§
impl Freeze for Quantity
impl RefUnwindSafe for Quantity
impl Send for Quantity
impl Sync for Quantity
impl Unpin for Quantity
impl UnsafeUnpin for Quantity
impl UnwindSafe for Quantity
Blanket Implementations§
impl<T> Allocation for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.