@@ -84,6 +84,11 @@ impl<N, A: Atomic<N>> Counter<N, A> {
8484 self . value . inc_by ( v)
8585 }
8686
87+ /// Resets the [`Counter`] to `0`, returning the previous value.
88+ pub fn reset ( & self ) -> N {
89+ self . value . reset ( )
90+ }
91+
8792 /// Get the current value of the [`Counter`].
8893 pub fn get ( & self ) -> N {
8994 self . value . get ( )
@@ -110,6 +115,9 @@ pub trait Atomic<N> {
110115 /// Increase the value.
111116 fn inc_by ( & self , v : N ) -> N ;
112117
118+ /// Reset the value to `0`.
119+ fn reset ( & self ) -> N ;
120+
113121 /// Get the the value.
114122 fn get ( & self ) -> N ;
115123}
@@ -124,6 +132,10 @@ impl Atomic<u64> for AtomicU64 {
124132 self . fetch_add ( v, Ordering :: Relaxed )
125133 }
126134
135+ fn reset ( & self ) -> u64 {
136+ self . swap ( Default :: default ( ) , Ordering :: Relaxed )
137+ }
138+
127139 fn get ( & self ) -> u64 {
128140 self . load ( Ordering :: Relaxed )
129141 }
@@ -138,6 +150,10 @@ impl Atomic<u32> for AtomicU32 {
138150 self . fetch_add ( v, Ordering :: Relaxed )
139151 }
140152
153+ fn reset ( & self ) -> u32 {
154+ self . swap ( Default :: default ( ) , Ordering :: Relaxed )
155+ }
156+
141157 fn get ( & self ) -> u32 {
142158 self . load ( Ordering :: Relaxed )
143159 }
@@ -164,6 +180,10 @@ impl Atomic<f64> for AtomicU64 {
164180 old_f64
165181 }
166182
183+ fn reset ( & self ) -> f64 {
184+ f64:: from_bits ( self . swap ( Default :: default ( ) , Ordering :: Relaxed ) )
185+ }
186+
167187 fn get ( & self ) -> f64 {
168188 f64:: from_bits ( self . load ( Ordering :: Relaxed ) )
169189 }
@@ -231,6 +251,14 @@ mod tests {
231251 assert_eq ! ( 1 , counter. get( ) ) ;
232252 }
233253
254+ #[ test]
255+ fn inc_reset_and_get ( ) {
256+ let counter: Counter = Counter :: default ( ) ;
257+ assert_eq ! ( 0 , counter. inc( ) ) ;
258+ assert_eq ! ( 1 , counter. reset( ) ) ;
259+ assert_eq ! ( 0 , counter. get( ) ) ;
260+ }
261+
234262 #[ cfg( not( any( target_arch = "mips" , target_arch = "powerpc" ) ) ) ]
235263 #[ test]
236264 fn f64_stored_in_atomic_u64 ( ) {
0 commit comments