Skip to content

Commit 4e856da

Browse files
authored
Add SeedableRng::{fork, try_fork} methods (#17)
1 parent 255ff4c commit 4e856da

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Remove feature `os_rng`, structs `OsRng` and `OsError` and fns `from_os_rng`, `try_from_os_rng` ([#1674])
1414
- Remove feature `std` ([#1674])
1515
- Removed dependency `getrandom` ([#1674])
16+
- Add `SeedableRng::fork` methods ([#17])
1617
### Other
1718
- Changed repository from [rust-random/rand] to [rust-random/core].
1819

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

2729
[rust-random/rand]: https://github.com/rust-random/rand
2830
[rust-random/core]: https://github.com/rust-random/core

src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,34 @@ pub trait SeedableRng: Sized {
488488
rng.try_fill_bytes(seed.as_mut())?;
489489
Ok(Self::from_seed(seed))
490490
}
491+
492+
/// Fork this PRNG
493+
///
494+
/// This creates a new PRNG from the current one by initializing a new one and
495+
/// seeding it from the current one.
496+
///
497+
/// This is useful when initializing a PRNG for a thread
498+
fn fork(&mut self) -> Self
499+
where
500+
Self: RngCore,
501+
{
502+
Self::from_rng(self)
503+
}
504+
505+
/// Fork this PRNG
506+
///
507+
/// This creates a new PRNG from the current one by initializing a new one and
508+
/// seeding it from the current one.
509+
///
510+
/// This is useful when initializing a PRNG for a thread.
511+
///
512+
/// This is the failable equivalent to [`SeedableRng::fork`]
513+
fn try_fork(&mut self) -> Result<Self, Self::Error>
514+
where
515+
Self: TryRngCore,
516+
{
517+
Self::try_from_rng(self)
518+
}
491519
}
492520

493521
#[cfg(test)]

0 commit comments

Comments
 (0)