Skip to content

Commit

Permalink
introduce streamable_struct macro, to make it more succinct to define…
Browse files Browse the repository at this point in the history
… new streamable types. The main fix is to make all members public
  • Loading branch information
arvidn committed Dec 15, 2022
1 parent 392da34 commit 9051216
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
21 changes: 10 additions & 11 deletions chia-protocol/src/coin.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
use crate::bytes::Bytes32;
use crate::chia_error;
use crate::streamable::Streamable;
use crate::streamable_struct;
use chia_streamable_macro::Streamable;
use sha2::{Digest, Sha256};
use std::convert::TryInto;

#[cfg(feature = "py-bindings")]
use crate::from_json_dict::FromJsonDict;
#[cfg(feature = "py-bindings")]
use crate::to_json_dict::ToJsonDict;
#[cfg(feature = "py-bindings")]
use chia_py_streamable_macro::PyStreamable;
use chia_streamable_macro::Streamable;

use crate::bytes::Bytes32;
use crate::chia_error;
use crate::streamable::Streamable;
#[cfg(feature = "py-bindings")]
use pyo3::prelude::*;
#[cfg(feature = "py-bindings")]
use pyo3::types::PyBytes;
use sha2::{Digest, Sha256};
use std::convert::TryInto;

#[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))]
#[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)]
pub struct Coin {
streamable_struct!(Coin {
parent_coin_info: Bytes32,
puzzle_hash: Bytes32,
amount: u64,
}
});

impl Coin {
pub fn coin_id(&self) -> [u8; 32] {
Expand Down
7 changes: 3 additions & 4 deletions chia-protocol/src/coin_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::chia_error;
use crate::coin::Coin;
use crate::streamable::Streamable;
use crate::streamable_struct;
use chia_streamable_macro::Streamable;

#[cfg(feature = "py-bindings")]
Expand All @@ -12,10 +13,8 @@ use chia_py_streamable_macro::PyStreamable;
#[cfg(feature = "py-bindings")]
use pyo3::prelude::*;

#[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))]
#[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)]
pub struct CoinState {
streamable_struct! (CoinState {
coin: Coin,
spent_height: Option<u32>,
created_height: Option<u32>,
}
});
10 changes: 6 additions & 4 deletions chia-protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#[cfg(feature = "py-bindings")]
pub mod from_json_dict;
#[cfg(feature = "py-bindings")]
pub mod to_json_dict;

pub mod bls;
pub mod bytes;
pub mod chia_error;
pub mod coin;
pub mod coin_state;
#[cfg(feature = "py-bindings")]
pub mod from_json_dict;
pub mod message_struct;
pub mod respond_to_ph_updates;
pub mod streamable;
#[cfg(feature = "py-bindings")]
pub mod to_json_dict;
10 changes: 10 additions & 0 deletions chia-protocol/src/message_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[macro_export]
macro_rules! streamable_struct {
($name:ident {$($field:ident: $t:ty $(,)? )*}) => {
#[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))]
#[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)]
pub struct $name {
$(pub $field: $t),*
}
}
}
7 changes: 3 additions & 4 deletions chia-protocol/src/respond_to_ph_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::bytes::Bytes32;
use crate::chia_error;
use crate::coin_state::CoinState;
use crate::streamable::Streamable;
use crate::streamable_struct;
use chia_streamable_macro::Streamable;

#[cfg(feature = "py-bindings")]
Expand All @@ -13,10 +14,8 @@ use chia_py_streamable_macro::PyStreamable;
#[cfg(feature = "py-bindings")]
use pyo3::prelude::*;

#[cfg_attr(feature = "py-bindings", pyclass(unsendable), derive(PyStreamable))]
#[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)]
pub struct RespondToPhUpdates {
streamable_struct! (RespondToPhUpdates {
puzzle_hashes: Vec<Bytes32>,
min_height: u32,
coin_states: Vec<CoinState>,
}
});
2 changes: 2 additions & 0 deletions wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def parse_rust_source(filename: str) -> List[Tuple[str, List[str]]]:
if not in_struct:
if line.startswith("pub struct ") and "{" in line:
in_struct = line.split("pub struct ")[1].split("{")[0].strip()
elif line.startswith("streamable_struct! (") and "{" in line:
in_struct = line.split("streamable_struct! (")[1].split("{")[0].strip()
elif line.startswith("pub struct ") and "(" in line and ");" in line:
name = line.split("pub struct ")[1].split("(")[0].strip()
rust_args = line.split("(")[1].split(");")[0]
Expand Down

0 comments on commit 9051216

Please sign in to comment.