domain-key
Type-safe domain identifiers for Rust.
domain-key helps you avoid mixing IDs from different domains.
UserId and OrderId are different types at compile time, even if both wrap numbers or UUIDs.
Install
[]
# Recommended default: DoS-resistant hashing
= { = "0.5.2", = ["secure"] }
# Max performance (requires AES-capable CPU)
# domain-key = { version = "0.5.2", features = ["fast"] }
Quick Start
use ;
;
;
type UserKey = ;
type OrderKey = ;
let user = new?;
let order = new?;
// This does not compile:
// let bad = user == order;
# Ok::
Identifier Types
| Type | Storage | Typical use |
|---|---|---|
Key<D> |
SmartString |
Human-readable validated keys |
Id<D> |
NonZeroU64 |
Numeric DB/entity identifiers |
Uuid<D> |
uuid::Uuid |
Typed UUID identifiers (uuid feature) |
Ulid<D> |
ulid::Ulid |
Typed prefixed ULIDs (ulid feature) |
Macro-first API
use *;
define_id!;
define_uuid!;
define_ulid!;
let user = new.unwrap;
let order = nil;
let exec = new;
assert_eq!;
assert!;
Key Features
- compile-time domain safety
- fast, low-allocation string key storage
- optional typed UUID and ULID wrappers
- optional SQLx + web framework integrations
no_stdsupport for core key logic
Feature Flags
Hash Profiles
secure(recommended):AHashfast:GxHash(AES-required)crypto:Blake3
Identifier Features
uuid,uuid-v4,uuid-v7ulid,ulid-monotonic
Integration Features
sqlx-postgres,sqlx-sqlite,sqlx-mysqlaxumactix-web
SQLx Storage Model
| Type | Postgres | SQLite/MySQL |
|---|---|---|
Key<D> |
string carrier | string carrier |
Id<D> |
BIGINT (i64) |
integer (i64) |
Uuid<D> |
native UUID | UUID string |
Ulid<D> |
native UUID (binary) | prefixed ULID string |
Byte Accessor Note (UUID/ULID)
Uuid<D>usesas_bytes().Ulid<D>now also exposesas_bytes().Ulid<D>::to_bytes()is kept as a deprecated compatibility shim.
Docs
Development
License
MIT. See LICENSE.