File tree 3 files changed +13
-16
lines changed
3 files changed +13
-16
lines changed Original file line number Diff line number Diff line change @@ -23,14 +23,9 @@ log = { version = "0.4", optional = true }
23
23
[target .'cfg(any(unix, target_os = "wasi"))' .dependencies ]
24
24
libc = " 0.2.54"
25
25
26
- # For holding file descriptors
27
- [target .'cfg(any(unix, target_os = "redox"))' .dependencies ]
28
- lazy_static = " 1.3.0"
29
-
30
26
[target .wasm32-unknown-unknown .dependencies ]
31
27
wasm-bindgen = { version = " 0.2.29" , optional = true }
32
28
stdweb = { version = " 0.4.9" , optional = true }
33
- lazy_static = " 1.3.0"
34
29
35
30
[features ]
36
31
std = []
Original file line number Diff line number Diff line change @@ -11,8 +11,7 @@ extern crate std;
11
11
12
12
use crate :: Error ;
13
13
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 } ;
16
15
17
16
#[ cfg( target_os = "redox" ) ]
18
17
const FILE_PATH : & str = "rand:" ;
@@ -29,10 +28,12 @@ const FILE_PATH: &str = "/dev/urandom";
29
28
const FILE_PATH : & str = "/dev/random" ;
30
29
31
30
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 ( ) ?;
36
37
37
38
if cfg ! ( target_os = "emscripten" ) {
38
39
// `Crypto.getRandomValues` documents `dest` should be at most 65536 bytes.
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ use stdweb::web::error::Error as WebError;
15
15
use stdweb:: { _js_impl, js} ;
16
16
17
17
use crate :: Error ;
18
- use lazy_static :: lazy_static ;
18
+ use std :: sync :: Once ;
19
19
20
20
#[ derive( Clone , Copy , Debug ) ]
21
21
enum RngSource {
@@ -25,11 +25,12 @@ enum RngSource {
25
25
26
26
pub fn getrandom_inner ( dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
27
27
assert_eq ! ( mem:: size_of:: <usize >( ) , 4 ) ;
28
+ static ONCE : Once = Once :: new ( ) ;
29
+ static mut RNG_SOURCE : Result < RngSource , Error > = Err ( Error :: UNAVAILABLE ) ;
28
30
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)
33
34
}
34
35
35
36
fn getrandom_init ( ) -> Result < RngSource , Error > {
You can’t perform that action at this time.
0 commit comments