Skip to content

Commit 776f908

Browse files
authored
Merge pull request rust-random#160 from tkilbourn/master
Implement rand for Fuchsia
2 parents b23740e + 5d5a50f commit 776f908

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rand"
3-
version = "0.3.15"
3+
version = "0.3.16"
44
authors = ["The Rust Project Developers"]
55
license = "MIT/Apache-2.0"
66
readme = "README.md"
@@ -25,3 +25,6 @@ log = "0.3.0"
2525

2626
[workspace]
2727
members = ["rand-derive"]
28+
29+
[target.'cfg(target_os = "fuchsia")'.dependencies]
30+
magenta = "^0.1.1"

src/os.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ fn next_u64(mut fill_buf: &mut FnMut(&mut [u8])) -> u64 {
6868
#[cfg(all(unix, not(target_os = "ios"),
6969
not(target_os = "nacl"),
7070
not(target_os = "freebsd"),
71+
not(target_os = "fuchsia"),
7172
not(target_os = "openbsd"),
7273
not(target_os = "redox")))]
7374
mod imp {
@@ -380,6 +381,45 @@ mod imp {
380381
}
381382
}
382383

384+
#[cfg(target_os = "fuchsia")]
385+
mod imp {
386+
extern crate magenta;
387+
388+
use std::io;
389+
use Rng;
390+
391+
use super::{next_u32, next_u64};
392+
393+
#[derive(Debug)]
394+
pub struct OsRng;
395+
396+
impl OsRng {
397+
pub fn new() -> io::Result<OsRng> {
398+
Ok(OsRng)
399+
}
400+
}
401+
402+
impl Rng for OsRng {
403+
fn next_u32(&mut self) -> u32 {
404+
next_u32(&mut |v| self.fill_bytes(v))
405+
}
406+
fn next_u64(&mut self) -> u64 {
407+
next_u64(&mut |v| self.fill_bytes(v))
408+
}
409+
fn fill_bytes(&mut self, v: &mut [u8]) {
410+
for s in v.chunks_mut(magenta::MX_CPRNG_DRAW_MAX_LEN) {
411+
let mut filled = 0;
412+
while filled < s.len() {
413+
match magenta::cprng_draw(&mut s[filled..]) {
414+
Ok(actual) => filled += actual,
415+
Err(e) => panic!("cprng_draw failed: {:?}", e),
416+
};
417+
}
418+
}
419+
}
420+
}
421+
}
422+
383423
#[cfg(windows)]
384424
mod imp {
385425
use std::io;

0 commit comments

Comments
 (0)