]> git.proxmox.com Git - cargo.git/blob - vendor/rand_os/src/cloudabi.rs
New upstream version 0.33.0
[cargo.git] / vendor / rand_os / src / cloudabi.rs
1 // Copyright 2018 Developers of the Rand project.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 //! Implementation for CloudABI
10
11 extern crate cloudabi;
12
13 use std::io;
14 use rand_core::{Error, ErrorKind};
15 use super::OsRngImpl;
16
17 #[derive(Clone, Debug)]
18 pub struct OsRng;
19
20 impl OsRngImpl for OsRng {
21 fn new() -> Result<OsRng, Error> { Ok(OsRng) }
22
23 fn fill_chunk(&mut self, dest: &mut [u8]) -> Result<(), Error> {
24 let errno = unsafe { cloudabi::random_get(dest) };
25 if errno == cloudabi::errno::SUCCESS {
26 Ok(())
27 } else {
28 // Cloudlibc provides its own `strerror` implementation so we
29 // can use `from_raw_os_error` here.
30 Err(Error::with_cause(
31 ErrorKind::Unavailable,
32 "random_get() system call failed",
33 io::Error::from_raw_os_error(errno as i32),
34 ))
35 }
36 }
37
38 fn method_str(&self) -> &'static str { "cloudabi::random_get" }
39 }