Skip to content

Commit 71c53be

Browse files
authored
Require SeedableRng::Seed to impl Clone + AsRef (#1491)
1 parent 9e030aa commit 71c53be

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
2121
- Rename `rand::distributions` to `rand::distr` (#1470)
2222
- The `serde1` feature has been renamed `serde` (#1477)
2323
- Mark `WeightError`, `PoissonError`, `BinomialError` as `#[non_exhaustive]` (#1480).
24+
- Require `Clone` and `AsRef` bound for `SeedableRng::Seed`. (#1491)
2425

2526
## [0.9.0-alpha.1] - 2024-03-18
2627
- Add the `Slice::num_choices` method to the Slice distribution (#1402)

rand_core/src/lib.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,15 @@ pub trait SeedableRng: Sized {
279279
///
280280
/// # Implementing `SeedableRng` for RNGs with large seeds
281281
///
282-
/// Note that the required traits `core::default::Default` and
283-
/// `core::convert::AsMut<u8>` are not implemented for large arrays
284-
/// `[u8; N]` with `N` > 32. To be able to implement the traits required by
285-
/// `SeedableRng` for RNGs with such large seeds, the newtype pattern can be
286-
/// used:
282+
/// Note that [`Default`] is not implemented for large arrays `[u8; N]` with
283+
/// `N` > 32. To be able to implement the traits required by `SeedableRng`
284+
/// for RNGs with such large seeds, the newtype pattern can be used:
287285
///
288286
/// ```
289287
/// use rand_core::SeedableRng;
290288
///
291289
/// const N: usize = 64;
290+
/// #[derive(Clone)]
292291
/// pub struct MyRngSeed(pub [u8; N]);
293292
/// # #[allow(dead_code)]
294293
/// pub struct MyRng(MyRngSeed);
@@ -299,6 +298,12 @@ pub trait SeedableRng: Sized {
299298
/// }
300299
/// }
301300
///
301+
/// impl AsRef<[u8]> for MyRngSeed {
302+
/// fn as_ref(&self) -> &[u8] {
303+
/// &self.0
304+
/// }
305+
/// }
306+
///
302307
/// impl AsMut<[u8]> for MyRngSeed {
303308
/// fn as_mut(&mut self) -> &mut [u8] {
304309
/// &mut self.0
@@ -313,7 +318,7 @@ pub trait SeedableRng: Sized {
313318
/// }
314319
/// }
315320
/// ```
316-
type Seed: Sized + Default + AsMut<[u8]>;
321+
type Seed: Clone + Default + AsRef<[u8]> + AsMut<[u8]>;
317322

318323
/// Create a new PRNG using the given seed.
319324
///

0 commit comments

Comments
 (0)