Add `Sync::Counter(T)`

#16 · open · 2 comments

View on GitHub ↗

ysbaddaden

Keeping counters is quite common, and it might be interesting to have a safe and simple type. The `T` is because we might want any kind of integer, from Int8 to UInt128 or BigInt, and maybe even Floats or BigDecimal. The type would use the best strategy depending on `T`, from simple relaxed atomics to a mutex. Counters can easily be implemented with an `Atomic(T)` with `#add` and `#sub` but a counter doesn't need memory ordering, and repeating `:relaxed` can quickly become boring, leading apps to implement their own counter.

Comments

straight-shoota

Any type other than primitive integers would be quite a bit complicated because it cannot use simple atomic fetch and add. Might be best to scale down the expectations. At least for an MVP integers should be sufficient. Let's talk about more complex types when there's a clear use case.

ysbaddaden

Primitives being able to use atomics is an optimization. Other types can merely call `+` or `-` in a mutex.