Skip to content

Commit 3343e85

Browse files
committed
Add support for rand 0.9
1 parent afb2574 commit 3343e85

File tree

11 files changed

+306
-35
lines changed

11 files changed

+306
-35
lines changed

Cargo.lock

Lines changed: 23 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ num_threads = "0.1.2"
1616
powerfmt = { version = "0.2.0", default-features = false }
1717
quickcheck = { version = "1.0.3", default-features = false }
1818
quickcheck_macros = "1.0.0"
19-
rand = { version = "0.8.4", default-features = false }
19+
rand08 = { package = "rand", version = "0.8.4", default-features = false }
20+
rand09 = { package = "rand", version = "0.9.2", default-features = false }
2021
rstest = { version = "0.23.0", default-features = false }
2122
rstest_reuse = "0.7.0"
2223
# ^1.0.184 due to serde-rs/serde#2538

benchmarks/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ mods![
100100
mod offset_date_time;
101101
mod parsing;
102102
mod primitive_date_time;
103-
mod rand;
103+
mod rand08;
104+
mod rand09;
104105
mod time;
105106
mod utc_offset;
106107
mod util;

benchmarks/rand.rs renamed to benchmarks/rand08.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use criterion::Bencher;
2-
use rand::rngs::mock::StepRng;
3-
use rand::Rng;
2+
use rand08::rngs::mock::StepRng;
3+
use rand08::Rng;
44
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday};
55

66
macro_rules! bench_rand {

benchmarks/rand09.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use criterion::Bencher;
2+
use rand09::Rng;
3+
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday};
4+
5+
macro_rules! bench_rand {
6+
($($name:ident : $type:ty),* $(,)?) => {
7+
setup_benchmark! {
8+
"Random",
9+
$(fn $name(ben: &mut Bencher<'_>) {
10+
iter_batched_ref!(
11+
ben,
12+
|| StepRng::new(0, 1),
13+
[|rng| rng.random::<$type>()]
14+
);
15+
})*
16+
}
17+
}
18+
}
19+
20+
bench_rand![
21+
time: Time,
22+
date: Date,
23+
utc_offset: UtcOffset,
24+
primitive_date_time: PrimitiveDateTime,
25+
offset_date_time: OffsetDateTime,
26+
duration: Duration,
27+
weekday: Weekday,
28+
month: Month,
29+
];
30+
31+
// copy of `StepRng` from rand 0.8 to avoid deprecation warnings
32+
#[derive(Debug, Clone)]
33+
struct StepRng {
34+
v: u64,
35+
a: u64,
36+
}
37+
38+
impl StepRng {
39+
const fn new(initial: u64, increment: u64) -> Self {
40+
Self {
41+
v: initial,
42+
a: increment,
43+
}
44+
}
45+
}
46+
47+
impl rand09::RngCore for StepRng {
48+
fn next_u32(&mut self) -> u32 {
49+
self.next_u64() as u32
50+
}
51+
52+
fn next_u64(&mut self) -> u64 {
53+
let res = self.v;
54+
self.v = self.v.wrapping_add(self.a);
55+
res
56+
}
57+
58+
fn fill_bytes(&mut self, dst: &mut [u8]) {
59+
rand09::rand_core::impls::fill_bytes_via_next(self, dst)
60+
}
61+
}

tests/meta.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use std::panic::{RefUnwindSafe, UnwindSafe};
1111
use std::time::{Duration as StdDuration, Instant as StdInstant, SystemTime};
1212

1313
use quickcheck::Arbitrary;
14-
use rand::distributions::{Distribution, Standard};
14+
use rand08::distributions::{Distribution as DistributionRand08, Standard as StandardRand08};
15+
use rand09::distr::{Distribution as DistributionRand09, StandardUniform as StandardUniformRand09};
1516
use serde::{Deserialize, Serialize};
1617
use time::format_description::well_known::iso8601;
1718
use time::format_description::{modifier, well_known, BorrowedFormatItem, Component};
@@ -1059,15 +1060,27 @@ assert_impl! { modifier::YearRepr:
10591060
Unpin,
10601061
UnwindSafe,
10611062
}
1062-
assert_impl! { Standard:
1063-
Distribution<Date>,
1064-
Distribution<Duration>,
1065-
Distribution<OffsetDateTime>,
1066-
Distribution<PrimitiveDateTime>,
1067-
Distribution<Time>,
1068-
Distribution<UtcOffset>,
1069-
Distribution<Month>,
1070-
Distribution<Weekday>,
1063+
assert_impl! { StandardRand08:
1064+
DistributionRand08<Date>,
1065+
DistributionRand08<Duration>,
1066+
DistributionRand08<OffsetDateTime>,
1067+
DistributionRand08<UtcDateTime>,
1068+
DistributionRand08<PrimitiveDateTime>,
1069+
DistributionRand08<Time>,
1070+
DistributionRand08<UtcOffset>,
1071+
DistributionRand08<Month>,
1072+
DistributionRand08<Weekday>,
1073+
}
1074+
assert_impl! { StandardUniformRand09:
1075+
DistributionRand09<Date>,
1076+
DistributionRand09<Duration>,
1077+
DistributionRand09<OffsetDateTime>,
1078+
DistributionRand09<UtcDateTime>,
1079+
DistributionRand09<PrimitiveDateTime>,
1080+
DistributionRand09<Time>,
1081+
DistributionRand09<UtcOffset>,
1082+
DistributionRand09<Month>,
1083+
DistributionRand09<Weekday>,
10711084
}
10721085
assert_impl! { StdDuration:
10731086
Add<Duration, Output = Duration>,

tests/rand.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use rand::Rng;
1+
use rand08::Rng as _;
2+
use rand09::Rng as _;
23
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday};
34

45
#[test]
5-
fn support() {
6+
fn support08() {
67
// Work around rust-random/rand#1020.
7-
let mut rng = rand::rngs::mock::StepRng::new(0, 656_175_560);
8+
let mut rng = rand08::rngs::mock::StepRng::new(0, 656_175_560);
89

910
for _ in 0..7 {
1011
let _ = rng.r#gen::<Weekday>();
@@ -19,3 +20,54 @@ fn support() {
1920
let _ = rng.r#gen::<OffsetDateTime>();
2021
let _ = rng.r#gen::<Duration>();
2122
}
23+
24+
#[test]
25+
fn support09() {
26+
// Work around rust-random/rand#1020.
27+
let mut rng = StepRng::new(0, 656_175_560);
28+
29+
for _ in 0..7 {
30+
let _ = rng.random::<Weekday>();
31+
}
32+
for _ in 0..12 {
33+
let _ = rng.random::<Month>();
34+
}
35+
let _ = rng.random::<Time>();
36+
let _ = rng.random::<Date>();
37+
let _ = rng.random::<UtcOffset>();
38+
let _ = rng.random::<PrimitiveDateTime>();
39+
let _ = rng.random::<OffsetDateTime>();
40+
let _ = rng.random::<Duration>();
41+
}
42+
43+
// copy of `StepRng` from rand 0.8 to avoid deprecation warnings
44+
#[derive(Debug, Clone)]
45+
struct StepRng {
46+
v: u64,
47+
a: u64,
48+
}
49+
50+
impl StepRng {
51+
fn new(initial: u64, increment: u64) -> Self {
52+
Self {
53+
v: initial,
54+
a: increment,
55+
}
56+
}
57+
}
58+
59+
impl rand09::RngCore for StepRng {
60+
fn next_u32(&mut self) -> u32 {
61+
self.next_u64() as u32
62+
}
63+
64+
fn next_u64(&mut self) -> u64 {
65+
let res = self.v;
66+
self.v = self.v.wrapping_add(self.a);
67+
res
68+
}
69+
70+
fn fill_bytes(&mut self, dst: &mut [u8]) {
71+
rand09::rand_core::impls::fill_bytes_via_next(self, dst)
72+
}
73+
}

time/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ local-offset = ["std", "dep:libc", "dep:num_threads"]
3838
macros = ["dep:time-macros"]
3939
parsing = ["time-macros?/parsing"]
4040
quickcheck = ["dep:quickcheck", "alloc", "deranged/quickcheck"]
41-
rand = ["dep:rand", "deranged/rand08"]
41+
rand = ["rand08", "rand09"]
42+
rand08 = ["dep:rand08", "deranged/rand08"]
43+
rand09 = ["dep:rand09", "deranged/rand09"]
4244
serde = ["dep:serde", "time-macros?/serde", "deranged/serde"]
4345
serde-human-readable = ["serde", "formatting", "parsing"]
4446
# Deprecated in favor of using the relevant flags directly.
@@ -53,7 +55,8 @@ deranged = { workspace = true }
5355
num-conv = { workspace = true }
5456
powerfmt = { workspace = true }
5557
quickcheck = { workspace = true, optional = true }
56-
rand = { workspace = true, optional = true }
58+
rand08 = { workspace = true, optional = true }
59+
rand09 = { workspace = true, optional = true }
5760
serde = { workspace = true, optional = true }
5861
time-core = { workspace = true }
5962
time-macros = { workspace = true, optional = true }
@@ -67,7 +70,8 @@ js-sys = { workspace = true, optional = true }
6770

6871
[dev-dependencies]
6972
num-conv = { workspace = true }
70-
rand = { workspace = true }
73+
rand08 = { workspace = true }
74+
rand09 = { workspace = true }
7175
serde = { workspace = true, features = ["derive"] }
7276
serde_json = { workspace = true }
7377
serde_test = { workspace = true }

time/src/lib.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,41 @@
4141
//!
4242
//! - `serde`
4343
//!
44-
//! Enables [serde](https://docs.rs/serde) support for all types.
44+
//! Enables [`serde`](https://docs.rs/serde) support for all types.
4545
//!
4646
//! - `serde-human-readable` (_implicitly enables `serde`, `formatting`, and `parsing`_)
4747
//!
48-
//! Allows serde representations to use a human-readable format. This is determined by the
48+
//! Allows `serde` representations to use a human-readable format. This is determined by the
4949
//! serializer, not the user. If this feature is not enabled or if the serializer requests a
5050
//! non-human-readable format, a format optimized for binary representation will be used.
5151
//!
5252
//! Libraries should never enable this feature, as the decision of what format to use should be up
5353
//! to the user.
5454
//!
55-
//! - `rand`
55+
//! - `rand` (_implicitly enables `rand08` and `rand09`_)
5656
//!
57-
//! Enables [rand](https://docs.rs/rand) support for all types.
57+
//! Previously, this would enable support for `rand` 0.8. Since the release of `rand` 0.9, the
58+
//! feature has been split into `rand08` and `rand09` to allow support for both versions. For
59+
//! backwards compatibility and simplicity, this feature enables support for _both_ series.
60+
//!
61+
//! It is strongly recommended to enable `rand08` or `rand09` directly, as enabling `rand` will
62+
//! needlessly pull in both versions.
63+
//!
64+
//! - `rand08`
65+
//!
66+
//! Enables [`rand` 0.8](https://docs.rs/rand/0.8) support for all types.
67+
//!
68+
//! - `rand09`
69+
//!
70+
//! Enables [`rand` 0.9](https://docs.rs/rand/0.9) support for all types.
5871
//!
5972
//! - `quickcheck` (_implicitly enables `alloc`_)
6073
//!
6174
//! Enables [quickcheck](https://docs.rs/quickcheck) support for all types.
6275
//!
6376
//! - `wasm-bindgen`
6477
//!
65-
//! Enables [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen) support for converting
78+
//! Enables [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) support for converting
6679
//! [JavaScript dates](https://rustwasm.github.io/wasm-bindgen/api/js_sys/struct.Date.html), as
6780
//! well as obtaining the UTC offset from JavaScript.
6881
@@ -102,8 +115,10 @@ pub mod parsing;
102115
mod primitive_date_time;
103116
#[cfg(feature = "quickcheck")]
104117
mod quickcheck;
105-
#[cfg(feature = "rand")]
106-
mod rand;
118+
#[cfg(feature = "rand08")]
119+
mod rand08;
120+
#[cfg(feature = "rand09")]
121+
mod rand09;
107122
#[cfg(feature = "serde")]
108123
pub mod serde;
109124
mod sys;

time/src/rand.rs renamed to time/src/rand08.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Implementation of [`Distribution`] for various structs.
22
3-
use rand::distributions::{Distribution, Standard};
4-
use rand::Rng;
3+
use rand08::distributions::{Distribution, Standard};
4+
use rand08::Rng;
55

66
use crate::{
77
Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcDateTime, UtcOffset, Weekday,

0 commit comments

Comments
 (0)