Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Remove feature `os_rng`, structs `OsRng` and `OsError` and fns `from_os_rng`, `try_from_os_rng` ([#1674])
- Remove feature `std` ([#1674])
- Removed dependency `getrandom` ([#1674])
- Add `SeedableRng::fork` methods ([#17])
### Other
- Changed repository from [rust-random/rand] to [rust-random/core].

Expand All @@ -23,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#1668]: https://github.com/rust-random/rand/pull/1668
[#1669]: https://github.com/rust-random/rand/pull/1669
[#1674]: https://github.com/rust-random/rand/pull/1674
[#17]: https://github.com/rust-random/rand-core/pull/17

[rust-random/rand]: https://github.com/rust-random/rand
[rust-random/core]: https://github.com/rust-random/core
Expand Down
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,34 @@ pub trait SeedableRng: Sized {
rng.try_fill_bytes(seed.as_mut())?;
Ok(Self::from_seed(seed))
}

/// Fork this PRNG
///
/// This creates a new PRNG from the current one by initializing a new one and
/// seeding it from the current one.
///
/// This is useful when initializing a PRNG for a thread
fn fork(&mut self) -> Self
where
Self: RngCore,
{
Self::from_rng(self)
}

/// Fork this PRNG
///
/// This creates a new PRNG from the current one by initializing a new one and
/// seeding it from the current one.
///
/// This is useful when initializing a PRNG for a thread.
///
/// This is the failable equivalent to [`SeedableRng::fork`]
fn try_fork(&mut self) -> Result<Self, Self::Error>
where
Self: TryRngCore,
{
Self::try_from_rng(self)
}
}

#[cfg(test)]
Expand Down