From 796519b9d94214d150a397d668b4b0d350c4a0da Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 10 Feb 2025 17:23:56 +0100 Subject: [PATCH] chore(deps)!: bump getrandom to 0.3, rand to 0.9 --- Cargo.toml | 4 ++-- crates/dyn-abi/Cargo.toml | 2 +- crates/dyn-abi/benches/types.rs | 8 ++++---- crates/primitives/Cargo.toml | 2 +- crates/primitives/src/bits/fixed.rs | 29 +++++++++++++++++++++++----- crates/primitives/src/bits/macros.rs | 23 ++++++++++++++++++++-- 6 files changed, 53 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c50d3ea17..62e9bcc47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ bincode = "1.3" bytes = { version = "1", default-features = false } criterion = "0.5" derive_arbitrary = "1.3" -getrandom = "0.2" +getrandom = "0.3" hex = { package = "const-hex", version = "1.14", default-features = false, features = [ "alloc", "core-error", @@ -95,7 +95,7 @@ postgres-types = "0.2.6" pretty_assertions = "1.4" proptest = "1" proptest-derive = "0.5" -rand = { version = "0.8", default-features = false, features = ["getrandom"] } +rand = { version = "0.9", default-features = false, features = ["os_rng"] } rayon = { version = "1.2", default-features = false } ruint = { version = "1.12.3", default-features = false, features = ["alloc"] } ruint-macro = { version = "1", default-features = false } diff --git a/crates/dyn-abi/Cargo.toml b/crates/dyn-abi/Cargo.toml index e9f4a7313..3560cbe4b 100644 --- a/crates/dyn-abi/Cargo.toml +++ b/crates/dyn-abi/Cargo.toml @@ -54,7 +54,7 @@ proptest = { workspace = true, optional = true } alloy-dyn-abi = { path = ".", version = ">=0", features = ["std"] } criterion.workspace = true ethabi = "18" -rand = { workspace = true, features = ["std", "std_rng"] } +rand = { workspace = true, features = ["thread_rng"] } serde_json.workspace = true [features] diff --git a/crates/dyn-abi/benches/types.rs b/crates/dyn-abi/benches/types.rs index 8f585e362..b1b38a267 100644 --- a/crates/dyn-abi/benches/types.rs +++ b/crates/dyn-abi/benches/types.rs @@ -5,7 +5,7 @@ use alloy_sol_type_parser::TypeSpecifier; use criterion::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, }; -use rand::seq::SliceRandom; +use rand::seq::IndexedRandom; use std::{hint::black_box, time::Duration}; const KEYWORDS: &[&str] = @@ -18,7 +18,7 @@ const COMPLEX: &[&str] = &[ fn parse(c: &mut Criterion) { let mut g = group(c, "parse"); - let rng = &mut rand::thread_rng(); + let rng = &mut rand::rng(); g.bench_function("keywords", |b| { b.iter(|| { @@ -38,7 +38,7 @@ fn parse(c: &mut Criterion) { fn resolve(c: &mut Criterion) { let mut g = group(c, "resolve"); - let rng = &mut rand::thread_rng(); + let rng = &mut rand::rng(); g.bench_function("keywords", |b| { let parsed_keywords = @@ -63,7 +63,7 @@ fn resolve(c: &mut Criterion) { fn format(c: &mut Criterion) { let mut g = group(c, "format"); - let rng = &mut rand::thread_rng(); + let rng = &mut rand::rng(); g.bench_function("keywords", |b| { let keyword_types = diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 9d457ebf5..37c15916a 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -113,7 +113,7 @@ std = [ "keccak-asm?/std", "proptest?/std", "rand?/std", - "rand?/std_rng", + "rand?/thread_rng", "rustc-hash?/std", "serde?/std", "sha3?/std", diff --git a/crates/primitives/src/bits/fixed.rs b/crates/primitives/src/bits/fixed.rs index 4b849bea5..0f44f6792 100644 --- a/crates/primitives/src/bits/fixed.rs +++ b/crates/primitives/src/bits/fixed.rs @@ -323,9 +323,7 @@ impl str::FromStr for FixedBytes { } #[cfg(feature = "rand")] -impl rand::distributions::Distribution> - for rand::distributions::Standard -{ +impl rand::distr::Distribution> for rand::distr::StandardUniform { #[inline] fn sample(&self, rng: &mut R) -> FixedBytes { FixedBytes::random_with(rng) @@ -407,6 +405,17 @@ impl FixedBytes { bytes } + /// Tries to create a new [`FixedBytes`] with the given random number generator. + #[cfg(feature = "rand")] + #[inline] + pub fn try_random_with(rng: &mut R) -> Result { + // SAFETY: `bytes` is only accessible after random initialization. + #[allow(clippy::uninit_assumed_init)] + let mut bytes = unsafe { core::mem::MaybeUninit::::uninit().assume_init() }; + bytes.try_randomize_with(rng)?; + Ok(bytes) + } + /// Fills this [`FixedBytes`] with the default cryptographic random number generator. /// /// See [`random`](Self::random) for more details. @@ -426,12 +435,12 @@ impl FixedBytes { pub fn try_randomize(&mut self) -> Result<(), getrandom::Error> { #[cfg(all(feature = "rand", feature = "std"))] { - self.randomize_with(&mut rand::thread_rng()); + self.randomize_with(&mut rand::rng()); Ok(()) } #[cfg(not(all(feature = "rand", feature = "std")))] { - getrandom::getrandom(&mut self.0) + getrandom::fill(&mut self.0) } } @@ -443,6 +452,16 @@ impl FixedBytes { rng.fill_bytes(&mut self.0); } + /// Tries to fill this [`FixedBytes`] with the given random number generator. + #[inline] + #[cfg(feature = "rand")] + pub fn try_randomize_with( + &mut self, + rng: &mut R, + ) -> Result<(), R::Error> { + rng.try_fill_bytes(&mut self.0) + } + /// Concatenate two `FixedBytes`. /// /// Due to constraints in the language, the user must specify the value of diff --git a/crates/primitives/src/bits/macros.rs b/crates/primitives/src/bits/macros.rs index 086fb9db1..ec902f69c 100644 --- a/crates/primitives/src/bits/macros.rs +++ b/crates/primitives/src/bits/macros.rs @@ -492,6 +492,15 @@ macro_rules! impl_rand { Self($crate::FixedBytes::random_with(rng)) } + /// Tries to create a new fixed byte array with the given random number generator. + #[inline] + #[cfg_attr(docsrs, doc(cfg(feature = "rand")))] + pub fn try_random_with( + rng: &mut R, + ) -> $crate::private::Result { + $crate::FixedBytes::try_random_with(rng).map(Self) + } + /// Fills this fixed byte array with the given random number generator. #[inline] #[doc(alias = "randomize_using")] @@ -499,12 +508,22 @@ macro_rules! impl_rand { pub fn randomize_with(&mut self, rng: &mut R) { self.0.randomize_with(rng); } + + /// Tries to fill this fixed byte array with the given random number generator. + #[inline] + #[cfg_attr(docsrs, doc(cfg(feature = "rand")))] + pub fn try_randomize_with( + &mut self, + rng: &mut R, + ) -> $crate::private::Result<(), R::Error> { + self.0.try_randomize_with(rng) + } }; ($t:ty) => { #[cfg_attr(docsrs, doc(cfg(feature = "rand")))] - impl $crate::private::rand::distributions::Distribution<$t> - for $crate::private::rand::distributions::Standard + impl $crate::private::rand::distr::Distribution<$t> + for $crate::private::rand::distr::StandardUniform { #[inline] fn sample(&self, rng: &mut R) -> $t {