#[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) -> Result<Self>
pub fn new_checked(value: f64, precision: u8) -> Result<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) -> Result<Self>
pub fn non_zero_checked(value: f64, precision: u8) -> Result<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 a correctness check fails. See Quantity::from_raw_checked for more details.
Sourcepub fn from_raw_checked(raw: QuantityRaw, precision: u8) -> Result<Self>
pub fn from_raw_checked(raw: QuantityRaw, precision: u8) -> Result<Self>
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 rules follow the Sub implementation: the left-hand precision is retained unless zero.
§Panics
Panics if the right-hand side has greater precision than the left-hand side (precision loss).
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 a correctness check fails. See Quantity::new_checked for more details.
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) -> Result<Self>
pub fn from_decimal_dp(decimal: Decimal, precision: u8) -> Result<Self>
Creates a new Quantity from a Decimal value with specified precision.
This method provides more reliable parsing by using Decimal arithmetic to avoid floating-point precision issues during 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 cannot be converted to the raw representation.
- Overflow occurs during scaling.
Sourcepub fn from_decimal(decimal: Decimal) -> Result<Self>
pub fn from_decimal(decimal: Decimal) -> Result<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.
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§
Source§impl<T: Into<QuantityRaw>> AddAssign<T> for Quantity
impl<T: Into<QuantityRaw>> AddAssign<T> for Quantity
Source§fn add_assign(&mut self, other: T)
fn add_assign(&mut self, other: T)
+= operation. Read moreSource§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>,
Source§impl From<&Quantity> for QuantityRaw
impl From<&Quantity> for QuantityRaw
Source§impl From<Quantity> for QuantityRaw
impl From<Quantity> for QuantityRaw
Source§impl<'py> IntoPyObject<'py> for Quantity
impl<'py> IntoPyObject<'py> for Quantity
Source§type Output = Bound<'py, <Quantity as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <Quantity as IntoPyObject<'py>>::Target>
Source§fn into_pyobject(
self,
py: Python<'py>,
) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
Source§impl<T: Into<QuantityRaw>> MulAssign<T> for Quantity
impl<T: Into<QuantityRaw>> MulAssign<T> for Quantity
Source§fn mul_assign(&mut self, other: T)
fn mul_assign(&mut self, other: T)
*= operation. Read moreSource§impl Ord for Quantity
impl Ord for Quantity
Source§impl PartialOrd for Quantity
impl PartialOrd for Quantity
Source§impl PyClassImpl for Quantity
impl PyClassImpl for Quantity
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = /// 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).
const RAW_DOC: &'static CStr = /// 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).
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type ThreadChecker = SendablePyClass<Quantity>
type ThreadChecker = SendablePyClass<Quantity>
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::ImmutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::ImmutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyClassNewTextSignature for Quantity
impl PyClassNewTextSignature for Quantity
const TEXT_SIGNATURE: &'static str = "(value, precision)"
Source§impl PyMethods<Quantity> for PyClassImplCollector<Quantity>
impl PyMethods<Quantity> for PyClassImplCollector<Quantity>
fn py_methods(self) -> &'static PyClassItems
Source§impl PyTypeInfo for Quantity
impl PyTypeInfo for Quantity
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
Source§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
Source§impl<T: Into<QuantityRaw>> SubAssign<T> for Quantity
impl<T: Into<QuantityRaw>> SubAssign<T> for Quantity
Source§fn sub_assign(&mut self, other: T)
fn sub_assign(&mut self, other: T)
-= operation. Read moreimpl Copy for Quantity
impl DerefToPyAny for Quantity
impl Eq for Quantity
impl ExtractPyClassWithClone 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 UnwindSafe for Quantity
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> 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
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.Source§impl<'a, 'py, T> FromPyObject<'a, 'py> for T
impl<'a, 'py, T> FromPyObject<'a, 'py> for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moreSource§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
Source§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.