Skip to content

Commit

Permalink
use derive for IntoPyObject on newtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky committed Jan 23, 2025
1 parent dfe7af4 commit 2224d38
Showing 1 changed file with 15 additions and 36 deletions.
51 changes: 15 additions & 36 deletions crates/chia-datalayer/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,14 @@ use std::iter::zip;
use std::ops::Range;
use thiserror::Error;

#[cfg_attr(feature = "py-bindings", derive(FromPyObject), pyo3(transparent))]
#[cfg_attr(
feature = "py-bindings",
derive(FromPyObject, IntoPyObject),
pyo3(transparent)
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Streamable)]
pub struct TreeIndex(u32);

#[cfg(feature = "py-bindings")]
impl<'py> IntoPyObject<'py> for TreeIndex {
type Target = PyInt;
type Output = Bound<'py, Self::Target>;
type Error = std::convert::Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
self.0.into_pyobject(py)
}
}

impl std::fmt::Display for TreeIndex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Expand All @@ -45,41 +38,27 @@ type Hash = Bytes32;
/// Key and value ids are provided from outside of this code and are implemented as
/// the row id from sqlite which is a signed 8 byte integer. The actual key and
/// value data bytes will not be handled within this code, only outside.
#[cfg_attr(feature = "py-bindings", derive(FromPyObject), pyo3(transparent))]
#[cfg_attr(
feature = "py-bindings",
derive(FromPyObject, IntoPyObject),
pyo3(transparent)
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Streamable)]
pub struct KeyId(i64);
#[cfg_attr(feature = "py-bindings", derive(FromPyObject), pyo3(transparent))]
#[cfg_attr(
feature = "py-bindings",
derive(FromPyObject, IntoPyObject),
pyo3(transparent)
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Streamable)]
pub struct ValueId(i64);

#[cfg(feature = "py-bindings")]
impl<'py> IntoPyObject<'py> for KeyId {
type Target = PyInt;
type Output = Bound<'py, Self::Target>;
type Error = std::convert::Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
self.0.into_pyobject(py)
}
}

impl std::fmt::Display for ValueId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

#[cfg(feature = "py-bindings")]
impl<'py> IntoPyObject<'py> for ValueId {
type Target = PyInt;
type Output = Bound<'py, Self::Target>;
type Error = std::convert::Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
self.0.into_pyobject(py)
}
}

impl std::fmt::Display for KeyId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Expand Down

0 comments on commit 2224d38

Please sign in to comment.