Skip to main content

ReseedingRng

Struct ReseedingRng 

Source
pub struct ReseedingRng<R, Rsdr> { /* private fields */ }
Expand description

A wrapper that periodically reseeds the underlying pseudorandom number generator.

This type reseeds the underlying generator every time a specified number of random bytes have been produced. If the periodic reseeding attempt fails, ReseedingRng silently skips it and retries after the next threshold is reached.

Unlike rand v0.9’s equivalent, this variant is built on top of TryRng instead of the block Generator, allowing a wider choice of underlying generators, including StdRng.

§Examples

ReseedingRng is useful to replicate the reseeding behavior of ThreadRng. As of rand v0.10.1, ThreadRng uses the same algorithm as StdRng and reseeds it via SysRng every 64KiB of output. You can emulate this behavior by configuring ReseedingRng as follows:

use rand::{RngExt as _, rngs::StdRng, rngs::SysRng};
use reseeding_rng::ReseedingRng;

let mut rng = ReseedingRng::<StdRng, _>::try_new(1024 * 64, SysRng)
    .expect("couldn't initialize ReseedingRng due to SysRng failure");
println!("{:?}", rng.random::<[char; 4]>());

See also [StdReseedingRng] for a convenient newtype with this configuration.

§Fork safety

The underlying generator is not automatically reseeded on process fork (contrast with ReseedingRng from rand v0.8 and earlier). Some applications need reseeding on fork to avoid the parent and child processes generating the same sequence of random numbers. The example below shows a wrapper that handles this using the forkguard crate.

use rand::{Rng as _, rngs::StdRng, rngs::SysRng};

struct ForkSafeReseedingRng {
    inner: reseeding_rng::ReseedingRng<StdRng, SysRng>,
    guard: forkguard::Guard,
}

impl ForkSafeReseedingRng {
    fn next_u32(&mut self) -> u32 {
        if self.guard.detected_fork() {
            // reseed ReseedingRng in child process
            let _ = self.inner.try_reseed();
        }
        self.inner.next_u32()
    }
}

Implementations§

Source§

impl<R, Rsdr> ReseedingRng<R, Rsdr>
where R: SeedableRng, Rsdr: TryRng,

Source

pub fn try_new(threshold: usize, reseeder: Rsdr) -> Result<Self, Rsdr::Error>

Creates a new instance with a reseeding threshold in bytes and a seed generator for initialization and reseeding.

§Panics

Panics if threshold is zero.

§Errors

Returns Err if reseeder fails to seed the underlying generator.

Source

pub fn try_reseed(&mut self) -> Result<(), Rsdr::Error>

Reseeds the underlying generator immediately.

§Errors

Returns Err if reseeder fails to seed the underlying generator.

Trait Implementations§

Source§

impl<R, Rsdr> Clone for ReseedingRng<R, Rsdr>
where R: SeedableRng, Rsdr: Clone + Rng,

This implementation reseeds the underlying generator upon clone().

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R, Rsdr> Debug for ReseedingRng<R, Rsdr>
where R: Debug, Rsdr: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<R, Rsdr> TryCryptoRng for ReseedingRng<R, Rsdr>

Source§

impl<R, Rsdr> TryRng for ReseedingRng<R, Rsdr>
where R: TryRng + SeedableRng, Rsdr: TryRng,

Source§

type Error = <R as TryRng>::Error

The type returned in the event of a RNG error. Read more
Source§

fn try_next_u32(&mut self) -> Result<u32, Self::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, Self::Error>

Return the next random u64.
Source§

fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error>

Fill dst entirely with random data.

Auto Trait Implementations§

§

impl<R, Rsdr> Freeze for ReseedingRng<R, Rsdr>
where R: Freeze, Rsdr: Freeze,

§

impl<R, Rsdr> RefUnwindSafe for ReseedingRng<R, Rsdr>
where R: RefUnwindSafe, Rsdr: RefUnwindSafe,

§

impl<R, Rsdr> Send for ReseedingRng<R, Rsdr>
where R: Send, Rsdr: Send,

§

impl<R, Rsdr> Sync for ReseedingRng<R, Rsdr>
where R: Sync, Rsdr: Sync,

§

impl<R, Rsdr> Unpin for ReseedingRng<R, Rsdr>
where R: Unpin, Rsdr: Unpin,

§

impl<R, Rsdr> UnsafeUnpin for ReseedingRng<R, Rsdr>
where R: UnsafeUnpin, Rsdr: UnsafeUnpin,

§

impl<R, Rsdr> UnwindSafe for ReseedingRng<R, Rsdr>
where R: UnwindSafe, Rsdr: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<R> CryptoRng for R
where R: TryCryptoRng<Error = Infallible> + ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<R> Rng for R
where R: TryRng<Error = Infallible> + ?Sized,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32.
Source§

fn next_u64(&mut self) -> u64

Return the next random u64.
Source§

fn fill_bytes(&mut self, dst: &mut [u8])

Fill dest with random data. Read more
Source§

impl<R> RngCore for R
where R: Rng,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<R> TryRngCore for R
where R: TryRng,

Source§

type Error = <R as TryRng>::Error

👎Deprecated since 0.10.0:

use TryRng instead

Error type.