rand_core

Trait TryRngCore

Source
pub trait TryRngCore {
    type Error: Debug + Display;

    // Required methods
    fn try_next_u32(&mut self) -> Result<u32, Self::Error>;
    fn try_next_u64(&mut self) -> Result<u64, Self::Error>;
    fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error>;

    // Provided methods
    fn unwrap_err(self) -> UnwrapErr<Self>
       where Self: Sized { ... }
    fn read_adapter(&mut self) -> RngReadAdapter<'_, Self> 
       where Self: Sized { ... }
}
Expand description

A potentially fallible version of RngCore.

This trait is primarily used for IO-based generators such as OsRng.

Most of higher-level generic code in the rand crate is built on top of the the RngCore trait. Users can transform a fallible RNG (i.e. TryRngCore implementor) into an “infallible” (but potentially panicking) RNG (i.e. RngCore implementor) using the UnwrapErr wrapper.

RngCore implementors also usually implement TryRngCore with the Error associated type being equal to Infallible. In other words, users can use TryRngCore to generalize over fallible and infallible RNGs.

Required Associated Types§

Source

type Error: Debug + Display

The type returned in the event of a RNG error.

Required Methods§

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 dest entirely with random data.

Provided Methods§

Source

fn unwrap_err(self) -> UnwrapErr<Self>
where Self: Sized,

Wrap RNG with the UnwrapErr wrapper.

Source

fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>
where Self: Sized,

Convert an RngCore to a RngReadAdapter.

Implementors§