@@ -28,8 +28,10 @@ use cfg_if::cfg_if;
28
28
cfg_if ! {
29
29
if #[ cfg( target_has_atomic = "64" ) ] {
30
30
type AtomicTick = core:: sync:: atomic:: AtomicU64 ;
31
+ type AtomicTickValue = u64 ;
31
32
} else {
32
33
type AtomicTick = core:: sync:: atomic:: AtomicU32 ;
34
+ type AtomicTickValue = u32 ;
33
35
const ATOMIC_PANIC_THRESHOLD : u32 = u32 :: MAX ;
34
36
}
35
37
}
@@ -51,9 +53,18 @@ impl Display for Tick {
51
53
}
52
54
}
53
55
56
+ impl Default for Tick {
57
+ #[ inline( always) ]
58
+ fn default ( ) -> Self {
59
+ Tick :: new ( )
60
+ }
61
+ }
62
+
54
63
impl Tick {
55
64
#[ inline( always) ]
56
- pub fn new ( ) -> Self {
65
+ pub const fn new ( ) -> Self {
66
+ // Safety:
67
+ // `TICK_BASE_VALUE` is nonzero
57
68
Tick ( unsafe { NonZeroU64 :: new_unchecked ( TICK_BASE_VALUE ) } )
58
69
}
59
70
}
@@ -77,9 +88,16 @@ impl Tick {
77
88
#[ derive( Clone , Copy , Debug ) ]
78
89
pub struct SmallTick ( pub u32 ) ;
79
90
91
+ impl Default for SmallTick {
92
+ #[ inline( always) ]
93
+ fn default ( ) -> Self {
94
+ SmallTick :: new ( )
95
+ }
96
+ }
97
+
80
98
impl SmallTick {
81
99
#[ inline( always) ]
82
- pub fn new ( ) -> Self {
100
+ pub const fn new ( ) -> Self {
83
101
SmallTick ( 0 )
84
102
}
85
103
}
@@ -121,15 +139,15 @@ impl SmallTick {
121
139
/// [`TickCounter::maintain`] returns `true`.
122
140
#[ inline( always) ]
123
141
pub fn maintain ( & mut self , current : Tick ) {
124
- let age = ( current. 0 . get ( ) as u32 ) . wrapping_sub ( self . 0 . into ( ) ) ;
142
+ let age = ( current. 0 . get ( ) as u32 ) . wrapping_sub ( self . 0 ) ;
125
143
if age > MAX_CHANGE_AGE {
126
144
self . 0 = ( current. 0 . get ( ) as u32 ) . wrapping_sub ( MAX_CHANGE_AGE ) ;
127
145
}
128
146
}
129
147
130
148
#[ cfg( test) ]
131
149
fn needs_maintenance ( & self , current : Tick ) -> bool {
132
- let age = ( current. 0 . get ( ) as u32 ) . wrapping_sub ( self . 0 . into ( ) ) ;
150
+ let age = ( current. 0 . get ( ) as u32 ) . wrapping_sub ( self . 0 ) ;
133
151
age > MAX_CHANGE_AGE
134
152
}
135
153
}
@@ -154,6 +172,14 @@ pub struct TickCounter {
154
172
last_maintenance : u64 ,
155
173
}
156
174
175
+ impl Default for TickCounter {
176
+ #[ inline( always) ]
177
+ fn default ( ) -> Self {
178
+ TickCounter :: new ( )
179
+ }
180
+ }
181
+
182
+ // #[allow(clippy::useless_conversion)]
157
183
impl TickCounter {
158
184
#[ inline]
159
185
pub fn new ( ) -> TickCounter {
@@ -181,10 +207,10 @@ impl TickCounter {
181
207
pub fn maintain ( & mut self ) -> bool {
182
208
#[ cfg( not( target_has_atomic = "64" ) ) ]
183
209
{
184
- self . offset += std:: mem:: replace ( self . atomic . get_mut ( ) , 0 ) ;
210
+ self . offset += std:: mem:: replace ( self . atomic . get_mut ( ) , 0 ) as u64 ;
185
211
}
186
212
187
- let atomic = u64 :: from ( * self . atomic . get_mut ( ) ) ;
213
+ let atomic = * self . atomic . get_mut ( ) ;
188
214
let tick_index = self . tick_index ( atomic) ;
189
215
if tick_index > self . last_maintenance + CHECK_TICK_THRESHOLD as u64 {
190
216
self . last_maintenance = tick_index;
@@ -197,43 +223,41 @@ impl TickCounter {
197
223
#[ inline( always) ]
198
224
pub fn current ( & self ) -> Tick {
199
225
let atomic = self . atomic . load ( Relaxed ) ;
200
- self . make_tick ( atomic. into ( ) )
226
+ self . make_tick ( atomic)
201
227
}
202
228
203
229
#[ inline( always) ]
204
230
pub fn current_mut ( & mut self ) -> Tick {
205
231
let atomic = * self . atomic . get_mut ( ) ;
206
- self . make_tick ( atomic. into ( ) )
232
+ self . make_tick ( atomic)
207
233
}
208
234
209
235
#[ inline( always) ]
210
236
pub fn next ( & self ) -> Tick {
211
237
let atomic = self . atomic . fetch_add ( 1 , Relaxed ) ;
212
- self . make_tick ( atomic. into ( ) )
238
+ self . make_tick ( atomic)
213
239
}
214
240
215
241
#[ inline]
216
- fn tick_index ( & self , atomic : u64 ) -> u64 {
242
+ fn tick_index ( & self , atomic : AtomicTickValue ) -> u64 {
217
243
cfg_if ! {
218
244
if #[ cfg( target_has_atomic = "64" ) ] {
219
245
atomic
220
246
} else {
221
- if atomic >= ATOMIC_PANIC_THRESHOLD as u64 {
247
+ if atomic >= ATOMIC_PANIC_THRESHOLD {
222
248
panic!( "Too many `TickCounter::next` calls between `TickCounter::maintain` calls" )
223
249
}
224
- self . offset + atomic
250
+ self . offset + atomic as u64
225
251
}
226
252
}
227
253
}
228
254
229
255
#[ inline( always) ]
230
- fn make_tick ( & self , atomic : u64 ) -> Tick {
256
+ fn make_tick ( & self , atomic : AtomicTickValue ) -> Tick {
231
257
let index = self . tick_index ( atomic) ;
232
- Tick ( unsafe {
233
- // # Safety
234
- // `TICK_BASE_VALUE` is nonzero
235
- NonZeroU64 :: new ( TICK_BASE_VALUE | index) . unwrap_unchecked ( )
236
- } )
258
+ // Safety:
259
+ // `TICK_BASE_VALUE` is nonzero
260
+ Tick ( unsafe { NonZeroU64 :: new ( TICK_BASE_VALUE | index) . unwrap_unchecked ( ) } )
237
261
}
238
262
239
263
#[ cfg( test) ]
0 commit comments