Skip to content

Commit 009d603

Browse files
committed
feat!(atomic,value,gauge): return old value on increment
Signed-off-by: Luukas Pörtfors <[email protected]>
1 parent 8151418 commit 009d603

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

src/atomic64.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ pub trait Atomic: Send + Sync {
6464
fn set(&self, val: Self::T);
6565
/// Get the value.
6666
fn get(&self) -> Self::T;
67-
/// Increment the value by a given amount.
68-
fn inc_by(&self, delta: Self::T);
69-
/// Decrement the value by a given amount.
70-
fn dec_by(&self, delta: Self::T);
67+
/// Increment the value by a given amount returning the old value.
68+
fn inc_by(&self, delta: Self::T) -> Self::T;
69+
/// Decrement the value by a given amount returning the old value.
70+
fn dec_by(&self, delta: Self::T) -> Self::T;
7171
}
7272

7373
/// A atomic float.
@@ -106,7 +106,7 @@ impl Atomic for AtomicF64 {
106106
}
107107

108108
#[inline]
109-
fn inc_by(&self, delta: Self::T) {
109+
fn inc_by(&self, delta: Self::T) -> Self::T {
110110
loop {
111111
let current = self.inner.load(Ordering::Acquire);
112112
let new = u64_to_f64(current) + delta;
@@ -117,14 +117,14 @@ impl Atomic for AtomicF64 {
117117
Ordering::Relaxed,
118118
);
119119
if result.is_ok() {
120-
return;
120+
return u64_to_f64(current);
121121
}
122122
}
123123
}
124124

125125
#[inline]
126-
fn dec_by(&self, delta: Self::T) {
127-
self.inc_by(-delta);
126+
fn dec_by(&self, delta: Self::T) -> Self::T {
127+
self.inc_by(-delta)
128128
}
129129
}
130130

@@ -161,13 +161,13 @@ impl Atomic for AtomicI64 {
161161
}
162162

163163
#[inline]
164-
fn inc_by(&self, delta: Self::T) {
165-
self.inner.fetch_add(delta, Ordering::Relaxed);
164+
fn inc_by(&self, delta: Self::T) -> Self::T {
165+
self.inner.fetch_add(delta, Ordering::Relaxed)
166166
}
167167

168168
#[inline]
169-
fn dec_by(&self, delta: Self::T) {
170-
self.inner.fetch_sub(delta, Ordering::Relaxed);
169+
fn dec_by(&self, delta: Self::T) -> Self::T {
170+
self.inner.fetch_sub(delta, Ordering::Relaxed)
171171
}
172172
}
173173

@@ -197,13 +197,13 @@ impl Atomic for AtomicU64 {
197197
}
198198

199199
#[inline]
200-
fn inc_by(&self, delta: Self::T) {
201-
self.inc_by_with_ordering(delta, Ordering::Relaxed);
200+
fn inc_by(&self, delta: Self::T) -> Self::T {
201+
self.inc_by_with_ordering(delta, Ordering::Relaxed)
202202
}
203203

204204
#[inline]
205-
fn dec_by(&self, delta: Self::T) {
206-
self.inner.fetch_sub(delta, Ordering::Relaxed);
205+
fn dec_by(&self, delta: Self::T) -> Self::T {
206+
self.inner.fetch_sub(delta, Ordering::Relaxed)
207207
}
208208
}
209209

@@ -229,14 +229,19 @@ impl AtomicU64 {
229229
}
230230

231231
/// Increment the value by a given amount with the provided memory ordering.
232-
pub fn inc_by_with_ordering(&self, delta: u64, ordering: Ordering) {
233-
self.inner.fetch_add(delta, ordering);
232+
pub fn inc_by_with_ordering(&self, delta: u64, ordering: Ordering) -> u64 {
233+
self.inner.fetch_add(delta, ordering)
234234
}
235235

236236
/// Stores a value into the atomic integer, returning the previous value.
237237
pub fn swap(&self, val: u64, ordering: Ordering) -> u64 {
238238
self.inner.swap(val, ordering)
239239
}
240+
241+
/// Returns a reference to the inner atomic u64
242+
pub fn inner(&self) -> &StdAtomicU64 {
243+
&self.inner
244+
}
240245
}
241246

242247
#[cfg(test)]

src/gauge.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ impl<P: Atomic> GenericGauge<P> {
7272
/// Add the given value to the gauge. (The value can be
7373
/// negative, resulting in a decrement of the gauge.)
7474
#[inline]
75-
pub fn add(&self, v: P::T) {
76-
self.v.inc_by(v);
75+
pub fn add(&self, v: P::T) -> P::T {
76+
self.v.inc_by(v)
7777
}
7878

7979
/// Subtract the given value from the gauge. (The value can be
8080
/// negative, resulting in an increment of the gauge.)
8181
#[inline]
82-
pub fn sub(&self, v: P::T) {
83-
self.v.dec_by(v);
82+
pub fn sub(&self, v: P::T) -> P::T {
83+
self.v.dec_by(v)
8484
}
8585

8686
/// Return the gauge value.
@@ -288,4 +288,21 @@ mod tests {
288288
assert!(vec.remove_label_values(&[v1.clone()]).is_err());
289289
assert!(vec.remove_label_values(&[v1.clone(), v3.clone()]).is_err());
290290
}
291+
292+
#[test]
293+
fn test_gauge_inc_sub_return_old() {
294+
let opts = Opts::new("test", "test help")
295+
.const_label("a", "1")
296+
.const_label("b", "2");
297+
298+
let gauge = IntGauge::with_opts(opts).unwrap();
299+
gauge.inc();
300+
assert_eq!(gauge.get(), 1);
301+
let val = gauge.add(20);
302+
assert_eq!(val, 1);
303+
assert_eq!(gauge.get(), 21);
304+
let val = gauge.sub(10);
305+
assert_eq!(val, 21);
306+
assert_eq!(gauge.get(), 11);
307+
}
291308
}

src/value.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ impl<P: Atomic> Value<P> {
6565
}
6666

6767
#[inline]
68-
pub fn inc_by(&self, val: P::T) {
69-
self.val.inc_by(val);
68+
pub fn inc_by(&self, val: P::T) -> P::T {
69+
self.val.inc_by(val)
7070
}
7171

7272
#[inline]
@@ -80,7 +80,7 @@ impl<P: Atomic> Value<P> {
8080
}
8181

8282
#[inline]
83-
pub fn dec_by(&self, val: P::T) {
83+
pub fn dec_by(&self, val: P::T) -> P::T {
8484
self.val.dec_by(val)
8585
}
8686

0 commit comments

Comments
 (0)