#[repr(C)]pub struct Money {
pub raw: MoneyRaw,
pub currency: Currency,
}Expand description
Fields§
§raw: MoneyRawRepresents the raw fixed-point amount, with currency.precision defining the number of decimal places.
currency: CurrencyThe currency denomination associated with the monetary amount.
Implementations§
Source§impl Money
impl Money
Sourcepub fn new_checked(amount: f64, currency: Currency) -> CorrectnessResult<Self>
pub fn new_checked(amount: f64, currency: Currency) -> CorrectnessResult<Self>
Sourcepub fn new(amount: f64, currency: Currency) -> Self
pub fn new(amount: f64, currency: Currency) -> Self
Creates a new Money instance.
§Panics
Panics if a correctness check fails. See Money::new_checked for more details.
Sourcepub fn from_raw(raw: MoneyRaw, currency: Currency) -> Self
pub fn from_raw(raw: MoneyRaw, currency: Currency) -> Self
Creates a new Money instance from the given raw fixed-point value and the specified currency.
§Panics
Panics if a correctness check fails. See Money::from_raw_checked for more details.
Sourcepub fn from_raw_checked(
raw: MoneyRaw,
currency: Currency,
) -> CorrectnessResult<Self>
pub fn from_raw_checked( raw: MoneyRaw, currency: Currency, ) -> CorrectnessResult<Self>
Sourcepub fn from_mantissa_exponent(
mantissa: i64,
exponent: i8,
currency: Currency,
) -> Self
pub fn from_mantissa_exponent( mantissa: i64, exponent: i8, currency: Currency, ) -> Self
Creates a new Money 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 MONEY_RAW_MAX or MONEY_RAW_MIN.
Sourcepub fn zero(currency: Currency) -> Self
pub fn zero(currency: Currency) -> Self
Creates a new Money instance with a value of zero with the given Currency.
§Panics
Panics if a correctness check fails. See Money::new_checked for more details.
Sourcepub fn normalized(&self) -> Self
pub fn normalized(&self) -> Self
Returns a copy with raw value rounded to currency precision, stripping any sub-scale bits.
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true if the value of this instance is positive (> 0).
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 falls outside [MONEY_RAW_MIN, MONEY_RAW_MAX], or when the operands
have mixed raw scales (e.g. a wei-scaled Money and a FIXED_SCALAR-scaled
Money, even if their currency codes match).
§Panics
Panics if self.currency and rhs.currency differ by code (currency mismatch
is a type-system invariant violation, not a recoverable arithmetic condition).
Sourcepub fn checked_sub(self, rhs: Self) -> Option<Self>
pub fn checked_sub(self, rhs: Self) -> Option<Self>
Performs a checked subtraction, returning None on raw integer underflow, when
the result falls outside [MONEY_RAW_MIN, MONEY_RAW_MAX], or when the operands
have mixed raw scales (e.g. a wei-scaled Money and a FIXED_SCALAR-scaled
Money, even if their currency codes match).
§Panics
Panics if self.currency and rhs.currency differ by code (currency mismatch
is a type-system invariant violation, not a recoverable arithmetic condition).
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(
decimal: Decimal,
currency: Currency,
) -> CorrectnessResult<Self>
pub fn from_decimal( decimal: Decimal, currency: Currency, ) -> CorrectnessResult<Self>
Creates a new Money from a Decimal value with specified currency.
This method provides more reliable parsing by using Decimal arithmetic to avoid floating-point precision issues during conversion.
§Errors
Returns an error if:
- The decimal value cannot be converted to the raw representation.
- Overflow occurs during scaling.
Source§impl Money
impl Money
Sourcepub fn from_wei<U>(raw_wei: U, currency: Currency) -> Selfwhere
U: Into<U256>,
pub fn from_wei<U>(raw_wei: U, currency: Currency) -> Selfwhere
U: Into<U256>,
Creates a new Money instance from raw wei value with 18-decimal precision.
This method is specifically designed for DeFi applications where values are represented in wei (the smallest unit of Ether, 1 ETH = 10^18 wei).
§Panics
Panics if currency.precision is not 18, or if the raw wei value exceeds the
signed 128-bit range.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Money
impl<'de> Deserialize<'de> for Money
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<'py> IntoPyObject<'py> for Money
impl<'py> IntoPyObject<'py> for Money
Source§impl Ord for Money
impl Ord for Money
Source§impl PartialOrd for Money
impl PartialOrd for Money
Source§impl PyClassImpl for Money
impl PyClassImpl for Money
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 an amount of money in a specified currency denomination.
///
/// - [`MONEY_MAX`] - Maximum representable money amount
/// - [`MONEY_MIN`] - Minimum representable money amount
const RAW_DOC: &'static CStr = /// Represents an amount of money in a specified currency denomination. /// /// - [`MONEY_MAX`] - Maximum representable money amount /// - [`MONEY_MIN`] - Minimum representable money amount
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type Layout = <<Money as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<Money>
type Layout = <<Money as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<Money>
Source§type ThreadChecker = NoopThreadChecker
type ThreadChecker = NoopThreadChecker
type Inventory = Pyo3MethodsInventoryForMoney
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<PyObjectOffset>
fn dict_offset() -> Option<PyObjectOffset>
§fn weaklist_offset() -> Option<PyObjectOffset>
fn weaklist_offset() -> Option<PyObjectOffset>
Source§impl PyClassNewTextSignature for Money
impl PyClassNewTextSignature for Money
const TEXT_SIGNATURE: &'static str = "(amount, currency)"
Source§impl PyStubType for Money
impl PyStubType for Money
Source§fn type_output() -> TypeInfo
fn type_output() -> TypeInfo
§fn type_input() -> TypeInfo
fn type_input() -> TypeInfo
Source§impl PyTypeInfo for Money
impl PyTypeInfo for Money
Source§const NAME: &str = <Self as ::pyo3::PyClass>::NAME
const NAME: &str = <Self as ::pyo3::PyClass>::NAME
prefer using ::type_object(py).name() to get the correct runtime value
Source§const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
prefer using ::type_object(py).module() to get the correct runtime value
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.impl Copy for Money
impl DerefToPyAny for Money
impl Eq for Money
Auto Trait Implementations§
impl Freeze for Money
impl RefUnwindSafe for Money
impl Send for Money
impl Sync for Money
impl Unpin for Money
impl UnsafeUnpin for Money
impl UnwindSafe for Money
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,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
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 more§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§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.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self into an owned Python object, dropping type information and unbinding it
from the 'py lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self into a Python object. Read more§impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
§fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
§const NAME: &'static str = T::NAME
const NAME: &'static str = T::NAME
Use ::classinfo_object() instead and format the type name at runtime. Note that using built-in cast features is often better than manual PyTypeCheck usage.
§fn type_check(object: &Bound<'_, PyAny>) -> bool
fn type_check(object: &Bound<'_, PyAny>) -> bool
§fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
isinstance and issubclass function. Read more