Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use derive for IntoPyObject on newtypes #897

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading