@@ -6,13 +6,14 @@ use rand_chacha::{
6
6
rand_core:: { RngCore , SeedableRng } ,
7
7
ChaCha20Rng ,
8
8
} ;
9
- use spin:: Mutex ;
9
+ use spin:: { Mutex , MutexGuard } ;
10
10
11
11
pub use crate :: input:: InputPreprocessor ;
12
12
use std:: { cell:: Cell , collections:: VecDeque } ;
13
13
14
14
pub type ActionList = Vec < Vec < MessageDiscriminant > > ;
15
15
16
+ #[ cfg( not( test) ) ]
16
17
static RNG : Mutex < Option < ChaCha20Rng > > = Mutex :: new ( None ) ;
17
18
18
19
// TODO: Add Send + Sync requirement
@@ -31,19 +32,29 @@ where
31
32
32
33
thread_local ! {
33
34
pub static UUID_SEED : Cell <Option <u64 >> = Cell :: new( None ) ;
35
+ #[ cfg( test) ]
36
+ static LOCAL_RNG : Mutex <Option <ChaCha20Rng >> = Mutex :: new( None ) ;
34
37
}
35
38
36
39
pub fn set_uuid_seed ( random_seed : u64 ) {
37
- UUID_SEED . with ( |seed| seed. set ( Some ( random_seed) ) )
40
+ UUID_SEED . with ( |seed| seed. set ( Some ( random_seed) ) ) ;
38
41
}
39
42
40
43
pub fn generate_uuid ( ) -> u64 {
41
- let mut lock = RNG . lock ( ) ;
42
- if lock. is_none ( ) {
43
- UUID_SEED . with ( |seed| {
44
- let random_seed = seed. get ( ) . expect ( "random seed not set before editor was initialized" ) ;
45
- * lock = Some ( ChaCha20Rng :: seed_from_u64 ( random_seed) ) ;
46
- } )
47
- }
48
- lock. as_mut ( ) . map ( ChaCha20Rng :: next_u64) . unwrap ( )
44
+ let init = |mut lock : MutexGuard < Option < ChaCha20Rng > > | {
45
+ if lock. is_none ( ) {
46
+ UUID_SEED . with ( |seed| {
47
+ let random_seed = seed. get ( ) . expect ( "random seed not set before editor was initialized" ) ;
48
+ * lock = Some ( ChaCha20Rng :: seed_from_u64 ( random_seed) ) ;
49
+ } )
50
+ }
51
+ lock. as_mut ( ) . map ( ChaCha20Rng :: next_u64) . unwrap ( )
52
+ } ;
53
+ (
54
+ #[ cfg( test) ]
55
+ LOCAL_RNG . with ( |rng| init ( rng. lock ( ) ) ) ,
56
+ #[ cfg ( not ( test) ) ]
57
+ init ( RNG . lock ( ) ) ,
58
+ )
59
+ . 0
49
60
}
0 commit comments