Skip to content

Commit

Permalink
optimize conversions to native-python uint32 etc. primitive integer t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
arvidn committed Feb 5, 2024
1 parent 4d372a4 commit 8fc62fd
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions chia-traits/src/int.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
use pyo3::prelude::*;
use pyo3::types::{PyAny, PyBool, PyDict, PyList, PyString, PyTuple};
use pyo3::types::{PyAny, PyBool, PyList, PyString, PyTuple};
use pyo3::PyResult;

pub fn py_int<'a, T: pyo3::ToPyObject + std::fmt::Debug>(
py: pyo3::Python<'a>,
py_type: &str,
val: T,
) -> PyResult<&'a PyAny> {
let ctx: &'a PyDict = PyDict::new(py);
ctx.set_item("value", val.to_object(py))?;
py.run(
format!(
"from chia.util.ints import {py_type}\n\
ret = {py_type}(value)\n"
)
.as_str(),
None,
Some(ctx),
)?;
Ok(ctx.get_item("ret").unwrap())
}

/// A custom to-python conversion trait that turns primitive integer types into
/// the chia-blockchain fixed-width integer types (uint8, int8, etc.)
pub trait ChiaToPython {
Expand All @@ -31,7 +12,10 @@ macro_rules! primitive_int {
($t:ty, $name:expr) => {
impl ChiaToPython for $t {
fn to_python<'a>(&self, py: Python<'a>) -> PyResult<&'a PyAny> {
py_int(py, $name, &self)
// py_int(py, $name, self)
let int_module = PyModule::import(py, "chia.util.ints")?;
let ty = int_module.getattr($name)?;
ty.call1((self.into_py(py),))
}
}
};
Expand Down

0 comments on commit 8fc62fd

Please sign in to comment.