Expand description
Snowflake-style IDs as distinct, validated newtypes.
Declare an ID type with #[typedflake], install a node
once with init, and generate IDs anywhere:
use typedflake::typedflake;
#[typedflake(epoch = "2025-01-01")]
pub struct UserId(i64);
#[typedflake(epoch = "2025-01-01")]
pub struct OrderId(i64);
// Once, during application startup.
typedflake::init(17)?;
// Anywhere in the application.
let user_id = UserId::generate()?;
let order_id = OrderId::generate()?;
assert_eq!(user_id.parts().node, 17);
assert_eq!(order_id.to_string().parse::<OrderId>()?, order_id);UserId and OrderId are separate types: they cannot be mixed up, and each
keeps its own generation state.
§Uniqueness
IDs of one type are unique as long as no two running processes use the same node for it, and the system clock does not move backwards across a restart. This crate does not assign nodes or coordinate deployments.
§Features
| Feature | Enables |
|---|---|
serde | Serde and SerdeEncoded derives |
sqlx-postgres | SqlxPostgres derive |
postgres | Postgres derive for postgres-types |
tokio | generate_async methods |
Structs§
- Alphabet
- The characters an ID is written with, one per digit.
- BitLayout
- Number of bits given to each part of an ID.
- Encoded
- An ID written in its alphabet. Returned by
encode. - Epoch
- Instant that ID timestamps are measured from.
- Format
- Persistent format of an ID type: its epoch and bit allocation.
- Generator
- Generates IDs of type
Ifor one node. - Node
Error - A node field holds a value too large for its width.
- Parts
- The timestamp, node, and sequence of an ID.
- Timestamp
Error - An ID’s creation time does not fit in a
u64of Unix milliseconds.
Enums§
- Decode
IdError - A string could not be decoded as an ID.
- Format
Error - Reason a
Formatcannot be used by an ID type. - Generate
Error - An ID could not be generated.
- Generator
Error - A generator could not be created for a node.
- Init
Error - A default node could not be installed.
- Invalid
Id - A raw value or set of parts is not a valid ID for the format.
- Parse
IdError - A string could not be parsed as an ID.
Traits§
- Encoded
Id - An ID type with an alphabet, declared with the
alphabetoption of#[typedflake]. - Id
- An ID type declared with
#[typedflake]. - Node
- A value that fills the node field of an ID.
- Repr
- Integer an ID is stored in:
i64oru64.
Functions§
- init
- Installs the node used by the static
generatemethods of every ID type with this kind of node.
Attribute Macros§
- typedflake
- Declares a Snowflake-style ID type.
Derive Macros§
- Postgres
- Implements
postgres-types’ToSqlandFromSqlfor ani64ID type, stored asBIGINT. - Serde
- Implements Serde’s
SerializeandDeserializefor an ID type. - Serde
Encoded - Implements Serde’s
SerializeandDeserializefor an ID type, using its encoded text. - Sqlx
Postgres - Implements SQLx’s PostgreSQL
Type,Encode,Decode, and array support for ani64ID type, stored asBIGINT. - Typed
Node - Splits the node field of an ID into named parts.