1
1
use std:: fmt:: Debug ;
2
2
3
3
use crate :: {
4
- entropy_source:: EntropySource , resource:: GlobalEntropy , Deserialize , RngCore , SeedableRng ,
5
- Serialize ,
4
+ thread_local_entropy:: ThreadLocalEntropy ,
5
+ resource:: GlobalEntropy ,
6
+ traits:: { EntropySource , SeedableEntropySource } ,
7
+ Deserialize , RngCore , SeedableRng , Serialize ,
6
8
} ;
7
9
use bevy_ecs:: { prelude:: Component , system:: ResMut , world:: Mut } ;
8
10
use bevy_reflect:: { FromReflect , Reflect , ReflectDeserialize , ReflectSerialize } ;
9
11
10
12
#[ derive( Debug , Clone , PartialEq , Eq , Component , Reflect , FromReflect , Serialize , Deserialize ) ]
11
13
#[ serde( bound( deserialize = "R: for<'a> Deserialize<'a>" ) ) ]
12
14
#[ reflect_value( Debug , PartialEq , Serialize , Deserialize ) ]
13
- pub struct EntropyComponent <
14
- R : RngCore
15
- + Clone
16
- + Debug
17
- + PartialEq
18
- + Sync
19
- + Send
20
- + Serialize
21
- + for < ' a > Deserialize < ' a >
22
- + ' static ,
23
- > ( R ) ;
24
-
25
- impl <
26
- R : RngCore
27
- + Clone
28
- + Debug
29
- + PartialEq
30
- + Sync
31
- + Send
32
- + Serialize
33
- + for < ' a > Deserialize < ' a >
34
- + ' static ,
35
- > EntropyComponent < R >
36
- {
15
+ pub struct EntropyComponent < R : EntropySource + ' static > ( R ) ;
16
+
17
+ impl < R : EntropySource + ' static > EntropyComponent < R > {
37
18
#[ inline]
38
19
#[ must_use]
39
20
pub fn new ( rng : R ) -> Self {
40
21
Self ( rng)
41
22
}
42
23
}
43
24
44
- impl <
45
- R : RngCore
46
- + SeedableRng
47
- + Clone
48
- + Debug
49
- + PartialEq
50
- + Sync
51
- + Send
52
- + Serialize
53
- + for < ' a > Deserialize < ' a >
54
- + ' static ,
55
- > EntropyComponent < R >
56
- {
25
+ impl < R : SeedableEntropySource + ' static > EntropyComponent < R > {
57
26
#[ inline]
58
27
#[ must_use]
59
28
pub fn from_entropy ( ) -> Self {
60
29
// Source entropy from thread local user-space RNG instead of
61
30
// system entropy source to reduce overhead when creating many
62
31
// rng instances for many entities at once.
63
- Self ( R :: from_rng ( EntropySource ) . unwrap ( ) )
32
+ Self ( R :: from_rng ( ThreadLocalEntropy ) . unwrap ( ) )
64
33
}
65
34
66
35
#[ inline]
@@ -69,53 +38,13 @@ impl<
69
38
}
70
39
}
71
40
72
- impl <
73
- R : RngCore
74
- + Clone
75
- + Debug
76
- + PartialEq
77
- + Sync
78
- + Send
79
- + Serialize
80
- + for < ' a > Deserialize < ' a >
81
- + ' static ,
82
- > From < R > for EntropyComponent < R >
83
- {
84
- fn from ( value : R ) -> Self {
85
- Self :: new ( value)
86
- }
87
- }
88
-
89
- impl <
90
- R : RngCore
91
- + SeedableRng
92
- + Clone
93
- + Debug
94
- + PartialEq
95
- + Sync
96
- + Send
97
- + Serialize
98
- + for < ' a > Deserialize < ' a >
99
- + ' static ,
100
- > Default for EntropyComponent < R >
101
- {
41
+ impl < R : SeedableEntropySource + ' static > Default for EntropyComponent < R > {
102
42
fn default ( ) -> Self {
103
43
Self :: from_entropy ( )
104
44
}
105
45
}
106
46
107
- impl <
108
- R : RngCore
109
- + Clone
110
- + Debug
111
- + PartialEq
112
- + Sync
113
- + Send
114
- + Serialize
115
- + for < ' a > Deserialize < ' a >
116
- + ' static ,
117
- > RngCore for EntropyComponent < R >
118
- {
47
+ impl < R : EntropySource + ' static > RngCore for EntropyComponent < R > {
119
48
#[ inline]
120
49
fn next_u32 ( & mut self ) -> u32 {
121
50
self . 0 . next_u32 ( )
@@ -137,75 +66,33 @@ impl<
137
66
}
138
67
}
139
68
140
- impl <
141
- R : RngCore
142
- + SeedableRng
143
- + Clone
144
- + Debug
145
- + PartialEq
146
- + Sync
147
- + Send
148
- + Serialize
149
- + for < ' a > Deserialize < ' a >
150
- + ' static ,
151
- > SeedableRng for EntropyComponent < R >
152
- {
69
+ impl < R : SeedableEntropySource + ' static > SeedableRng for EntropyComponent < R > {
153
70
type Seed = R :: Seed ;
154
71
155
72
fn from_seed ( seed : Self :: Seed ) -> Self {
156
73
Self :: new ( R :: from_seed ( seed) )
157
74
}
158
75
}
159
76
160
- impl <
161
- R : RngCore
162
- + SeedableRng
163
- + Clone
164
- + Debug
165
- + PartialEq
166
- + Sync
167
- + Send
168
- + Serialize
169
- + for < ' a > Deserialize < ' a >
170
- + ' static ,
171
- > From < & mut R > for EntropyComponent < R >
172
- {
77
+ impl < R : EntropySource + ' static > From < R > for EntropyComponent < R > {
78
+ fn from ( value : R ) -> Self {
79
+ Self :: new ( value)
80
+ }
81
+ }
82
+
83
+ impl < R : SeedableEntropySource + ' static > From < & mut R > for EntropyComponent < R > {
173
84
fn from ( rng : & mut R ) -> Self {
174
85
Self :: from_rng ( rng) . unwrap ( )
175
86
}
176
87
}
177
88
178
- impl <
179
- R : RngCore
180
- + SeedableRng
181
- + Clone
182
- + Debug
183
- + PartialEq
184
- + Sync
185
- + Send
186
- + Serialize
187
- + for < ' a > Deserialize < ' a >
188
- + ' static ,
189
- > From < & mut Mut < ' _ , R > > for EntropyComponent < R >
190
- {
89
+ impl < R : SeedableEntropySource + ' static > From < & mut Mut < ' _ , R > > for EntropyComponent < R > {
191
90
fn from ( rng : & mut Mut < ' _ , R > ) -> Self {
192
91
Self :: from_rng ( rng. as_mut ( ) ) . unwrap ( )
193
92
}
194
93
}
195
94
196
- impl <
197
- R : RngCore
198
- + SeedableRng
199
- + Clone
200
- + Debug
201
- + PartialEq
202
- + Sync
203
- + Send
204
- + Serialize
205
- + for < ' a > Deserialize < ' a >
206
- + ' static ,
207
- > From < & mut ResMut < ' _ , GlobalEntropy < R > > > for EntropyComponent < R >
208
- {
95
+ impl < R : SeedableEntropySource + ' static > From < & mut ResMut < ' _ , GlobalEntropy < R > > > for EntropyComponent < R > {
209
96
fn from ( rng : & mut ResMut < ' _ , GlobalEntropy < R > > ) -> Self {
210
97
Self :: from_rng ( rng. as_mut ( ) ) . unwrap ( )
211
98
}
0 commit comments