Skip to main content

Crate nexus_id

Crate nexus_id 

Source
Expand description

High-performance unique ID generators for low-latency systems.

§Overview

nexus-id provides unique ID generation optimized for trading systems and other latency-sensitive applications. All generators avoid syscalls on the hot path and produce stack-allocated output.

GeneratorSpeed (p50)Time-orderedOutputUse Case
Snowflake64~22 cyclesYesSnowflakeId64Numeric IDs with extraction
Snowflake32~22 cyclesYesSnowflakeId32Compact numeric IDs
UuidV4~48 cyclesNoUuidRandom unique IDs
UuidV7~62 cyclesYesUuidTime-ordered UUIDs
UlidGenerator~80 cyclesYesUlidSortable 26-char IDs

§ID Types

TypeFormatUse Case
SnowflakeId64Packed u64Numeric IDs with field extraction
MixedId64Fibonacci-mixed u64Identity hasher-safe keys
Uuidxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxStandard UUIDs
UuidCompact32-char hexCompact UUIDs
Ulid26-char Crockford Base32Sortable string IDs
HexId6416-char hexHex-encoded u64
Base62Id11-char alphanumericShort encoded u64
Base36Id13-char alphanumericCase-insensitive u64
TypeIdprefix_suffixDomain-typed sortable IDs

§Parsing

All string types support parsing from strings:

use nexus_id::{Uuid, Ulid, HexId64};

let uuid: Uuid = "01234567-89ab-cdef-fedc-ba9876543210".parse().unwrap();
let hex: HexId64 = "deadbeefcafebabe".parse().unwrap();

§Snowflake ID Newtypes

use nexus_id::{Snowflake64, SnowflakeId64, MixedId64};

let mut generator: Snowflake64<42, 6, 16> = Snowflake64::new(5);

// Typed ID with field extraction
let id: SnowflakeId64<42, 6, 16> = generator.next_id(0).unwrap();
assert_eq!(id.worker(), 5);
assert_eq!(id.sequence(), 0);

// Mixed for identity hashers (Fibonacci multiply, ~1 cycle)
let mixed: MixedId64<42, 6, 16> = id.mixed();
let recovered = mixed.unmix();
assert_eq!(recovered, id);

§HashMap Usage

Snowflake IDs have poor bit distribution for power-of-2 hash tables. Use either a real hasher or the mixed ID type:

use rustc_hash::FxHashMap;

// Option 1: Use a real hasher with raw IDs
let map: FxHashMap<SnowflakeId64<42, 6, 16>, Order> = FxHashMap::default();

// Option 2: Use mixed IDs with identity hasher (fastest)
let map: HashMap<MixedId64<42, 6, 16>, Order, nohash::BuildNoHashHasher<u64>> = ...;

§Features

FeatureDescription
std (default)UUID/ULID generators, Error impls, from_entropy()
serdeSerialize/Deserialize for all types
uuidInterop with the uuid crate
bytesBufMut writing via the bytes crate

Re-exports§

pub use ulid::UlidGenerator;
pub use uuid::UuidV4;
pub use uuid::UuidV7;

Modules§

ulid
ULID generator (Universally Unique Lexicographically Sortable Identifier).
uuid
UUID generators optimized for high-performance systems.

Structs§

Base36Id
Base36-encoded 64-bit ID.
Base62Id
Base62-encoded 64-bit ID.
HexId64
Hex-encoded 64-bit ID.
MixedId32
32-bit Snowflake ID with Fibonacci-mixed bits.
MixedId64
64-bit Snowflake ID with Fibonacci-mixed bits.
SequenceExhausted
Sequence exhausted within the current tick.
Snowflake
Snowflake ID generator.
SnowflakeId32
32-bit Snowflake ID with compile-time layout.
SnowflakeId64
64-bit Snowflake ID with compile-time layout.
TypeId
Type-prefixed sortable identifier.
Ulid
ULID (Universally Unique Lexicographically Sortable Identifier).
Uuid
UUID in standard dashed format.
UuidCompact
UUID in compact format (no dashes).

Enums§

DecodeError
Error parsing a base-N encoded ID string.
ParseError
Error parsing a fixed-format ID string.
TypeIdParseError
Error parsing or constructing a TypeId.
UuidParseError
Error parsing a UUID string.

Traits§

IdInt
Integer types usable as snowflake IDs.

Type Aliases§

Snowflake32
32-bit snowflake generator.
Snowflake64
64-bit snowflake generator.
SnowflakeSigned32
Signed 32-bit snowflake generator.
SnowflakeSigned64
Signed 64-bit snowflake generator.