Skip to content

Commit

Permalink
Require SeedableRng::Seed to impl Clone + AsRef (#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey authored Sep 9, 2024
1 parent 9e030aa commit 71c53be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
- Rename `rand::distributions` to `rand::distr` (#1470)
- The `serde1` feature has been renamed `serde` (#1477)
- Mark `WeightError`, `PoissonError`, `BinomialError` as `#[non_exhaustive]` (#1480).
- Require `Clone` and `AsRef` bound for `SeedableRng::Seed`. (#1491)

## [0.9.0-alpha.1] - 2024-03-18
- Add the `Slice::num_choices` method to the Slice distribution (#1402)
Expand Down
17 changes: 11 additions & 6 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,15 @@ pub trait SeedableRng: Sized {
///
/// # Implementing `SeedableRng` for RNGs with large seeds
///
/// Note that the required traits `core::default::Default` and
/// `core::convert::AsMut<u8>` are not implemented for large arrays
/// `[u8; N]` with `N` > 32. To be able to implement the traits required by
/// `SeedableRng` for RNGs with such large seeds, the newtype pattern can be
/// used:
/// Note that [`Default`] is not implemented for large arrays `[u8; N]` with
/// `N` > 32. To be able to implement the traits required by `SeedableRng`
/// for RNGs with such large seeds, the newtype pattern can be used:
///
/// ```
/// use rand_core::SeedableRng;
///
/// const N: usize = 64;
/// #[derive(Clone)]
/// pub struct MyRngSeed(pub [u8; N]);
/// # #[allow(dead_code)]
/// pub struct MyRng(MyRngSeed);
Expand All @@ -299,6 +298,12 @@ pub trait SeedableRng: Sized {
/// }
/// }
///
/// impl AsRef<[u8]> for MyRngSeed {
/// fn as_ref(&self) -> &[u8] {
/// &self.0
/// }
/// }
///
/// impl AsMut<[u8]> for MyRngSeed {
/// fn as_mut(&mut self) -> &mut [u8] {
/// &mut self.0
Expand All @@ -313,7 +318,7 @@ pub trait SeedableRng: Sized {
/// }
/// }
/// ```
type Seed: Sized + Default + AsMut<[u8]>;
type Seed: Clone + Default + AsRef<[u8]> + AsMut<[u8]>;

/// Create a new PRNG using the given seed.
///
Expand Down

0 comments on commit 71c53be

Please sign in to comment.