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, 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 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