Skip to content

Commit

Permalink
fix: restructure Python bindings to avoid macro issues
Browse files Browse the repository at this point in the history
- Changed PyO3 macro structure
- Added multiple-pymethods feature
- Made struct fields explicit
- Used getter attributes for string representation
- Fixed non-local impl definition errors

This fixes the PyO3 macro issues by using a different macro structure
and adding necessary features for multiple Python method implementations.
  • Loading branch information
dirvine committed Nov 29, 2024
1 parent b131a0f commit 9ceb1d4
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct PyDataMap {
pub(crate) inner: crate::DataMap
}

#[pymethods]
#[pyclass]
impl PyDataMap {
#[new]
pub fn new() -> Self {
Expand All @@ -22,6 +22,7 @@ impl PyDataMap {
}
}

#[getter]
pub fn __str__(&self) -> PyResult<String> {
Ok(format!("{:?}", self.inner))
}
Expand All @@ -35,9 +36,11 @@ impl From<crate::DataMap> for PyDataMap {

#[pyclass]
#[derive(Clone)]
pub struct PyXorName(crate::XorName);
pub struct PyXorName {
pub(crate) inner: crate::XorName
}

#[pymethods]
#[pyclass]
impl PyXorName {
#[new]
pub fn new(bytes: &[u8]) -> PyResult<Self> {
Expand All @@ -48,17 +51,18 @@ impl PyXorName {
}
let mut array = [0u8; 32];
array.copy_from_slice(bytes);
Ok(PyXorName(crate::XorName(array)))
Ok(PyXorName { inner: crate::XorName(array) })
}

#[getter]
pub fn __str__(&self) -> PyResult<String> {
Ok(hex::encode(self.0 .0))
Ok(hex::encode(self.inner.0))
}
}

impl From<crate::XorName> for PyXorName {
fn from(inner: crate::XorName) -> Self {
PyXorName(inner)
PyXorName { inner }
}
}

Expand Down

0 comments on commit 9ceb1d4

Please sign in to comment.