diff --git a/rand_core/src/error.rs b/rand_core/src/error.rs index 34cfbf8c8e4..594d3576986 100644 --- a/rand_core/src/error.rs +++ b/rand_core/src/error.rs @@ -14,6 +14,8 @@ use core::fmt; #[cfg(feature="std")] use std::error::Error as stdError; +#[cfg(feature="std")] +use std::io; /// Error kind which can be matched over. #[derive(PartialEq, Eq, Debug, Copy, Clone)] @@ -161,3 +163,17 @@ impl stdError for Error { self.cause.as_ref().map(|e| e.as_ref() as &stdError) } } + +#[cfg(feature="std")] +impl From for io::Error { + fn from(error: Error) -> Self { + use std::io::ErrorKind::*; + match error.kind { + ErrorKind::Unavailable => io::Error::new(NotFound, error), + ErrorKind::Unexpected | + ErrorKind::Transient => io::Error::new(Other, error), + ErrorKind::NotReady => io::Error::new(WouldBlock, error), + ErrorKind::__Nonexhaustive => unreachable!(), + } + } +} diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index fc8c419eeb3..2f87fd2b2e4 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -439,3 +439,11 @@ impl RngCore for Box { (**self).try_fill_bytes(dest) } } + +#[cfg(feature="std")] +impl std::io::Read for RngCore { + fn read(&mut self, buf: &mut [u8]) -> Result { + self.try_fill_bytes(buf)?; + Ok(buf.len()) + } +}