rasa/counter

Atomic 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 nanosecond-precision monotonic time values, or new to supply a custom function.

Types

A counter that produces integer values.

pub opaque type Counter

The time unit for a monotonic counter. This is passed directly to Erlang’s monotonic_time.

pub type TimeUnit {
  Second
  Millisecond
  Microsecond
  Nanosecond
  Native
}

Constructors

  • Second

    Monotonic time in seconds.

  • Millisecond

    Monotonic time in milliseconds.

  • Microsecond

    Monotonic time in microseconds.

  • Nanosecond

    Monotonic time in nanoseconds.

  • Native

    Native time unit used by the erlang runtime system.

Values

pub fn atomic() -> Counter

Returns an atomic Counter that increases by 1 every time it’s passed to next.

pub fn monotonic(unit: 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() -> Result(Int, Nil)) -> 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) -> Result(Int, Nil)

Returns the next value from the Counter.

Search Document