[][src]Crate lazy_id

lazy_id::Id is a thread-safe 64-bit id that only initializes itself to a specific value when you use it rather than when you create it.

Why would this be helpful? If you need a unique per-instance Id for your type, usually the approach is a global atomic that you increment each time you allocate an id. The only problem here is that now if you want to store your type in a static of some sort, you need to use OnceCell or lazy_static.

This can be pretty annoying if your API is most useful as a static, and you've been carefully designing to allow static initialization, especially this now means the API you designed forces users to use these libraries around your types.

In my case, I was playing around with low level threading code already, and the extra locking these imposed on all accesses felt like it completely defeated the point of my fancy data structure, and would have shown up in the public api.

Anyway, unlike lazy_static/OnceCell/the hypothetical std::lazy, this crate is entirely lock free, and only uses a few relaxed atomic operations to initialize itself on first access (automatically), and the fast path of reading an already-initialized Id is just a single relaxed load. This is all to say, it's much more efficient than most of the alternatives would be and more efficient than I had expected it to be.

Structs

Id

A thread-safe lazily-initialized 64-bit ID.