| Crates.io | typed-uuid |
| lib.rs | typed-uuid |
| version | 0.2.0 |
| created_at | 2023-01-15 15:20:59.719524+00 |
| updated_at | 2023-06-11 18:41:04.726901+00 |
| description | Typed wrapper around Uuid to disambiguate different Ids |
| homepage | |
| repository | https://github.com/MathiasPius/typed-uuid |
| max_upload_size | |
| id | 759529 |
| size | 21,753 |
Id is a typed wrapper around a uuid::Uuid.
Use it to add type safety and prevent confusion between different kinds of Uuid.
Represent different types of Id to prevent mixups or invalid states. If describing
a unique resource's relationship to another, for example the Role a User has,
the relationship can be expressed as follows:
// Subtype the Id type to specify the version of the Id, instead
// of repeating yourself everywhere.
type Id<T> = typed_uuid::Id<T, typed_uuid::V4>;
struct Relation {
user: Id<User>,
role: Id<Role>,
}
Ids with different T parameter types are incompatible, and cannot be compared.
Attempting to assign an Id<User> to a variable of type Id<Role> is a compilation error.
let user = Id::<User>::new();
let role = Id::<Role>::new();
// Compilation fails here, can't compare Id<User> and Id<Role>
assert_eq!(user, role);
But Ids of the same type work:
let mut first = Id::<User>::new();
let second = Id::<User>::new();
first = second;
assert_eq!(first, second);
When depending on this library, you need to explicitly select the versions of the uuid, you will be using, as well as optionally serde support:
[dependencies.typed-uuid]
version = "*"
default-features = false
features = ["v4", "serde"]