]> git.proxmox.com Git - cargo.git/blob - vendor/rand_os/src/dragonfly_haiku_emscripten.rs
New upstream version 0.33.0
[cargo.git] / vendor / rand_os / src / dragonfly_haiku_emscripten.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 DragonFly / Haiku / Emscripten
10
11 use rand_core::Error;
12 use super::random_device;
13 use super::OsRngImpl;
14 use std::fs::File;
15
16 #[derive(Clone, Debug)]
17 pub struct OsRng();
18
19 impl OsRngImpl for OsRng {
20 fn new() -> Result<OsRng, Error> {
21 random_device::open("/dev/random", &|p| File::open(p))?;
22 Ok(OsRng())
23 }
24
25 fn fill_chunk(&mut self, dest: &mut [u8]) -> Result<(), Error> {
26 random_device::read(dest)
27 }
28
29 #[cfg(target_os = "emscripten")]
30 fn max_chunk_size(&self) -> usize {
31 // `Crypto.getRandomValues` documents `dest` should be at most 65536
32 // bytes. `crypto.randomBytes` documents: "To minimize threadpool
33 // task length variation, partition large randomBytes requests when
34 // doing so as part of fulfilling a client request.
35 65536
36 }
37
38 fn method_str(&self) -> &'static str { "/dev/random" }
39 }