Skip to content

Commit

Permalink
Remove lazy_static dependancy
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlr committed Jul 2, 2019
1 parent aa28bee commit 6f69984
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@ log = { version = "0.4", optional = true }
[target.'cfg(any(unix, target_os = "wasi"))'.dependencies]
libc = "0.2.54"

# For holding file descriptors
[target.'cfg(any(unix, target_os = "redox"))'.dependencies]
lazy_static = "1.3.0"

[target.wasm32-unknown-unknown.dependencies]
wasm-bindgen = { version = "0.2.29", optional = true }
stdweb = { version = "0.4.9", optional = true }
lazy_static = "1.3.0"

[features]
std = []
13 changes: 7 additions & 6 deletions src/use_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ extern crate std;

use crate::Error;
use core::num::NonZeroU32;
use lazy_static::lazy_static;
use std::{fs::File, io::Read};
use std::{fs::File, io::Read, sync::Once};

#[cfg(target_os = "redox")]
const FILE_PATH: &str = "rand:";
Expand All @@ -29,10 +28,12 @@ const FILE_PATH: &str = "/dev/urandom";
const FILE_PATH: &str = "/dev/random";

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
lazy_static! {
static ref FILE: Result<File, Error> = init_file();
}
let mut f = FILE.as_ref()?;
static ONCE: Once = Once::new();
static mut FILE: Option<Result<File, Error>> = None;

// SAFETY: FILE is only written once, before being read.
ONCE.call_once(|| unsafe { FILE = Some(init_file()); });
let mut f = unsafe { FILE.as_ref() }.unwrap().as_ref()?;

if cfg!(target_os = "emscripten") {
// `Crypto.getRandomValues` documents `dest` should be at most 65536 bytes.
Expand Down
11 changes: 6 additions & 5 deletions src/wasm32_stdweb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use stdweb::web::error::Error as WebError;
use stdweb::{_js_impl, js};

use crate::Error;
use lazy_static::lazy_static;
use std::sync::Once;

#[derive(Clone, Copy, Debug)]
enum RngSource {
Expand All @@ -25,11 +25,12 @@ enum RngSource {

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
assert_eq!(mem::size_of::<usize>(), 4);
static ONCE: Once = Once::new();
static mut RNG_SOURCE: Result<RngSource, Error> = Err(Error::UNAVAILABLE);

lazy_static! {
static ref RNG_SOURCE: Result<RngSource, Error> = getrandom_init();
}
getrandom_fill((*RNG_SOURCE)?, dest)
// SAFETY: RNG_SOURCE is only written once, before being read.
ONCE.call_once(|| unsafe { RNG_SOURCE = getrandom_init(); });
getrandom_fill(unsafe { RNG_SOURCE }?, dest)
}

fn getrandom_init() -> Result<RngSource, Error> {
Expand Down

0 comments on commit 6f69984

Please sign in to comment.