Skip to content

Commit 6cee306

Browse files
authored
Merge pull request #224 from EdSchouten/cloudabi
Port rand to CloudABI.
2 parents f090aa0 + 2c31e89 commit 6cee306

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "profileapi", "
3131
[workspace]
3232
members = ["rand-derive"]
3333

34+
[target.'cfg(target_os = "cloudabi")'.dependencies]
35+
cloudabi = "0.0.3"
36+
3437
[target.'cfg(target_os = "fuchsia")'.dependencies]
3538
fuchsia-zircon = "0.3.2"

src/os.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ impl<R: Read> ReadRng<R> {
115115
}
116116
}
117117

118-
#[cfg(all(unix, not(target_os = "ios"),
119-
not(target_os = "nacl"),
118+
#[cfg(all(unix,
119+
not(target_os = "cloudabi"),
120120
not(target_os = "freebsd"),
121121
not(target_os = "fuchsia"),
122+
not(target_os = "ios"),
123+
not(target_os = "nacl"),
122124
not(target_os = "openbsd"),
123125
not(target_os = "redox")))]
124126
mod imp {
@@ -267,6 +269,33 @@ mod imp {
267269
}
268270
}
269271

272+
#[cfg(target_os = "cloudabi")]
273+
mod imp {
274+
extern crate cloudabi;
275+
276+
use {Error, ErrorKind};
277+
278+
#[derive(Debug)]
279+
pub struct OsRng;
280+
281+
impl OsRng {
282+
pub fn new() -> Result<OsRng, Error> {
283+
Ok(OsRng)
284+
}
285+
286+
pub fn try_fill_bytes(&mut self, v: &mut [u8]) -> Result<(), Error> {
287+
if unsafe { cloudabi::random_get(v) } == cloudabi::errno::SUCCESS {
288+
Ok(())
289+
} else {
290+
Err(Error::new(
291+
ErrorKind::Unavailable,
292+
"random_get() system call failed",
293+
))
294+
}
295+
}
296+
}
297+
}
298+
270299
#[cfg(target_os = "ios")]
271300
mod imp {
272301
extern crate libc;

0 commit comments

Comments
 (0)