6 releases

0.3.0 Feb 14, 2024
0.2.1 Nov 26, 2023
0.1.2 Nov 26, 2023

#182 in Configuration

27 downloads per month

MIT/Apache

17KB
217 lines

lid (Lily's ID)

MSRV: 1.60.0

I wanted something that I could trust with being unique enough, while also being extremely fast and light on system resources.

My initial usecase for this was an ID that was unique and could be generated by multiple services without worry, I didn't need it to be unguessable, though you can configure it to fit a need where you don't want people guessing IDs.

By default, this uses the Base36 Alphabet with 16 prefix bytes and 12 sequence bytes. This gives (36^28) possible IDs.

Example usage

use lid::configs::{new_distributed, new_random};

fn main() {
    // `new_distributed` gives a low randomness LID instance with the default size of 20 bytes.
    // Don't use it if you don't want people potentially guessing next IDs.
    let mut lid = new_distributed();
    println!("{}", lid.generate());

    // `new_random` gives a high randomness LID instance.
    // Generic parameters are prefix and sequence length, combined is the total size of the id.
    let mut lid = new_random::<6, 6>();
    println!("{}", lid.generate());

    // This uses static Mutex backed instances of the generators above.
    // You must enable the `easy` feature to use these.
    println!("{}", lid::easy::generate_distributed());
    println!("{}", lid::easy::generate_random());
}

Benchmarks

CPU: 7950X3D

LID w/ 12 prefix, 8 sequence, min incr=100, max incr=1000
                        time:   [13.656 ns 13.707 ns 13.783 ns]

LID w/ 16 prefix, 12 sequence, min incr=100, max incr=1000
                        time:   [17.138 ns 17.208 ns 17.328 ns]

LID w/ 12 prefix, 8 sequence, min incr=50_000, max incr=5_000_000
                        time:   [13.781 ns 13.860 ns 13.964 ns]

LID w/ 16 prefix, 12 sequence, min incr=50_000, max incr=5_000_000
                        time:   [15.851 ns 15.886 ns 15.921 ns]

colorid w/ 20 bytes     time:   [405.33 ns 406.75 ns 408.38 ns]
colorid w/ 28 bytes     time:   [508.13 ns 508.84 ns 509.60 ns]

nanoid::nanoid!()       time:   [342.53 ns 343.13 ns 343.74 ns]
nanoid::nanoid!(28)     time:   [366.67 ns 366.89 ns 367.12 ns]

Dependencies

~245–350KB