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

Port rand to CloudABI. #224

Merged
merged 1 commit into from
Jan 11, 2018
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
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