Skip to content

Commit c963f9d

Browse files
committed
Switch rand_os to use fuchsia-cprng crate
The fuchsia team has spun out a new crate, [fuchsia-cprng](https://crates.io/crates/fuchsia-cprng), which reduces your exposure to changes in our syscalls, which are still not stable. This crate just exposes the CPRNG syscall, which we do not expect to change. Also note, the underlying syscall, [zx_cprng_draw](https://fuchsia.googlesource.com/zircon/+/HEAD/docs/syscalls/cprng_draw.md) no longer needs the loop. Instead, we guarantee that the buffer always be filled with randomness.
1 parent 6f3875f commit c963f9d

File tree

2 files changed

+4
-19
lines changed

2 files changed

+4
-19
lines changed

rand_os/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "winnt"] }
2828
cloudabi = "0.0.3"
2929

3030
[target.'cfg(target_os = "fuchsia")'.dependencies]
31-
fuchsia-zircon = "0.3.2"
31+
fuchsia-cprng = "0.1.0"
3232

3333
[target.wasm32-unknown-unknown.dependencies]
3434
wasm-bindgen = { version = "0.2.12", optional = true }

rand_os/src/fuchsia.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
//! Implementation for Fuchsia Zircon
1010
11-
extern crate fuchsia_zircon;
11+
extern crate fuchsia_cprng;
1212

13-
use rand_core::{Error, ErrorKind};
13+
use rand_core::Error;
1414
use super::OsRngImpl;
1515

1616
#[derive(Clone, Debug)]
@@ -20,24 +20,9 @@ impl OsRngImpl for OsRng {
2020
fn new() -> Result<OsRng, Error> { Ok(OsRng) }
2121

2222
fn fill_chunk(&mut self, dest: &mut [u8]) -> Result<(), Error> {
23-
let mut read = 0;
24-
while read < dest.len() {
25-
match fuchsia_zircon::cprng_draw(&mut dest[read..]) {
26-
Ok(actual) => read += actual,
27-
Err(e) => {
28-
return Err(Error::with_cause(
29-
ErrorKind::Unavailable,
30-
"cprng_draw failed",
31-
e.into_io_error()));
32-
}
33-
};
34-
}
23+
fuchsia_cprng::cprng_draw(dest);
3524
Ok(())
3625
}
3726

38-
fn max_chunk_size(&self) -> usize {
39-
fuchsia_zircon::sys::ZX_CPRNG_DRAW_MAX_LEN
40-
}
41-
4227
fn method_str(&self) -> &'static str { "cprng_draw" }
4328
}

0 commit comments

Comments
 (0)