From 335c39078726cb38e51adf16012fb0366e167ef9 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 8 May 2018 13:56:16 +0200 Subject: [PATCH 1/2] Implement From for io::Error --- rand_core/src/error.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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!(), + } + } +} From 7959fbf63f6f66f83758fc94bd44b98c9295d645 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 8 May 2018 13:56:30 +0200 Subject: [PATCH 2/2] Implement std::io::Read for RngCore --- rand_core/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) 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()) + } +}