Skip to content

Commit

Permalink
feat: derive Allocative on FixedBytes (alloy-rs#531)
Browse files Browse the repository at this point in the history
* use allocative

* ci: don't run --all-features on MSRV

* feat: add allocative to address

---------

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
  • Loading branch information
Rjected and DaniPopes authored Feb 27, 2024
1 parent 134236b commit 1a2bdae
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: ["stable", "beta", "nightly", "1.65"] # MSRV
rust: ["stable", "beta", "nightly"]
flags: ["--no-default-features", "", "--all-features"]
include:
- rust: "1.65" # MSRV
flags: "--no-default-features"
- rust: "1.65" # MSRV
flags: ""
- rust: "1.65" # MSRV
flags: "--features json"
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ ruint = { version = "1.11.1", default-features = false, features = ["alloc"] }
ruint-macro = { version = "1", default-features = false }
winnow = { version = "0.6", default-features = false, features = ["alloc"] }
postgres-types = "0.2.6"
allocative = { version = "0.3.2", default-features = false }
4 changes: 4 additions & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ derive_arbitrary = { workspace = true, optional = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }

# allocative
allocative = { workspace = true, optional = true }

# postgres
postgres-types = { workspace = true, optional = true }

Expand Down Expand Up @@ -97,6 +100,7 @@ arbitrary = [
"ethereum_ssz?/arbitrary",
]
k256 = ["dep:k256"]
allocative = ["dep:allocative"]

# `const-hex` compatibility feature for `hex`.
# Should not be needed most of the time.
Expand Down
1 change: 1 addition & 0 deletions crates/primitives/src/bits/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use derive_more::{Deref, DerefMut, From, Index, IndexMut, IntoIterator};
IntoIterator,
)]
#[cfg_attr(feature = "arbitrary", derive(derive_arbitrary::Arbitrary, proptest_derive::Arbitrary))]
#[cfg_attr(feature = "allocative", derive(allocative::Allocative))]
#[repr(transparent)]
pub struct FixedBytes<const N: usize>(#[into_iterator(owned, ref, ref_mut)] pub [u8; N]);

Expand Down
22 changes: 22 additions & 0 deletions crates/primitives/src/bits/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ macro_rules! wrap_fixed_bytes {
$crate::impl_fb_traits!($name, $n);
$crate::impl_rlp!($name, $n);
$crate::impl_serde!($name);
$crate::impl_allocative!($name);
$crate::impl_arbitrary!($name, $n);
$crate::impl_ssz_fixed_len!($name, $n);
$crate::impl_rand!($name);
Expand Down Expand Up @@ -558,6 +559,27 @@ macro_rules! impl_rlp {
($t:ty, $n:literal) => {};
}

#[doc(hidden)]
#[macro_export]
#[cfg(feature = "allocative")]
macro_rules! impl_allocative {
($t:ty) => {
impl $crate::private::allocative::Allocative for $t {
#[inline]
fn visit<'a, 'b: 'a>(&self, visitor: &'a mut $crate::private::allocative::Visitor<'b>) {
$crate::private::allocative::Allocative::visit(&self.0, visitor)
}
}
};
}

#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "allocative"))]
macro_rules! impl_allocative {
($t:ty) => {};
}

#[doc(hidden)]
#[macro_export]
#[cfg(feature = "serde")]
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ pub mod private {
#[cfg(feature = "rlp")]
pub use alloy_rlp;

#[cfg(feature = "allocative")]
pub use allocative;

#[cfg(feature = "ssz")]
pub use ssz;

Expand Down

0 comments on commit 1a2bdae

Please sign in to comment.