Skip to content

Commit

Permalink
Merge pull request #224 from EdSchouten/cloudabi
Browse files Browse the repository at this point in the history
Port rand to CloudABI.
  • Loading branch information
dhardy authored Jan 11, 2018
2 parents f090aa0 + 2c31e89 commit 6cee306
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "profileapi", "
[workspace]
members = ["rand-derive"]

[target.'cfg(target_os = "cloudabi")'.dependencies]
cloudabi = "0.0.3"

[target.'cfg(target_os = "fuchsia")'.dependencies]
fuchsia-zircon = "0.3.2"
33 changes: 31 additions & 2 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ impl<R: Read> ReadRng<R> {
}
}

#[cfg(all(unix, not(target_os = "ios"),
not(target_os = "nacl"),
#[cfg(all(unix,
not(target_os = "cloudabi"),
not(target_os = "freebsd"),
not(target_os = "fuchsia"),
not(target_os = "ios"),
not(target_os = "nacl"),
not(target_os = "openbsd"),
not(target_os = "redox")))]
mod imp {
Expand Down Expand Up @@ -267,6 +269,33 @@ mod imp {
}
}

#[cfg(target_os = "cloudabi")]
mod imp {
extern crate cloudabi;

use {Error, ErrorKind};

#[derive(Debug)]
pub struct OsRng;

impl OsRng {
pub fn new() -> Result<OsRng, Error> {
Ok(OsRng)
}

pub fn try_fill_bytes(&mut self, v: &mut [u8]) -> Result<(), Error> {
if unsafe { cloudabi::random_get(v) } == cloudabi::errno::SUCCESS {
Ok(())
} else {
Err(Error::new(
ErrorKind::Unavailable,
"random_get() system call failed",
))
}
}
}
}

#[cfg(target_os = "ios")]
mod imp {
extern crate libc;
Expand Down

0 comments on commit 6cee306

Please sign in to comment.