rasa/counter

Counters for generating sequential integer values. Counters are used by rasa/queue to index entries but can also be used on their own. Use atomic for a simple incrementing counter, from_atomic for a custom atomic counter, monotonic for guaranteed unique monotonic values, monotonic_time for time-based monotonic values, or new to supply a custom function.

Types

A counter that produces integer values.

pub opaque type Counter

Values

pub fn atomic() -> Counter

Returns an atomic Counter that increases by 1 every time it’s passed to next. Backed by rasa/atomic, each call to next is a single atomic add-and-get operation with no race conditions.

pub fn from_atomic(
  atomic: atomic.Atomic,
  handle_next: fn(atomic.Atomic) -> Int,
) -> Counter

Creates a Counter from an existing Atomic and a function describing how to produce the next value from it. The handle_next function receives the Atomic and is called each time the counter is passed to next.

Useful for custom step sizes or starting values, or when you need to retain a reference to the underlying Atomic (e.g. to read or reset it):

let a = atomic.new()
let c = counter.from_atomic(a, atomic.add_get(_, 2))
pub fn monotonic() -> Counter

Returns a Counter backed by strictly monotonically increasing unique integers. Unlike monotonic_time, consecutive calls to next are guaranteed to produce strictly increasing values. Backed by erlang’s unique_integer/1, these are more expensive to call than monotonic_time.

pub fn monotonic_time(unit: monotonic.TimeUnit) -> Counter

Returns a Counter tied to erlang’s monotonic_time. This counter will provide monotonically increasing time values, but consecutive calls to next may return the same result.

pub fn new(handle_next: fn() -> Int) -> Counter

Creates a Counter from a custom function. The function is called each time the counter is passed to counter.next.

pub fn next(counter: Counter) -> Int

Returns the next value from the Counter.

Search Document