Skip to content

Commit

Permalink
chore(codecs): contain [Struct]Flags in a mod and import what's nec…
Browse files Browse the repository at this point in the history
…essary (paradigmxyz#329)

* contain StructFlags in a mod with its imports

* update code generation test
  • Loading branch information
joshieDo authored and gakonst committed Dec 7, 2022
1 parent f65969e commit 13df80c
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 50 deletions.
46 changes: 30 additions & 16 deletions crates/codecs/derive/src/compact/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ pub(crate) fn generate_flag_struct(ident: &Ident, fields: &FieldList) -> TokenSt
let is_enum = fields.iter().any(|field| matches!(field, FieldTypes::EnumVariant(_)));

let flags_ident = format_ident!("{ident}Flags");
let mod_flags_ident = format_ident!("{ident}_flags");

let mut field_flags = vec![];

let total_bits = if is_enum {
field_flags.push(quote! {
variant: B8,
pub variant: B8,
});
8
} else {
Expand Down Expand Up @@ -44,17 +46,26 @@ pub(crate) fn generate_flag_struct(ident: &Ident, fields: &FieldList) -> TokenSt

// Generate the flag struct.
quote! {
#[bitfield]
#[derive(Clone, Copy, Debug, Default)]
struct #flags_ident {
#(#field_flags)*
}

impl #flags_ident {
fn from(mut buf: &[u8]) -> (Self, &[u8]) {
(#flags_ident::from_bytes([
#(#readable_bytes)*
]), buf)
pub use #mod_flags_ident::#flags_ident;
mod #mod_flags_ident {
use bytes::Buf;
use modular_bitfield::prelude::*;

/// Fieldset that facilitates compacting the parent type.
#[bitfield]
#[derive(Clone, Copy, Debug, Default)]
pub struct #flags_ident {
#(#field_flags)*
}

impl #flags_ident {
/// Deserializes this fieldset and returns it, alongside the original slice in an advanced position.
pub fn from(mut buf: &[u8]) -> (Self, &[u8]) {
(#flags_ident::from_bytes([
#(#readable_bytes)*
]), buf)
}
}
}
}
Expand Down Expand Up @@ -82,13 +93,13 @@ fn build_struct_field_flags(
total_bits += bitsize;

field_flags.push(quote! {
#name: #bsize ,
pub #name: #bsize ,
});
} else {
let name = format_ident!("{name}");

field_flags.push(quote! {
#name: bool ,
pub #name: bool ,
});

total_bits += 1;
Expand Down Expand Up @@ -120,15 +131,18 @@ fn pad_flag_struct(total_bits: u8, field_flags: &mut Vec<TokenStream2>) -> u8 {
/// Placeholder struct for when there are no bitfields to be added.
fn placeholder_flag_struct(flags: &Ident) -> TokenStream2 {
quote! {
/// Placeholder struct for when there is no need for a fieldset. Doesn't actually write or read any data.
#[derive(Debug, Default)]
struct #flags {
pub struct #flags {
}

impl #flags {
fn from(mut buf: &[u8]) -> (Self, &[u8]) {
/// Placeholder: does not read any value.
pub fn from(mut buf: &[u8]) -> (Self, &[u8]) {
(#flags::default(), buf)
}
fn into_bytes(self) -> [u8; 0] {
/// Placeholder: returns an empty array.
pub fn into_bytes(self) -> [u8; 0] {
[]
}
}
Expand Down
46 changes: 27 additions & 19 deletions crates/codecs/derive/src/compact/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,25 +205,33 @@ mod tests {

// Expected output in a TokenStream format. Commas matter!
let should_output = quote! {
#[bitfield]
#[derive(Clone, Copy, Debug, Default)]
struct TestStructFlags {
f_u64_len: B4,
f_u256_len: B6,
f_bool_t_len: B1,
f_bool_f_len: B1,
f_option_none_len: B1,
f_option_some_len: B1,
f_option_some_u64_len: B1,
#[skip]
unused: B1,
}
impl TestStructFlags {
fn from(mut buf: &[u8]) -> (Self, &[u8]) {
(
TestStructFlags::from_bytes([buf.get_u8(), buf.get_u8(),]),
buf
)
pub use TestStruct_flags::TestStructFlags;
mod TestStruct_flags {
use bytes::Buf;
use modular_bitfield::prelude::*;

#[doc=r" Fieldset that facilitates compacting the parent type."]
#[bitfield]
#[derive(Clone, Copy, Debug, Default)]
pub struct TestStructFlags {
pub f_u64_len: B4,
pub f_u256_len: B6,
pub f_bool_t_len: B1,
pub f_bool_f_len: B1,
pub f_option_none_len: B1,
pub f_option_some_len: B1,
pub f_option_some_u64_len: B1,
#[skip]
unused: B1,
}
impl TestStructFlags {
#[doc=r" Deserializes this fieldset and returns it, alongside the original slice in an advanced position."]
pub fn from(mut buf: &[u8]) -> (Self, &[u8]) {
(
TestStructFlags::from_bytes([buf.get_u8(), buf.get_u8(),]),
buf
)
}
}
}
#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion crates/codecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ impl Compact for bool {
mod tests {
use super::*;
use ethers_core::types::Address;
use modular_bitfield::prelude::*;

#[test]
fn compact_bytes() {
Expand Down
2 changes: 0 additions & 2 deletions crates/interfaces/src/db/codecs/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use crate::db::{
models::{accounts::AccountBeforeTx, StoredBlockBody},
Compress, Decompress, Error,
};
use bytes::Buf;
use modular_bitfield::prelude::*;
use reth_codecs::{main_codec, Compact};
use reth_primitives::*;

Expand Down
3 changes: 1 addition & 2 deletions crates/interfaces/src/db/models/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use crate::{
},
impl_fixed_arbitrary,
};
use bytes::{Buf, Bytes};
use modular_bitfield::prelude::*;
use bytes::Bytes;
use reth_codecs::{main_codec, Compact};
use reth_primitives::{BlockHash, BlockNumber, Header, TxNumber, H256};
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 0 additions & 2 deletions crates/primitives/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{H256, U256};
use bytes::Buf;
use modular_bitfield::prelude::*;
use reth_codecs::{main_codec, Compact};

/// Account saved in database
Expand Down
3 changes: 1 addition & 2 deletions crates/primitives/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use crate::{
proofs::{EMPTY_LIST_HASH, EMPTY_ROOT},
BlockHash, BlockNumber, Bloom, H160, H256, U256,
};
use bytes::{Buf, BufMut, BytesMut};
use bytes::{BufMut, BytesMut};
use ethers_core::{types::H64, utils::keccak256};
use modular_bitfield::prelude::*;
use reth_codecs::{main_codec, Compact};
use reth_rlp::{length_of_length, Decodable, Encodable};
use std::ops::Deref;
Expand Down
1 change: 0 additions & 1 deletion crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{Bloom, Log, TxType};
use bytes::{Buf, BufMut, BytesMut};
use modular_bitfield::prelude::*;
use reth_codecs::{main_codec, Compact};
use reth_rlp::{length_of_length, Decodable, Encodable};
use std::cmp::Ordering;
Expand Down
2 changes: 0 additions & 2 deletions crates/primitives/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::{H256, U256};
use bytes::Buf;
use modular_bitfield::prelude::*;
use reth_codecs::{main_codec, Compact};

/// Account storage entry.
Expand Down
1 change: 0 additions & 1 deletion crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub use access_list::{AccessList, AccessListItem};
use bytes::{Buf, BytesMut};
use derive_more::{AsRef, Deref};
use ethers_core::utils::keccak256;
use modular_bitfield::prelude::*;
use reth_codecs::{main_codec, Compact};
use reth_rlp::{length_of_length, Decodable, DecodeError, Encodable, Header, EMPTY_STRING_CODE};
pub use signature::Signature;
Expand Down
2 changes: 0 additions & 2 deletions crates/primitives/src/transaction/signature.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{transaction::util::secp256k1, Address, H256, U256};
use bytes::Buf;
use modular_bitfield::prelude::*;
use reth_codecs::{main_codec, Compact};
use reth_rlp::{Decodable, DecodeError, Encodable};

Expand Down

0 comments on commit 13df80c

Please sign in to comment.