Skip to content

Commit

Permalink
chore(deps)!: bump getrandom to 0.3, rand to 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Feb 10, 2025
1 parent e3071c8 commit 796519b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion crates/dyn-abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions crates/dyn-abi/benches/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand All @@ -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(|| {
Expand All @@ -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 =
Expand All @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 24 additions & 5 deletions crates/primitives/src/bits/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,7 @@ impl<const N: usize> str::FromStr for FixedBytes<N> {
}

#[cfg(feature = "rand")]
impl<const N: usize> rand::distributions::Distribution<FixedBytes<N>>
for rand::distributions::Standard
{
impl<const N: usize> rand::distr::Distribution<FixedBytes<N>> for rand::distr::StandardUniform {
#[inline]
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> FixedBytes<N> {
FixedBytes::random_with(rng)
Expand Down Expand Up @@ -407,6 +405,17 @@ impl<const N: usize> FixedBytes<N> {
bytes
}

/// Tries to create a new [`FixedBytes`] with the given random number generator.
#[cfg(feature = "rand")]
#[inline]
pub fn try_random_with<R: rand::TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
// SAFETY: `bytes` is only accessible after random initialization.
#[allow(clippy::uninit_assumed_init)]
let mut bytes = unsafe { core::mem::MaybeUninit::<Self>::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.
Expand All @@ -426,12 +435,12 @@ impl<const N: usize> FixedBytes<N> {
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)
}
}

Expand All @@ -443,6 +452,16 @@ impl<const N: usize> FixedBytes<N> {
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<R: rand::TryRngCore + ?Sized>(
&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
Expand Down
23 changes: 21 additions & 2 deletions crates/primitives/src/bits/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,19 +492,38 @@ 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<R: $crate::private::rand::TryRngCore + ?Sized>(
rng: &mut R,
) -> $crate::private::Result<Self, R::Error> {
$crate::FixedBytes::try_random_with(rng).map(Self)
}

/// Fills this fixed byte array with the given random number generator.
#[inline]
#[doc(alias = "randomize_using")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
pub fn randomize_with<R: $crate::private::rand::RngCore + ?Sized>(&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<R: $crate::private::rand::TryRngCore + ?Sized>(
&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<R: $crate::private::rand::Rng + ?Sized>(&self, rng: &mut R) -> $t {
Expand Down

0 comments on commit 796519b

Please sign in to comment.