pyo3::conversions

Module rust_decimal

Source
Available on crate feature rust_decimal only.
Expand description

Conversions to and from rust_decimal’s [Decimal] type.

This is useful for converting Python’s decimal.Decimal into and from a native Rust type.

§Setup

To use this feature, add to your Cargo.toml:

[dependencies]
pyo3 = { version = "0.23.1", features = ["rust_decimal"] }
rust_decimal = "1.0"

Note that you must use a compatible version of rust_decimal and PyO3. The required rust_decimal version may vary based on the version of PyO3.

§Example

Rust code to create a function that adds one to a Decimal

use rust_decimal::Decimal;
use pyo3::prelude::*;

#[pyfunction]
fn add_one(d: Decimal) -> Decimal {
    d + Decimal::ONE
}

#[pymodule]
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(add_one, m)?)?;
    Ok(())
}

Python code that validates the functionality

from my_module import add_one
from decimal import Decimal

d = Decimal("2")
value = add_one(d)

assert d + 1 == value

Statics§

Functions§

⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here