idgen 0.1.2

Unique 64-bit ID generator based on Twitter Snowflake
Documentation
  • Coverage
  • 0%
    0 out of 5 items documented0 out of 5 items with examples
  • Size
  • Source code size: 7.74 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 254.30 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • abatyuk

Unique ID Generator

Unique 64 bit ID generator inspired by twitter snowflake

By default, settings are:

  • Machine ID - 8 bit (up to 255 machines)
  • Timestamp subset - 41 bits ~69 years since Unix Epoch
  • Remaining bits (15) - sequence number

If sequence number is overflowing, generator will wait in chunks of 100 microseconds until next millisecond.

Can be configured:

  • Machine ID - up to 8 bit
  • Timestamp subset - from 41 to 43 bits (up to 278 years since Unix Epoch)

Usage

Add into your Cargo.toml dependencies:

idgen = "0.1.2"
use idgen::IDGen;

fn main() {
    let idgen = IDGen::new(128);
    println!("{}", idgen.new_id());
}

Alternatively, it can be configured to have more bits for sequence number with less bits for machine ID:

use idgen::IDGen;

fn main() {
    let idgen = IDGen::new_with_config(1, 1, 41);
    println!("{}", idgen.new_id());
}

Notes

  • Performance is "ok" - on MacBook Air 2019 it generates ~3M unique ids per second in single-threaded mode (Overhead for interior mutability/thread safety at least halves the performance)
  • It is thread-safe
  • Strictly speaking, it can be used with less than 41 bits for timestamp (as only last meaningful bits are taken into account)