Skip to content
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

Implement rand for Fuchsia #160

Merged
merged 2 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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