-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rand_core::Error new version #771
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,20 +28,18 @@ | |
#![deny(missing_debug_implementations)] | ||
#![doc(test(attr(allow(unused_variables), deny(warnings))))] | ||
|
||
#![cfg_attr(feature = "stdweb", recursion_limit="128")] | ||
|
||
#![no_std] // but see getrandom crate | ||
|
||
pub use rand_core; // re-export | ||
|
||
use getrandom::getrandom; | ||
use rand_core::{CryptoRng, RngCore, Error, ErrorKind, impls}; | ||
use rand_core::{CryptoRng, RngCore, Error, impls}; | ||
|
||
/// A random number generator that retrieves randomness from from the | ||
/// operating system. | ||
/// | ||
/// This is a zero-sized struct. It can be freely constructed with `OsRng`. | ||
/// | ||
/// | ||
/// The implementation is provided by the [getrandom] crate. Refer to | ||
/// [getrandom] documentation for details. | ||
/// | ||
|
@@ -84,12 +82,14 @@ impl RngCore for OsRng { | |
} | ||
|
||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { | ||
// Some systems do not support reading 0 random bytes. | ||
// (And why waste a system call?) | ||
if dest.len() == 0 { return Ok(()); } | ||
|
||
getrandom(dest).map_err(|e| | ||
Error::with_cause(ErrorKind::Unavailable, "OsRng failed", e)) | ||
getrandom(dest).map_err(|e| { | ||
#[cfg(feature="std")] { | ||
rand_core::Error::from_cause(e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nicer not to have to use a |
||
} | ||
#[cfg(not(feature="std"))] { | ||
rand_core::Error::from_code(e.code()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
|
||
use std::io::Read; | ||
|
||
use rand_core::{RngCore, Error, ErrorKind, impls}; | ||
use rand_core::{RngCore, Error, impls}; | ||
|
||
|
||
/// An RNG that reads random bytes straight from any type supporting | ||
|
@@ -73,22 +73,15 @@ impl<R: Read> RngCore for ReadRng<R> { | |
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { | ||
if dest.len() == 0 { return Ok(()); } | ||
// Use `std::io::read_exact`, which retries on `ErrorKind::Interrupted`. | ||
self.reader.read_exact(dest).map_err(|err| { | ||
match err.kind() { | ||
::std::io::ErrorKind::UnexpectedEof => Error::with_cause( | ||
ErrorKind::Unavailable, | ||
"not enough bytes available, reached end of source", err), | ||
_ => Error::with_cause(ErrorKind::Unavailable, | ||
"error reading from Read source", err) | ||
} | ||
}) | ||
self.reader.read_exact(dest).map_err(Error::from_cause) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In theory it's nice to be able to include an extra message here (error manifesting and cause), but not critical. |
||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::ReadRng; | ||
use {RngCore, ErrorKind}; | ||
use RngCore; | ||
use std::io; | ||
|
||
#[test] | ||
fn test_reader_rng_u64() { | ||
|
@@ -131,6 +124,13 @@ mod test { | |
|
||
let mut rng = ReadRng::new(&v[..]); | ||
|
||
assert!(rng.try_fill_bytes(&mut w).err().unwrap().kind == ErrorKind::Unavailable); | ||
let err_kind = rng.try_fill_bytes(&mut w) | ||
.err() | ||
.unwrap() | ||
.cause() | ||
.downcast_ref() | ||
.map(|e: &io::Error| e.kind()) | ||
.unwrap(); | ||
assert_eq!(err_kind, io::ErrorKind::UnexpectedEof); | ||
dhardy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want this (and it's also not incompatible with the current Error type).