12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ use std:: cell:: RefCell ;
15
16
use std:: collections:: HashMap ;
16
17
use std:: marker:: PhantomData ;
17
18
use std:: sync:: Arc ;
@@ -170,7 +171,7 @@ impl<P: Atomic> GenericCounterVec<P> {
170
171
/// and [`LocalIntCounter`](::local::LocalIntCounter).
171
172
pub struct GenericLocalCounter < P : Atomic > {
172
173
counter : GenericCounter < P > ,
173
- val : P :: T ,
174
+ val : RefCell < P :: T > ,
174
175
}
175
176
176
177
/// An unsync [`Counter`](::Counter).
@@ -184,7 +185,7 @@ impl<P: Atomic> GenericLocalCounter<P> {
184
185
fn new ( counter : GenericCounter < P > ) -> Self {
185
186
Self {
186
187
counter,
187
- val : P :: T :: from_i64 ( 0 ) ,
188
+ val : RefCell :: new ( P :: T :: from_i64 ( 0 ) ) ,
188
189
}
189
190
}
190
191
@@ -194,31 +195,31 @@ impl<P: Atomic> GenericLocalCounter<P> {
194
195
///
195
196
/// Panics in debug build if the value is < 0.
196
197
#[ inline]
197
- pub fn inc_by ( & mut self , v : P :: T ) {
198
+ pub fn inc_by ( & self , v : P :: T ) {
198
199
debug_assert ! ( v >= P :: T :: from_i64( 0 ) ) ;
199
- self . val += v;
200
+ * self . val . borrow_mut ( ) += v;
200
201
}
201
202
202
203
/// Increase the local counter by 1.
203
204
#[ inline]
204
- pub fn inc ( & mut self ) {
205
- self . val += P :: T :: from_i64 ( 1 ) ;
205
+ pub fn inc ( & self ) {
206
+ * self . val . borrow_mut ( ) += P :: T :: from_i64 ( 1 ) ;
206
207
}
207
208
208
209
/// Return the local counter value.
209
210
#[ inline]
210
211
pub fn get ( & self ) -> P :: T {
211
- self . val
212
+ * self . val . borrow ( )
212
213
}
213
214
214
215
/// Flush the local metrics to the [`Counter`](::Counter).
215
216
#[ inline]
216
- pub fn flush ( & mut self ) {
217
- if self . val == P :: T :: from_i64 ( 0 ) {
217
+ pub fn flush ( & self ) {
218
+ if * self . val . borrow ( ) == P :: T :: from_i64 ( 0 ) {
218
219
return ;
219
220
}
220
- self . counter . inc_by ( self . val ) ;
221
- self . val = P :: T :: from_i64 ( 0 ) ;
221
+ self . counter . inc_by ( * self . val . borrow ( ) ) ;
222
+ * self . val . borrow_mut ( ) = P :: T :: from_i64 ( 0 ) ;
222
223
}
223
224
}
224
225
@@ -327,8 +328,8 @@ mod tests {
327
328
#[ test]
328
329
fn test_local_counter ( ) {
329
330
let counter = Counter :: new ( "counter" , "counter helper" ) . unwrap ( ) ;
330
- let mut local_counter1 = counter. local ( ) ;
331
- let mut local_counter2 = counter. local ( ) ;
331
+ let local_counter1 = counter. local ( ) ;
332
+ let local_counter2 = counter. local ( ) ;
332
333
333
334
local_counter1. inc ( ) ;
334
335
local_counter2. inc ( ) ;
@@ -345,7 +346,7 @@ mod tests {
345
346
#[ test]
346
347
fn test_int_local_counter ( ) {
347
348
let counter = IntCounter :: new ( "foo" , "bar" ) . unwrap ( ) ;
348
- let mut local_counter = counter. local ( ) ;
349
+ let local_counter = counter. local ( ) ;
349
350
350
351
local_counter. inc ( ) ;
351
352
assert_eq ! ( local_counter. get( ) , 1 ) ;
@@ -524,7 +525,7 @@ mod tests {
524
525
#[ should_panic( expected = "assertion failed" ) ]
525
526
fn test_local_counter_negative_inc ( ) {
526
527
let counter = Counter :: new ( "foo" , "bar" ) . unwrap ( ) ;
527
- let mut local = counter. local ( ) ;
528
+ let local = counter. local ( ) ;
528
529
local. inc_by ( -42.0 ) ;
529
530
}
530
531
@@ -541,7 +542,7 @@ mod tests {
541
542
#[ should_panic( expected = "assertion failed" ) ]
542
543
fn test_int_local_counter_negative_inc ( ) {
543
544
let counter = IntCounter :: new ( "foo" , "bar" ) . unwrap ( ) ;
544
- let mut local = counter. local ( ) ;
545
+ let local = counter. local ( ) ;
545
546
local. inc_by ( -42 ) ;
546
547
}
547
548
}
0 commit comments