pub struct TypedUuid<T: TypedUuidKind> { /* private fields */ }
Expand description
A UUID with type-level information about what it’s used for.
For more, see the library documentation.
Implementations§
Source§impl<T: TypedUuidKind> TypedUuid<T>
impl<T: TypedUuidKind> TypedUuid<T>
Sourcepub const fn nil() -> Self
pub const fn nil() -> Self
The ‘nil UUID’ (all zeros).
The nil UUID is a special form of UUID that is specified to have all 128 bits set to zero.
§References
Sourcepub const fn max() -> Self
pub const fn max() -> Self
The ‘max UUID’ (all ones).
The max UUID is a special form of UUID that is specified to have all 128 bits set to one.
§References
Sourcepub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: [u8; 8]) -> Self
pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: [u8; 8]) -> Self
Creates a UUID from four field values.
Sourcepub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: [u8; 8]) -> Self
pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: [u8; 8]) -> Self
Creates a UUID from four field values in little-endian order.
The bytes in the d1
, d2
and d3
fields will be flipped to convert into big-endian
order. This is based on the endianness of the UUID, rather than the target environment so
bytes will be flipped on both big and little endian machines.
Sourcepub const fn from_u128_le(value: u128) -> Self
pub const fn from_u128_le(value: u128) -> Self
Creates a UUID from a 128bit value in little-endian order.
The entire value will be flipped to convert into big-endian order. This is based on the endianness of the UUID, rather than the target environment so bytes will be flipped on both big and little endian machines.
Sourcepub const fn from_u64_pair(d1: u64, d2: u64) -> Self
pub const fn from_u64_pair(d1: u64, d2: u64) -> Self
Creates a UUID from two 64bit values.
Sourcepub const fn from_bytes(bytes: Bytes) -> Self
pub const fn from_bytes(bytes: Bytes) -> Self
Creates a UUID using the supplied bytes.
Sourcepub const fn from_bytes_le(bytes: Bytes) -> Self
pub const fn from_bytes_le(bytes: Bytes) -> Self
Creates a UUID using the supplied bytes in little-endian order.
The individual fields encoded in the buffer will be flipped.
Sourcepub const fn get_version_num(&self) -> usize
pub const fn get_version_num(&self) -> usize
Returns the version number of the UUID.
This represents the algorithm used to generate the value.
This method is the future-proof alternative to Self::get_version
.
§References
Sourcepub fn get_version(&self) -> Option<Version>
pub fn get_version(&self) -> Option<Version>
Returns the version of the UUID.
This represents the algorithm used to generate the value.
If the version field doesn’t contain a recognized version then None
is returned. If you’re trying to read the version for a future extension
you can also use Uuid::get_version_num
to unconditionally return a
number. Future extensions may start to return Some
once they’re
standardized and supported.
§References
Sourcepub const fn upcast<U: TypedUuidKind>(self) -> TypedUuid<U>where
T: Into<U>,
pub const fn upcast<U: TypedUuidKind>(self) -> TypedUuid<U>where
T: Into<U>,
Converts the UUID to one with looser semantics.
By default, UUID kinds are considered independent, and conversions
between them must happen via the GenericUuid
interface. But in some
cases, there may be a relationship between two different UUID kinds, and
you may wish to easily convert UUIDs from one kind to another.
Typically, a conversion from TypedUuid<T>
to TypedUuid<U>
is most
useful when T
’s semantics are a superset of U
’s, or in other words,
when every TypedUuid<T>
is logically also a TypedUuid<U>
.
For instance:
- Imagine you have
TypedUuidKind
s for different types of database connections, whereDbConnKind
is the general type andPgConnKind
is a specific kind for Postgres. - Since every Postgres connection is also a database connection,
a cast from
TypedUuid<PgConnKind>
toTypedUuid<DbConnKind>
makes sense. - The inverse cast would not make sense, as a database connection may not necessarily be a Postgres connection.
This interface provides an alternative, safer way to perform this
conversion. Indicate your intention to allow a conversion between kinds
by implementing From<T> for U
, as shown in the example below.
§Examples
use newtype_uuid::{TypedUuid, TypedUuidKind, TypedUuidTag};
// Let's say that these UUIDs represent repositories for different
// version control systems, such that you have a generic RepoKind:
pub enum RepoKind {}
impl TypedUuidKind for RepoKind {
fn tag() -> TypedUuidTag {
const TAG: TypedUuidTag = TypedUuidTag::new("repo");
TAG
}
}
// You also have more specific kinds:
pub enum GitRepoKind {}
impl TypedUuidKind for GitRepoKind {
fn tag() -> TypedUuidTag {
const TAG: TypedUuidTag = TypedUuidTag::new("git_repo");
TAG
}
}
// (and HgRepoKind, JujutsuRepoKind, etc...)
// First, define a `From` impl. This impl indicates your desire
// to convert from one kind to another.
impl From<GitRepoKind> for RepoKind {
fn from(value: GitRepoKind) -> Self {
match value {}
}
}
// Now you can convert between them:
let git_uuid: TypedUuid<GitRepoKind> =
TypedUuid::from_u128(0xe9245204_34ea_4ca7_a1c6_2e94fa49df61);
let repo_uuid: TypedUuid<RepoKind> = git_uuid.upcast();