#[repr(transparent)]pub struct Uuid(_);
Expand description
Universally Unique Identifier, or UUID.
This type is repr(transparent)
and guaranteed to have the same layout
as [u8; 16]
.
The various methods on Uuid
assume each field
is laid out Most Significant Byte First/MSB/Big-Endian/Network Endian.
This type is also serde(transparent)
, when serde is enabled.
Implementations§
source§impl Uuid
impl Uuid
sourcepub const fn max() -> Self
Available on crate feature experimental_uuid
only.
pub const fn max() -> Self
experimental_uuid
only.The special Max UUID, where all bits are set to one.
sourcepub const fn from_bytes(bytes: Bytes) -> Self
pub const fn from_bytes(bytes: Bytes) -> Self
Create a UUID from bytes.
sourcepub const fn from_bytes_me(bytes: Bytes) -> Self
pub const fn from_bytes_me(bytes: Bytes) -> Self
Create a UUID from mixed-endian bytes.
The resulting UUID will be stored in-memory as big-endian.
This will primarily come up when interacting with Microsoft GUIDs/UUIDs
The following fields are expected to be little-endian instead of big-endian:
time_low
time_mid
time_hi_and_version
Other fields are left unchanged
sourcepub const fn to_bytes_me(self) -> Bytes
pub const fn to_bytes_me(self) -> Bytes
Return the UUID as mixed-endian bytes.
See Uuid::from_bytes_me
for details.
sourcepub const fn variant(self) -> Variant
pub const fn variant(self) -> Variant
The UUID Variant
Warning
Many UUIDs out in the wild are incorrectly generated, so this value can’t be relied upon.
sourcepub const fn version(self) -> Version
pub const fn version(self) -> Version
The UUID Version
For “incorrect” or non-RFC UUIDs, this value may be nonsensical.
If the version bits do not match any known version,
Version::Reserved
is returned instead.
As new versions are recognized, this may change.
Warning
Many UUIDs out in the wild are incorrectly generated, so this value can’t be relied upon.
sourcepub const fn timestamp(self) -> u64
pub const fn timestamp(self) -> u64
The 60-bit UUID timestamp
This value will only make sense for Version::Time
or
Version::Database
UUIDs
The value of this will depend on Uuid::version
sourcepub const fn clock_sequence(self) -> u16
pub const fn clock_sequence(self) -> u16
The 14-bit UUID clock sequence
This value will only make sense for Version::Time
or
Version::Database
UUIDs
The value of this will depend on Uuid::version
sourcepub fn to_str(self, buf: &mut [u8; 36]) -> &mut str
pub fn to_str(self, buf: &mut [u8; 36]) -> &mut str
Write UUID as a lowercase ASCII string into buf
, and returns it as a
string.
Examples
const EXAMPLE_UUID: &str = "662aa7c7-7598-4d56-8bcc-a72c30f998a2";
let uuid = Uuid::parse(EXAMPLE_UUID)?;
let mut buf = [0u8; 36];
let string = uuid.to_str(&mut buf);
assert_eq!(string, EXAMPLE_UUID);
With an array
let uuid = Uuid::new_v4();
let mut buf = [0u8; 36];
let string = uuid.to_str(&mut buf);
With a slice
let uuid = Uuid::new_v4();
let mut data = [0u8; 50];
let string = uuid.to_str((&mut data[..36]).try_into().unwrap());
With a slice, incorrectly.
The problem here is that the slices length is unconstrained, and could be more or less than 36.
let uuid = Uuid::new_v4();
let mut data = [0u8; 50];
let string = uuid.to_str((&mut data[..]).try_into().unwrap());
sourcepub fn to_urn(self, buf: &mut [u8; 45]) -> &mut str
pub fn to_urn(self, buf: &mut [u8; 45]) -> &mut str
Write a UUID as a lowercase ASCII string into buf
, and return it as a
string.
For usage examples see Uuid::to_str
.
sourcepub fn to_str_upper(self, buf: &mut [u8; 36]) -> &mut str
pub fn to_str_upper(self, buf: &mut [u8; 36]) -> &mut str
Uuid::to_str
, but uppercase.
sourcepub fn to_urn_upper(self, buf: &mut [u8; 45]) -> &mut str
pub fn to_urn_upper(self, buf: &mut [u8; 45]) -> &mut str
Uuid::to_urn
, but the UUID is uppercase.
source§impl Uuid
impl Uuid
sourcepub fn parse(s: &str) -> Result<Self, ParseUuidError>
pub fn parse(s: &str) -> Result<Self, ParseUuidError>
Parse a Uuid
from a string
This method is case insensitive and supports the following formats:
urn:uuid:
urn:uuid:662aa7c7-7598-4d56-8bcc-a72c30f998a2
- “Braced”
{662aa7c7-7598-4d56-8bcc-a72c30f998a2}
- “Hyphenate”
662aa7c7-7598-4d56-8bcc-a72c30f998a2
- “Simple”
662aa7c775984d568bcca72c30f998a2
Example
Uuid::parse("662aa7c7-7598-4d56-8bcc-a72c30f998a2").unwrap();
Uuid::parse("662AA7C7-7598-4D56-8BCC-A72C30F998A2").unwrap();
Uuid::parse("urn:uuid:662aa7c7-7598-4d56-8bcc-a72c30f998a2").unwrap();
Uuid::parse("urn:uuid:662AA7C7-7598-4D56-8BCC-A72C30F998A2").unwrap();
Uuid::parse("662aa7c775984d568bcca72c30f998a2").unwrap();
Uuid::parse("662AA7C775984D568BCCA72C30F998A2").unwrap();
Uuid::parse("{662aa7c7-7598-4d56-8bcc-a72c30f998a2}").unwrap();
Uuid::parse("{662AA7C7-7598-4D56-8BCC-A72C30F998A2}").unwrap();
sourcepub fn parse_me(s: &str) -> Result<Self, ParseUuidError>
pub fn parse_me(s: &str) -> Result<Self, ParseUuidError>
Parse a Uuid
from a string that is in mixed-endian
This method is bad and should never be needed, but there are UUIDs in the wild that do this.
These UUIDs are being displayed wrong, but you still need to parse them correctly.
See Uuid::from_bytes_me
for details.
sourcepub fn new_v4() -> Self
Available on crate feature getrandom
only.
pub fn new_v4() -> Self
getrandom
only.Create a new Version 4(Random) UUID.
This requires the getrandom
feature.
If generating a lot of UUID’s very quickly, prefer Uuid::new_v4_rng
.
Example
let uuid = Uuid::new_v4();
sourcepub fn new_v4_rng(rng: &mut Rng) -> Self
pub fn new_v4_rng(rng: &mut Rng) -> Self
Create a new Version 4(Random) UUID, using the provided Rng
This method is useful if you need to generate a lot of UUID’s very quickly, since it won’t create and seed a new RNG each time.
Providing a good seed is left to you, however. If a bad seed is used, the resulting UUIDs may not be sufficiently random or unique.
Example
let mut rng = Rng::from_seed(seed);
for _ in 0..10 {
let uuid = Uuid::new_v4_rng(&mut rng);
}
sourcepub fn new_v3(namespace: Uuid, name: &[u8]) -> Self
pub fn new_v3(namespace: Uuid, name: &[u8]) -> Self
Create a new Version 3 UUID with the provided name and namespace.
Note
Version 3 UUID’s use the obsolete MD5 algorithm,
Uuid::new_v5
should be preferred.
Example
let uuid = Uuid::new_v3(NAMESPACE_DNS, b"example.com");
sourcepub fn new_v5(namespace: Uuid, name: &[u8]) -> Self
pub fn new_v5(namespace: Uuid, name: &[u8]) -> Self
Create a new Version 5 UUID with the provided name and namespace.
Example
let uuid = Uuid::new_v5(NAMESPACE_DNS, b"example.com");
sourcepub fn new_v1(timestamp: u64, counter: u16, node: [u8; 6]) -> Self
pub fn new_v1(timestamp: u64, counter: u16, node: [u8; 6]) -> Self
Create a new Version 1 UUID using the provided 60-bit timestamp, 14-bit counter, and node.
The 4 high bits of timestamp
are ignored
The 2 high bits of counter
are ignored
Example
let uuid = Uuid::new_v1(TIMESTAMP, RANDOM, RANDOM_OR_MAC);
sourcepub fn new_v6(timestamp: u64, counter: u16, node: [u8; 6]) -> Self
Available on crate feature experimental_uuid
only.
pub fn new_v6(timestamp: u64, counter: u16, node: [u8; 6]) -> Self
experimental_uuid
only.Create a new Version 6 UUID
This is identical to Version 1 UUIDs (see Uuid::new_v1
),
except that the timestamp fields are re-ordered
Example
let uuid = Uuid::new_v6(TIMESTAMP, RANDOM, PSEUDO);
Trait Implementations§
source§impl Debug for Uuid
impl Debug for Uuid
Display the Uuid
debug representation
The alternate(#
) flag can be used to get more more detailed debug
information.
Example
let uuid = Uuid::parse("662aa7c7-7598-4d56-8bcc-a72c30f998a2").unwrap();
assert_eq!(format!("{:?}", uuid), "Uuid(662AA7C7-7598-4D56-8BCC-A72C30F998A2)");
assert_eq!(format!("{:#?}", uuid), r#"Uuid(662AA7C7-7598-4D56-8BCC-A72C30F998A2) {
Version: Random(4),
Variant: Rfc4122(1),
}"#);
source§impl Display for Uuid
impl Display for Uuid
source§impl FromStr for Uuid
impl FromStr for Uuid
See Uuid::parse
for details.
§type Err = ParseUuidError
type Err = ParseUuidError
source§impl LowerHex for Uuid
impl LowerHex for Uuid
Display the Uuid
in lowercase
The alternate(#
) flag can be used to get a URN.
Example
let uuid = Uuid::parse("662aa7c7-7598-4d56-8bcc-a72c30f998a2").unwrap();
assert_eq!(format!("{:x}", uuid), "662aa7c7-7598-4d56-8bcc-a72c30f998a2");
assert_eq!(format!("{:#x}", uuid), "urn:uuid:662aa7c7-7598-4d56-8bcc-a72c30f998a2");
source§impl Ord for Uuid
impl Ord for Uuid
source§impl PartialEq<Uuid> for Uuid
impl PartialEq<Uuid> for Uuid
source§impl PartialOrd<Uuid> for Uuid
impl PartialOrd<Uuid> for Uuid
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl UpperHex for Uuid
impl UpperHex for Uuid
Display the Uuid
in uppercase
The alternate(#
) flag can be used to get a URN.
Example
let uuid = Uuid::parse("662aa7c7-7598-4d56-8bcc-a72c30f998a2").unwrap();
assert_eq!(format!("{:X}", uuid), "662AA7C7-7598-4D56-8BCC-A72C30F998A2");
assert_eq!(format!("{:#X}", uuid), "urn:uuid:662AA7C7-7598-4D56-8BCC-A72C30F998A2");