From 8ca3d695a33a958ba1944d1447c978053a8602b2 Mon Sep 17 00:00:00 2001 From: Tim Kilbourn Date: Mon, 24 Jul 2017 14:28:51 -0700 Subject: [PATCH 1/2] Implement rand for Fuchsia --- Cargo.toml | 5 ++++- src/os.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 51ff34a1b69..3bdfc14f56f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rand" -version = "0.3.15" +version = "0.3.16" authors = ["The Rust Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" @@ -25,3 +25,6 @@ log = "0.3.0" [workspace] members = ["rand-derive"] + +[target.'cfg(target_os = "fuchsia")'.dependencies] +magenta = "0.1.1" diff --git a/src/os.rs b/src/os.rs index 7f241197535..82a09ad0347 100644 --- a/src/os.rs +++ b/src/os.rs @@ -68,6 +68,7 @@ fn next_u64(mut fill_buf: &mut FnMut(&mut [u8])) -> u64 { #[cfg(all(unix, not(target_os = "ios"), not(target_os = "nacl"), not(target_os = "freebsd"), + not(target_os = "fuchsia"), not(target_os = "openbsd"), not(target_os = "redox")))] mod imp { @@ -380,6 +381,45 @@ mod imp { } } +#[cfg(target_os = "fuchsia")] +mod imp { + extern crate magenta; + + use std::io; + use Rng; + + use super::{next_u32, next_u64}; + + #[derive(Debug)] + pub struct OsRng; + + impl OsRng { + pub fn new() -> io::Result { + Ok(OsRng) + } + } + + impl Rng for OsRng { + fn next_u32(&mut self) -> u32 { + next_u32(&mut |v| self.fill_bytes(v)) + } + fn next_u64(&mut self) -> u64 { + next_u64(&mut |v| self.fill_bytes(v)) + } + fn fill_bytes(&mut self, v: &mut [u8]) { + for s in v.chunks_mut(magenta::MX_CPRNG_DRAW_MAX_LEN) { + let mut filled = 0; + while filled < s.len() { + match magenta::cprng_draw(&mut s[filled..]) { + Ok(actual) => filled += actual, + Err(e) => panic!("cprng_draw failed: {:?}", e), + }; + } + } + } + } +} + #[cfg(windows)] mod imp { use std::io; From d8bfc94e7efad922f1fc0362fb45ead71ecc7a48 Mon Sep 17 00:00:00 2001 From: Tim Kilbourn Date: Thu, 27 Jul 2017 11:18:39 -0700 Subject: [PATCH 2/2] More flexibility in magenta version requirements --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3bdfc14f56f..e7dd0f42ae1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,4 @@ log = "0.3.0" members = ["rand-derive"] [target.'cfg(target_os = "fuchsia")'.dependencies] -magenta = "0.1.1" +magenta = "^0.1.1"