Skip to content

Commit

Permalink
feat(primitives): implement map module (#743)
Browse files Browse the repository at this point in the history
* feat(primitives): implement `map` module

* chore: nits

* ci

* chore: fixes, clippy

* perf: override write_usize on stable

* chore: updates

* fix

* feat: docs, disable hashbrown by default

* ci: add more flags to cargo hack

* feat: remove S generic from Fb* types

Easier to change the implementation later on.

* fix: keep hashbrown by default for no_std

Ideally this is only a depedencny if `not(feature = "std")` but this is
currently not something you can have with cargo features.
  • Loading branch information
DaniPopes authored Sep 25, 2024
1 parent eb522be commit 0b94089
Show file tree
Hide file tree
Showing 19 changed files with 470 additions and 56 deletions.
25 changes: 16 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
os: ["ubuntu-latest", "windows-latest"]
rust: [
"stable",
"beta",
"nightly",
"1.79", # MSRV
]
Expand All @@ -43,12 +42,9 @@ jobs:
flags: "--features json"
# All features
- os: "ubuntu-latest"
rust: "stable"
flags: "--all-features"
- os: "ubuntu-latest"
rust: "beta"
rust: "nightly"
flags: "--all-features"
- os: "ubuntu-latest"
- os: "windows-latest"
rust: "nightly"
flags: "--all-features"
steps:
Expand Down Expand Up @@ -98,17 +94,28 @@ jobs:
run: cargo check --workspace --target wasm32-unknown-unknown

feature-checks:
name: features ${{ matrix.rust }} ${{ matrix.flags }}
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
rust: ["stable", "nightly"]
flags: ["", "--all-targets"]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- uses: taiki-e/install-action@cargo-hack
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- name: cargo hack
run: cargo hack check --feature-powerset --depth 2
run: |
args=(${{ matrix.flags }})
[ ${{ matrix.rust }} == "stable" ] && args+=(--skip nightly)
./scripts/check_features.sh "${args[@]}"
check-no-std:
name: check no_std ${{ matrix.features }}
Expand All @@ -130,7 +137,7 @@ jobs:
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
components: clippy
- uses: Swatinem/rust-cache@v2
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ paste = "1.0"
num_enum = "0.7"
thiserror = "1.0"

# crypto
digest = "0.10"
k256 = { version = "0.13", default-features = false }
keccak-asm = { version = "0.1.0", default-features = false }
tiny-keccak = { version = "2.0", default-features = false }
sha3 = { version = "0.10.8", default-features = false }

# maps
hashbrown = { version = "0.14", default-features = false }
indexmap = { version = "2.5", default-features = false }
rustc-hash = { version = "2.0", default-features = false }

# misc
allocative = { version = "0.3.2", default-features = false }
alloy-rlp = { version = "0.3", default-features = false }
Expand Down
36 changes: 26 additions & 10 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,58 @@ rustdoc-args = ["--cfg", "docsrs"]
workspace = true

[dependencies]
alloy-primitives = { workspace = true, default-features = false }
alloy-primitives.workspace = true

alloy-dyn-abi = { workspace = true, default-features = false, optional = true }
alloy-json-abi = { workspace = true, default-features = false, optional = true }
alloy-sol-types = { workspace = true, default-features = false, optional = true }
alloy-dyn-abi = { workspace = true, optional = true }
alloy-json-abi = { workspace = true, optional = true }
alloy-sol-types = { workspace = true, optional = true }

alloy-rlp = { workspace = true, default-features = false, optional = true }
alloy-rlp = { workspace = true, optional = true }

[features]
default = [
"std",
"alloy-primitives/default",
"alloy-dyn-abi?/default",
"alloy-json-abi?/default",
"alloy-sol-types?/default",
"alloy-rlp?/default",
]
std = [
"alloy-primitives/std",
"alloy-json-abi?/std",
"alloy-dyn-abi?/std",
"alloy-sol-types?/std",
"alloy-rlp?/std",
]
nightly = ["alloy-primitives/nightly"]

dyn-abi = ["sol-types", "dep:alloy-dyn-abi"]
json-abi = ["json", "serde", "dep:alloy-json-abi"]
json = ["alloy-sol-types?/json"]
sol-types = ["dep:alloy-sol-types"]

tiny-keccak = ["alloy-primitives/tiny-keccak"]
native-keccak = ["alloy-primitives/native-keccak"]
asm-keccak = ["alloy-primitives/asm-keccak"]
native-keccak = ["alloy-primitives/native-keccak"]
sha3-keccak = ["alloy-primitives/sha3-keccak"]
tiny-keccak = ["alloy-primitives/tiny-keccak"]

map = ["alloy-primitives/map"]
map-hashbrown = ["alloy-primitives/map-hashbrown"]
map-indexmap = ["alloy-primitives/map-indexmap"]
map-fxhash = ["alloy-primitives/map-fxhash"]

postgres = ["std", "alloy-primitives/postgres"]
getrandom = ["alloy-primitives/getrandom"]
rand = ["alloy-primitives/rand"]
rlp = ["alloy-primitives/rlp", "dep:alloy-rlp"]
serde = ["alloy-primitives/serde"]
k256 = ["alloy-primitives/k256"]
eip712 = ["alloy-sol-types?/eip712-serde", "alloy-dyn-abi?/eip712"]

postgres = ["std", "alloy-primitives/postgres"]
arbitrary = [
"std",
"alloy-primitives/arbitrary",
"alloy-sol-types?/arbitrary",
"alloy-dyn-abi?/arbitrary",
]
k256 = ["alloy-primitives/k256"]
eip712 = ["alloy-sol-types?/eip712-serde", "alloy-dyn-abi?/eip712"]
4 changes: 2 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use alloy_json_abi as json_abi;
#[cfg(feature = "sol-types")]
#[doc(inline)]
pub use alloy_sol_types as sol_types;
#[cfg(all(doc, feature = "sol-types"))] // Show this re-export in docs instead of the wrapper below.
#[cfg(all(feature = "sol-types", doc))] // Show this re-export in docs instead of the wrapper below.
#[doc(no_inline)]
pub use sol_types::sol;

Expand All @@ -34,7 +34,7 @@ pub use alloy_rlp as rlp;
/// [`sol!`](sol_types::sol!) `macro_rules!` wrapper to set import attributes.
///
/// See [`sol!`](sol_types::sol!) for the actual macro documentation.
#[cfg(all(not(doc), feature = "sol-types"))] // Show the actual macro in docs.
#[cfg(all(feature = "sol-types", not(doc)))] // Show the actual macro in docs.
#[macro_export]
macro_rules! sol {
($($t:tt)*) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/tests/sol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(feature = "sol-types")]
#![allow(missing_docs)]
#![cfg(feature = "sol-types")]

use alloy_core::sol;

Expand Down
4 changes: 2 additions & 2 deletions crates/dyn-abi/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use alloy_primitives::{Address, Function, B256, I256, U256};
use arbitrary::{size_hint, Unstructured};
use core::ops::RangeInclusive;
use proptest::{
collection::{hash_set as hash_set_strategy, vec as vec_strategy, VecStrategy},
collection::{vec as vec_strategy, VecStrategy},
prelude::*,
strategy::{Flatten, Map, Recursive, TupleUnion, WA},
};
Expand Down Expand Up @@ -240,7 +240,7 @@ macro_rules! custom_struct_strategy {
.prop_flat_map(move |sz| {
(
IDENT_STRATEGY,
hash_set_strategy(IDENT_STRATEGY, sz..=sz)
proptest::collection::hash_set(IDENT_STRATEGY, sz..=sz)
.prop_map(|prop_names| prop_names.into_iter().collect()),
vec_strategy(elem.clone(), sz..=sz),
)
Expand Down
7 changes: 5 additions & 2 deletions crates/json-abi/src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,11 @@ mod tests {
let param_value = serde_json::from_str::<serde_json::Value>(param).unwrap();
assert_eq!(serde_json::from_value::<Param>(param_value).unwrap(), expected);

let reader = std::io::Cursor::new(param);
assert_eq!(serde_json::from_reader::<_, Param>(reader).unwrap(), expected);
#[cfg(feature = "std")]
{
let reader = std::io::Cursor::new(param);
assert_eq!(serde_json::from_reader::<_, Param>(reader).unwrap(), expected);
}
}

#[test]
Expand Down
52 changes: 42 additions & 10 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ itoa.workspace = true
ruint.workspace = true

# macros
cfg-if.workspace = true
derive_more = { workspace = true, features = [
"as_ref",
"add",
Expand All @@ -43,7 +44,7 @@ derive_more = { workspace = true, features = [
"into_iterator",
"display",
] }
cfg-if.workspace = true
paste.workspace = true

# keccak256
keccak-asm = { workspace = true, optional = true }
Expand All @@ -65,6 +66,14 @@ rand = { workspace = true, optional = true, features = ["getrandom"] }
# k256
k256 = { workspace = true, optional = true, features = ["ecdsa"] }

# map
hashbrown = { workspace = true, optional = true, features = [
"ahash",
"inline-more",
] }
indexmap = { workspace = true, optional = true }
rustc-hash = { workspace = true, optional = true }

# arbitrary
arbitrary = { workspace = true, optional = true }
derive_arbitrary = { workspace = true, optional = true }
Expand All @@ -84,30 +93,53 @@ criterion.workspace = true
serde_json.workspace = true

[features]
default = ["std"]
default = ["std", "map-fxhash"]
std = [
"bytes/std",
"hex/std",
"ruint/std",
"alloy-rlp?/std",
"indexmap?/std",
"k256?/std",
"keccak-asm?/std",
"proptest?/std",
"rand?/std",
"rustc-hash?/std",
"serde?/std",
"k256?/std",
"sha3?/std",
]
nightly = [
"hex/nightly",
"ruint/nightly",
"hashbrown?/nightly",
"rustc-hash?/nightly",
]

tiny-keccak = []
native-keccak = []
asm-keccak = ["dep:keccak-asm"]
native-keccak = []
sha3-keccak = ["dep:sha3"]
tiny-keccak = []

map = ["dep:hashbrown"]
map-hashbrown = ["map"]
map-indexmap = ["map", "dep:indexmap"]
map-fxhash = ["map", "dep:rustc-hash"]

postgres = ["std", "dep:postgres-types", "ruint/postgres"]
getrandom = ["dep:getrandom"]
rand = ["dep:rand", "getrandom", "ruint/rand"]
k256 = ["dep:k256"]
rand = ["dep:rand", "getrandom", "ruint/rand", "rustc-hash?/rand"]
rlp = ["dep:alloy-rlp", "ruint/alloy-rlp"]
serde = ["dep:serde", "bytes/serde", "hex/serde", "ruint/serde"]
serde = [
"dep:serde",
"bytes/serde",
"hex/serde",
"ruint/serde",
"hashbrown?/serde",
"indexmap?/serde",
"rand?/serde",
]

allocative = ["dep:allocative"]
arbitrary = [
"std",
"dep:arbitrary",
Expand All @@ -116,9 +148,9 @@ arbitrary = [
"dep:proptest-derive",
"ruint/arbitrary",
"ruint/proptest",
"indexmap?/arbitrary",
]
k256 = ["dep:k256"]
allocative = ["dep:allocative"]
postgres = ["std", "dep:postgres-types", "ruint/postgres"]

# `const-hex` compatibility feature for `hex`.
# Should not be needed most of the time.
Expand Down
8 changes: 5 additions & 3 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "nightly", feature(hasher_prefixfree_extras))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[macro_use]
extern crate alloc;

use paste as _;
#[cfg(feature = "sha3-keccak")]
use sha3 as _;

use tiny_keccak as _;

#[cfg(feature = "postgres")]
Expand Down Expand Up @@ -42,8 +43,9 @@ pub use common::TxKind;

mod log;
pub use log::{IntoLogData, Log, LogData};
#[cfg(feature = "serde")]
mod log_serde;

#[cfg(feature = "map")]
pub mod map;

mod sealed;
pub use sealed::{Sealable, Sealed};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::{Address, Bytes, B256};
use alloc::vec::Vec;

#[cfg(feature = "serde")]
mod serde;

/// An Ethereum event log object.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(derive_arbitrary::Arbitrary, proptest_derive::Arbitrary))]
pub struct LogData {
/// The indexed topic list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ mod tests {
log::{Log, LogData},
Bytes,
};
use alloc::vec::Vec;

#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
struct TestStruct {
Expand Down
Loading

0 comments on commit 0b94089

Please sign in to comment.