Crate oysterpack_uid

source ·
Expand description

Provides support for universally unique identifiers that confirm to the ULID spec.

Features

  • ULID generation via ULID

  • ULIDs can be associated with a domain. Example domains are user ids, request ids, application ids, service ids, etc.

    • TypedULID<T>
      • the domain is defined by the type system
      • business logic should be working with this strongly typed domain ULID
    • DomainULID<T>
      • the domain is defined explicitly on the struct
      • meant to be used when ULIDs need to be handled in a generic fashion, e.g., event tagging, storing to a database, etc
  • ULIDs are thread safe, i.e., they can be sent across threads

  • ULIDs are lightweight and require no heap allocation

  • ULIDs are serializable via serde

Generating ULIDs

let id = ULID::generate();

Generating TypedULID<T> where T is a struct

struct User;
type UserId = TypedULID<User>;
let id = UserId::generate();

TypedULID<T> where T is a trait

use oysterpack_uid::TypedULID;
trait Foo{}
// Send + Sync are added to the type def in order to satisfy TypedULID type constraints for thread safety,
// i.e., in order to be able to send the TypedULID across threads.
type FooId = TypedULID<dyn Foo + Send + Sync>;
let id = FooId::generate();

Generating DomainULIDs

const DOMAIN: Domain = Domain("Foo");
let id = DomainULID::generate(&DOMAIN);

ULID vs UUID Performance

  • below are the times to generate 1 million ULIDs are on my machine (Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz):
DescriptionTestDuration (ms)
TypedULID generationTypedULID::generate()995
ULID generationulid_str()980
V4 UUID generationuuid::Uuid::new_v4()966
TypedULID encoded as StringTypedULID::generate().to_string()4113
ULID encoded as StringULID::generate().to_string()3271
V4 UUID encoded as Stringuuid::Uuid::new_v4().to_string()6051
Performance Test Summary
  • in terms of raw id generation performance, it’s a draw between ULID and UUID
  • ULID is the clear winner in terms of encoding the identifier as a String

Re-exports

pub use ulid::Domain;
pub use ulid::DomainULID;
pub use ulid::HasDomain;
pub use ulid::TypedULID;
pub use ulid::ULID;

Modules

Provides the ULID functionality.