Struct OsRng

struct OsRng

An interface over the operating-system's random data source

This is a zero-sized struct. It can be freely constructed with just OsRng.

The implementation is provided by the getrandom crate. Refer to getrandom documentation for details.

This struct is available as rand_core::OsRng and as rand::rngs::OsRng. In both cases, this requires the crate feature os_rng or std (enabled by default in rand but not in rand_core).

Blocking and error handling

It is possible that when used during early boot the first call to OsRng will block until the system's RNG is initialised. It is also possible (though highly unlikely) for OsRng to fail on some platforms, most likely due to system mis-configuration.

After the first successful call, it is highly unlikely that failures or significant delays will occur (although performance should be expected to be much slower than a user-space PRNG).

Usage example

use rand_core::{TryRngCore, OsRng};

let mut key = [0u8; 16];
OsRng.try_fill_bytes(&mut key).unwrap();
let random_u64 = OsRng.try_next_u64().unwrap();

Implementations

impl Clone for OsRng

fn clone(self: &Self) -> OsRng

impl Copy for OsRng

impl Debug for OsRng

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

impl Default for OsRng

fn default() -> OsRng

impl Freeze for OsRng

impl RefUnwindSafe for OsRng

impl Send for OsRng

impl Sync for OsRng

impl TryCryptoRng for OsRng

impl TryRngCore for OsRng

fn try_next_u32(self: &mut Self) -> Result<u32, <Self as >::Error>
fn try_next_u64(self: &mut Self) -> Result<u64, <Self as >::Error>
fn try_fill_bytes(self: &mut Self, dest: &mut [u8]) -> Result<(), <Self as >::Error>

impl Unpin for OsRng

impl UnsafeUnpin for OsRng

impl UnwindSafe for OsRng

impl<T> Any for OsRng

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for OsRng

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for OsRng

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

impl<T> CloneToUninit for OsRng

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

impl<T> From for OsRng

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for OsRng

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for OsRng

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for OsRng

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

impl<T, U> TryInto for OsRng

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