Skip to content

Commit 6f69984

Browse files
committed
Remove lazy_static dependancy
1 parent aa28bee commit 6f69984

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

Cargo.toml

-5
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@ log = { version = "0.4", optional = true }
2323
[target.'cfg(any(unix, target_os = "wasi"))'.dependencies]
2424
libc = "0.2.54"
2525

26-
# For holding file descriptors
27-
[target.'cfg(any(unix, target_os = "redox"))'.dependencies]
28-
lazy_static = "1.3.0"
29-
3026
[target.wasm32-unknown-unknown.dependencies]
3127
wasm-bindgen = { version = "0.2.29", optional = true }
3228
stdweb = { version = "0.4.9", optional = true }
33-
lazy_static = "1.3.0"
3429

3530
[features]
3631
std = []

src/use_file.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ extern crate std;
1111

1212
use crate::Error;
1313
use core::num::NonZeroU32;
14-
use lazy_static::lazy_static;
15-
use std::{fs::File, io::Read};
14+
use std::{fs::File, io::Read, sync::Once};
1615

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

3130
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
32-
lazy_static! {
33-
static ref FILE: Result<File, Error> = init_file();
34-
}
35-
let mut f = FILE.as_ref()?;
31+
static ONCE: Once = Once::new();
32+
static mut FILE: Option<Result<File, Error>> = None;
33+
34+
// SAFETY: FILE is only written once, before being read.
35+
ONCE.call_once(|| unsafe { FILE = Some(init_file()); });
36+
let mut f = unsafe { FILE.as_ref() }.unwrap().as_ref()?;
3637

3738
if cfg!(target_os = "emscripten") {
3839
// `Crypto.getRandomValues` documents `dest` should be at most 65536 bytes.

src/wasm32_stdweb.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use stdweb::web::error::Error as WebError;
1515
use stdweb::{_js_impl, js};
1616

1717
use crate::Error;
18-
use lazy_static::lazy_static;
18+
use std::sync::Once;
1919

2020
#[derive(Clone, Copy, Debug)]
2121
enum RngSource {
@@ -25,11 +25,12 @@ enum RngSource {
2525

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

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

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

0 commit comments

Comments
 (0)