Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ impl<P: Atomic> GenericCounter<P> {
self.v.inc();
}

/// Set the counter to an arbitrary value.
#[inline]
pub fn set(&self, v: P::T) {
#[cfg(debug_assertions)]
let old = self.get();

self.v.set(v);
debug_assert!(self.get() >= old);
}

/// Return the counter value.
#[inline]
pub fn get(&self) -> P::T {
Expand Down Expand Up @@ -595,4 +605,20 @@ mod tests {
let local = counter.local();
local.inc_by(-42);
}

#[test]
fn test_int_counter_set() {
let counter = IntCounter::new("foo", "bar").unwrap();
counter.set(42);
assert_eq!(counter.get(), 42);
}

#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "assertion failed")]
fn test_int_counter_set_decrease() {
let counter = IntCounter::new("foo", "bar").unwrap();
counter.inc_by(69);
counter.set(42);
}
}