snof 0.1.0

Unique ID generator
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented1 out of 7 items with examples
  • Size
  • Source code size: 8.29 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 362.26 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 8s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • mellowcoffee/snof
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • mellowcoffee

❄️ snof

snof is a unique ID generator. Loosely based on snowflake ID-s, snof generates 64 bit long identifiers consisting of a 32 bit millisecond-based timestamp, and 22 bits of sequence distinguishing identifiers generated within the same millisecond.

The generator uses atomic operations for tracking state, thus it provides a thread-safe, lock-free way of generating unique ID-s. In case of the sequence being exhausted, or the clock moving backwards, the generator spins until validity is restored.

Usage

use snof::SnowflakeGenerator;

fn main() {
    let generator = Arc::new(SnowflakeGenerator::new());

    let threads: Vec<_> = (0..4).map(|_| {
        let other_generator = Arc::clone(&generator);
        thread::spawn(move || {
            let id = other_generator.generate();
            println!("thread id: {}", id.0);
        })
    }).collect();

    for t in threads { t.join().unwrap(); }
}