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 fn as_fields(&self) -> (u32, u16, u16, &[u8; 8])
pub fn as_fields(&self) -> (u32, u16, u16, &[u8; 8])
Returns the four field values of the UUID.
These values can be passed to Self::from_fields to reconstruct the
original UUID. The first field represents the initial eight hex digits
as a big-endian u32. The second and third fields represent subsequent
hex digit groups as u16 values. The final field contains the last two
groups of hex digits as an 8-byte array.
§Examples
let uuid: TypedUuid<ExampleKind> =
"a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8".parse().unwrap();
assert_eq!(
uuid.as_fields(),
(
0xa1a2a3a4,
0xb1b2,
0xc1c2,
&[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8],
)
);Sourcepub fn to_fields_le(&self) -> (u32, u16, u16, &[u8; 8])
pub fn to_fields_le(&self) -> (u32, u16, u16, &[u8; 8])
Returns the four field values in little-endian order.
The bytes within integer fields are converted from 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.
§Examples
let uuid: TypedUuid<ExampleKind> =
"a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8".parse().unwrap();
assert_eq!(
uuid.to_fields_le(),
(
0xa4a3a2a1,
0xb2b1,
0xc2c1,
&[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8],
)
);Sourcepub const fn as_u128(&self) -> u128
pub const fn as_u128(&self) -> u128
Returns a 128-bit value containing the UUID bytes.
§Examples
let uuid: TypedUuid<ExampleKind> =
"a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8".parse().unwrap();
assert_eq!(
uuid.as_u128(),
0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8u128,
);Sourcepub fn to_u128_le(&self) -> u128
pub fn to_u128_le(&self) -> u128
Returns a 128-bit little-endian value.
The bytes in the u128 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.
Note that this will produce a different result than
Self::to_fields_le, because the entire UUID is reversed, rather than
reversing the individual fields in-place.
§Examples
let uuid: TypedUuid<ExampleKind> =
"a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8".parse().unwrap();
assert_eq!(
uuid.to_u128_le(),
0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1u128,
);Sourcepub const fn as_u64_pair(&self) -> (u64, u64)
pub const fn as_u64_pair(&self) -> (u64, u64)
Returns two 64-bit values representing the UUID.
The first u64 contains the most significant 64 bits; the second
contains the least significant bits.
§Examples
let uuid: TypedUuid<ExampleKind> =
"a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8".parse().unwrap();
assert_eq!(
uuid.as_u64_pair(),
(0xa1a2a3a4b1b2c1c2, 0xd1d2d3d4d5d6d7d8),
);Sourcepub const fn as_bytes(&self) -> &Bytes
pub const fn as_bytes(&self) -> &Bytes
Returns a slice of 16 octets containing the value.
This method borrows the underlying byte value of the UUID.
§Examples
let bytes = [
0xa1, 0xa2, 0xa3, 0xa4,
0xb1, 0xb2,
0xc1, 0xc2,
0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
];
let uuid = TypedUuid::<ExampleKind>::from_bytes(bytes);
let bytes2 = uuid.as_bytes();
assert_eq!(&bytes, bytes2);Sourcepub const fn into_bytes(self) -> Bytes
pub const fn into_bytes(self) -> Bytes
Consumes self and returns the underlying byte value of the UUID.
§Examples
let bytes = [
0xa1, 0xa2, 0xa3, 0xa4,
0xb1, 0xb2,
0xc1, 0xc2,
0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
];
let uuid = TypedUuid::<ExampleKind>::from_bytes(bytes);
assert_eq!(bytes, uuid.into_bytes());Sourcepub fn to_bytes_le(&self) -> Bytes
pub fn to_bytes_le(&self) -> Bytes
Returns the bytes of the UUID in little-endian order.
The bytes will be flipped to convert into little-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.
§Examples
let uuid: TypedUuid<ExampleKind> =
"a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8".parse().unwrap();
assert_eq!(
uuid.to_bytes_le(),
[
0xa4, 0xa3, 0xa2, 0xa1,
0xb2, 0xb1,
0xc2, 0xc1,
0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
]
);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
TypedUuidKinds for different types of database connections, whereDbConnKindis the general type andPgConnKindis 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();Trait Implementations§
Source§impl<T> Arbitrary for TypedUuid<T>where
T: TypedUuidKind,
Generates random TypedUuid<T> instances.
impl<T> Arbitrary for TypedUuid<T>where
T: TypedUuidKind,
Generates random TypedUuid<T> instances.
Currently, this always returns a version 4 UUID. Support for other kinds
of UUIDs might be added via Self::Parameters in the future.
Source§type Parameters = TypedUuidParams
type Parameters = TypedUuidParams
arbitrary_with accepts for configuration
of the generated Strategy. Parameters must implement Default.Source§type Strategy = BoxedStrategy<TypedUuid<T>>
type Strategy = BoxedStrategy<TypedUuid<T>>
Strategy used to generate values of type Self.Source§fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy
Source§impl<T: TypedUuidKind> Clone for TypedUuid<T>
impl<T: TypedUuidKind> Clone for TypedUuid<T>
Source§impl<T: TypedUuidKind> Debug for TypedUuid<T>
impl<T: TypedUuidKind> Debug for TypedUuid<T>
Source§impl<T: TypedUuidKind> Default for TypedUuid<T>
impl<T: TypedUuidKind> Default for TypedUuid<T>
Source§impl<'de, T: TypedUuidKind> Deserialize<'de> for TypedUuid<T>
impl<'de, T: TypedUuidKind> Deserialize<'de> for TypedUuid<T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: TypedUuidKind> Display for TypedUuid<T>
impl<T: TypedUuidKind> Display for TypedUuid<T>
Source§impl<T: TypedUuidKind> FromStr for TypedUuid<T>
impl<T: TypedUuidKind> FromStr for TypedUuid<T>
Source§impl<T: TypedUuidKind> GenericUuid for TypedUuid<T>
impl<T: TypedUuidKind> GenericUuid for TypedUuid<T>
Source§impl<T: TypedUuidKind> Hash for TypedUuid<T>
impl<T: TypedUuidKind> Hash for TypedUuid<T>
Source§impl<T> JsonSchema for TypedUuid<T>where
T: TypedUuidKind + JsonSchema,
Implements JsonSchema for TypedUuid<T>, if T implements JsonSchema.
impl<T> JsonSchema for TypedUuid<T>where
T: TypedUuidKind + JsonSchema,
Implements JsonSchema for TypedUuid<T>, if T implements JsonSchema.
schema_nameis set to"TypedUuidFor", concatenated by the schema name ofT.schema_idis set toformat!("newtype_uuid::TypedUuid<{}>", T::schema_id()).json_schemais the same as the one forUuid, with thex-rust-typeextension to allow automatic replacement in typify and progenitor.
Source§fn schema_name() -> String
fn schema_name() -> String
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref keyword. Read more