Skip to content

Commit

Permalink
Merge pull request #160 from tkilbourn/master
Browse files Browse the repository at this point in the history
Implement rand for Fuchsia
  • Loading branch information
alexcrichton authored Jul 27, 2017
2 parents bb78689 + d8bfc94 commit 1a2100d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -25,3 +25,6 @@ log = "0.3.0"

[workspace]
members = ["rand-derive"]

[target.'cfg(target_os = "fuchsia")'.dependencies]
magenta = "^0.1.1"
40 changes: 40 additions & 0 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<OsRng> {
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;
Expand Down

0 comments on commit 1a2100d

Please sign in to comment.