Trait rand::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.

Implementations on Foreign Types§

source§

impl TryRngCore for ChaCha8Rng

source§

impl TryRngCore for ChaCha12Rng

source§

impl TryRngCore for ChaCha20Rng

source§

impl<'a, R> TryRngCore for &'a mut R
where R: TryRngCore + ?Sized,

§

type Error = <R as TryRngCore>::Error

source§

fn try_next_u32(&mut self) -> Result<u32, <&'a mut R as TryRngCore>::Error>

source§

fn try_next_u64(&mut self) -> Result<u64, <&'a mut R as TryRngCore>::Error>

source§

fn try_fill_bytes( &mut self, dst: &mut [u8] ) -> Result<(), <&'a mut R as TryRngCore>::Error>

source§

impl<R> TryRngCore for Box<R>
where R: TryRngCore + ?Sized,

§

type Error = <R as TryRngCore>::Error

source§

fn try_next_u32(&mut self) -> Result<u32, <Box<R> as TryRngCore>::Error>

source§

fn try_next_u64(&mut self) -> Result<u64, <Box<R> as TryRngCore>::Error>

source§

fn try_fill_bytes( &mut self, dst: &mut [u8] ) -> Result<(), <Box<R> as TryRngCore>::Error>

Implementors§