pub struct OsRng;
Expand description
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();
Trait Implementations§
Source§impl TryRngCore for OsRng
impl TryRngCore for OsRng
Source§fn try_next_u32(&mut self) -> Result<u32, <OsRng as TryRngCore>::Error>
fn try_next_u32(&mut self) -> Result<u32, <OsRng as TryRngCore>::Error>
u32
.Source§fn try_next_u64(&mut self) -> Result<u64, <OsRng as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <OsRng as TryRngCore>::Error>
u64
.Source§fn try_fill_bytes(
&mut self,
dest: &mut [u8],
) -> Result<(), <OsRng as TryRngCore>::Error>
fn try_fill_bytes( &mut self, dest: &mut [u8], ) -> Result<(), <OsRng as TryRngCore>::Error>
dest
entirely with random data.Source§fn read_adapter(&mut self) -> RngReadAdapter<'_, Self> ⓘwhere
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self> ⓘwhere
Self: Sized,
RngCore
to a RngReadAdapter
.impl Copy for OsRng
impl TryCryptoRng for OsRng
Auto Trait Implementations§
impl Freeze for OsRng
impl RefUnwindSafe for OsRng
impl Send for OsRng
impl Sync for OsRng
impl Unpin for OsRng
impl UnwindSafe for OsRng
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more