Skip to content

Commit f2d98ed

Browse files
authored
Provide immutable interface for local counter (tikv#251)
Signed-off-by: Breezewish <[email protected]>
1 parent 9c3d03c commit f2d98ed

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.7.0
4+
5+
- Provide immutable interface for local counters.
6+
37
## 0.6.1
48

59
- Fix compile error when ProtoBuf feature is not enabled (#240).

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prometheus"
3-
version = "0.6.1"
3+
version = "0.7.0"
44
license = "Apache-2.0"
55
keywords = ["prometheus", "metrics"]
66

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The main Structures and APIs are ported from [Go client](https://github.com/prom
1313

1414
```toml
1515
[dependencies]
16-
prometheus = "0.6"
16+
prometheus = "0.7"
1717
```
1818

1919
+ Add this to your crate in `lib.rs`:

src/counter.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::cell::RefCell;
1516
use std::collections::HashMap;
1617
use std::marker::PhantomData;
1718
use std::sync::Arc;
@@ -170,7 +171,7 @@ impl<P: Atomic> GenericCounterVec<P> {
170171
/// and [`LocalIntCounter`](::local::LocalIntCounter).
171172
pub struct GenericLocalCounter<P: Atomic> {
172173
counter: GenericCounter<P>,
173-
val: P::T,
174+
val: RefCell<P::T>,
174175
}
175176

176177
/// An unsync [`Counter`](::Counter).
@@ -184,7 +185,7 @@ impl<P: Atomic> GenericLocalCounter<P> {
184185
fn new(counter: GenericCounter<P>) -> Self {
185186
Self {
186187
counter,
187-
val: P::T::from_i64(0),
188+
val: RefCell::new(P::T::from_i64(0)),
188189
}
189190
}
190191

@@ -194,31 +195,31 @@ impl<P: Atomic> GenericLocalCounter<P> {
194195
///
195196
/// Panics in debug build if the value is < 0.
196197
#[inline]
197-
pub fn inc_by(&mut self, v: P::T) {
198+
pub fn inc_by(&self, v: P::T) {
198199
debug_assert!(v >= P::T::from_i64(0));
199-
self.val += v;
200+
*self.val.borrow_mut() += v;
200201
}
201202

202203
/// Increase the local counter by 1.
203204
#[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);
206207
}
207208

208209
/// Return the local counter value.
209210
#[inline]
210211
pub fn get(&self) -> P::T {
211-
self.val
212+
*self.val.borrow()
212213
}
213214

214215
/// Flush the local metrics to the [`Counter`](::Counter).
215216
#[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) {
218219
return;
219220
}
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);
222223
}
223224
}
224225

@@ -327,8 +328,8 @@ mod tests {
327328
#[test]
328329
fn test_local_counter() {
329330
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();
332333

333334
local_counter1.inc();
334335
local_counter2.inc();
@@ -345,7 +346,7 @@ mod tests {
345346
#[test]
346347
fn test_int_local_counter() {
347348
let counter = IntCounter::new("foo", "bar").unwrap();
348-
let mut local_counter = counter.local();
349+
let local_counter = counter.local();
349350

350351
local_counter.inc();
351352
assert_eq!(local_counter.get(), 1);
@@ -524,7 +525,7 @@ mod tests {
524525
#[should_panic(expected = "assertion failed")]
525526
fn test_local_counter_negative_inc() {
526527
let counter = Counter::new("foo", "bar").unwrap();
527-
let mut local = counter.local();
528+
let local = counter.local();
528529
local.inc_by(-42.0);
529530
}
530531

@@ -541,7 +542,7 @@ mod tests {
541542
#[should_panic(expected = "assertion failed")]
542543
fn test_int_local_counter_negative_inc() {
543544
let counter = IntCounter::new("foo", "bar").unwrap();
544-
let mut local = counter.local();
545+
let local = counter.local();
545546
local.inc_by(-42);
546547
}
547548
}

0 commit comments

Comments
 (0)