#unique-identifier #sortable #timestamp #integer #id #unique-id #counter

no-std scru64

SCRU64: Sortable, Clock-based, Realm-specifically Unique identifier

12 releases (1 stable)

1.0.0 Sep 28, 2023
0.5.0 Sep 23, 2023
0.4.2 Sep 6, 2023
0.4.1 Aug 29, 2023
0.1.0 Mar 15, 2023

#4 in #sortable

Download history 2/week @ 2024-02-24 242/week @ 2024-03-09 12/week @ 2024-03-16 6/week @ 2024-03-30

260 downloads per month

Apache-2.0

83KB
1.5K SLoC

SCRU64: Sortable, Clock-based, Realm-specifically Unique identifier

Crates.io License

SCRU64 ID offers compact, time-ordered unique identifiers generated by distributed nodes. SCRU64 has the following features:

  • 63-bit non-negative integer storable as signed/unsigned 64-bit integer
  • Sortable by generation time (as integer and as text)
  • 12-digit case-insensitive textual representation (Base36)
  • ~38-bit Unix epoch-based timestamp that ensures useful life until year 4261
  • Variable-length node/machine ID and counter fields that share 24 bits
// pass node ID through environment variable
std::env::set_var("SCRU64_NODE_SPEC", "42/8");

// generate a new identifier object
let x = scru64::new();
println!("{}", x); // e.g., "0u2r85hm2pt3"
println!("{}", x.to_u64()); // as a 64-bit unsigned integer

// generate a textual representation directly
println!("{}", scru64::new_string()); // e.g., "0u2r85hm2pt4"

See SCRU64 Specification for details.

SCRU64's uniqueness is realm-specific, i.e., dependent on the centralized assignment of node ID to each generator. If you need decentralized, globally unique time-ordered identifiers, consider SCRU128.

Crate features

Default features:

  • std integrates the library with, among others, the system clock to draw current timestamps. Without std, this crate provides limited functionality available under no_std environments.
  • global_gen (implies std) enables the primary new() and new_string() functions and the process-wide global generator under the hood.

Optional features:

  • serde enables serialization/deserialization via serde.
  • tokio (together with global_gen) enables the tokio::new() and tokio::new_string() functions, the non-blocking counterpart of new() and new_string().

License

Licensed under the Apache License, Version 2.0.

See also

Dependencies

~0–1.4MB
~24K SLoC