pub struct Uuid(/* private fields */);Expand description
A 128-bit UUID (RFC 9562).
Implementations§
Source§impl Uuid
impl Uuid
Sourcepub fn new_v4() -> Uuid
Available on crate feature std only.
pub fn new_v4() -> Uuid
std only.Generate a new version 4 (random) UUID.
§Examples
use uuid::{Uuid, Version};
let uuid = Uuid::new_v4();
assert_eq!(uuid.version(), Version::V4);Sourcepub fn new_v7() -> Uuid
Available on crate feature std only.
pub fn new_v7() -> Uuid
std only.Generate a new version 7 UUID with a 32-bit monotonic counter.
The 48-bit timestamp is milliseconds since the Unix epoch.
A 32-bit monotonic counter occupies rand_a (12 bits) and the
most-significant 20 bits of rand_b, guaranteeing up to
2³² UUIDs within a single millisecond per thread (per
RFC 9562 §6.2, Method 1).
The counter is seeded with 31 random bits each time the timestamp advances and incremented on repeated calls within the same millisecond. If the counter does overflow, this function spin-waits for the next millisecond tick.
The 128-bit layout follows RFC 9562:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| unix_ts_ms |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| unix_ts_ms | ver | rand_a |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var| rand_b |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| rand_b |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+The 32-bit counter spans rand_a (12 bits) and the most-
significant 20 bits of rand_b (bytes 6–10).
§Panics
Panics if the system clock is before the Unix epoch.
§Examples
use uuid::{Uuid, Version};
let uuid = Uuid::new_v7();
assert_eq!(uuid.version(), Version::V7);Source§impl Uuid
impl Uuid
Sourcepub fn parse(input: impl AsRef<[u8]>) -> Result<Uuid, Error>
pub fn parse(input: impl AsRef<[u8]>) -> Result<Uuid, Error>
Parse a UUID from its canonical 8-4-4-4-12 hexadecimal string form.
Accepts both lowercase and uppercase hex characters.
§Errors
Returns Error::InvalidUuid if the input is not exactly 36 characters,
contains misplaced hyphens, or has non-hexadecimal characters.
§Examples
use uuid::Uuid;
let uuid = Uuid::parse("f47ac10b-58cc-4372-a567-0e02b2c3d479").unwrap();
assert_eq!(uuid.to_string(), "f47ac10b-58cc-4372-a567-0e02b2c3d479");Sourcepub const fn from_bytes(bytes: [u8; 16]) -> Uuid
pub const fn from_bytes(bytes: [u8; 16]) -> Uuid
Create a UUID from a 16-byte array.
§Examples
use uuid::Uuid;
let uuid = Uuid::from_bytes([0; 16]);
assert_eq!(uuid, Uuid::nil());Sourcepub fn from_slice(bytes: &[u8]) -> Result<Uuid, Error>
pub fn from_slice(bytes: &[u8]) -> Result<Uuid, Error>
Create a UUID from a byte slice of length 16.
§Errors
Returns Error::InvalidUuid if the slice is not exactly 16 bytes.
§Examples
use uuid::Uuid;
let uuid = Uuid::from_slice(&[0; 16]).unwrap();
assert_eq!(uuid, Uuid::nil());Sourcepub const fn as_bytes(&self) -> [u8; 16]
pub const fn as_bytes(&self) -> [u8; 16]
Return the 16-byte array representation.
§Examples
use uuid::Uuid;
let uuid = Uuid::nil();
assert_eq!(uuid.as_bytes(), [0; 16]);Sourcepub const fn from_u128(v: u128) -> Uuid
pub const fn from_u128(v: u128) -> Uuid
Create a UUID from a u128 value (big-endian).
§Examples
use uuid::Uuid;
let uuid = Uuid::from_u128(0);
assert_eq!(uuid, Uuid::nil());Sourcepub const fn as_u128(&self) -> u128
pub const fn as_u128(&self) -> u128
Return the UUID as a u128 value (big-endian).
§Examples
use uuid::Uuid;
let uuid = Uuid::max();
assert_eq!(uuid.as_u128(), !0);Sourcepub const fn nil() -> Uuid
pub const fn nil() -> Uuid
The Nil UUID: all 128 bits set to zero.
§Examples
use uuid::Uuid;
assert_eq!(Uuid::nil().to_string(), "00000000-0000-0000-0000-000000000000");Sourcepub const fn max() -> Uuid
pub const fn max() -> Uuid
The Max UUID: all 128 bits set to one.
§Examples
use uuid::Uuid;
assert_eq!(Uuid::max().to_string(), "ffffffff-ffff-ffff-ffff-ffffffffffff");Sourcepub fn timestamp(&self) -> Option<u64>
pub fn timestamp(&self) -> Option<u64>
Return the Unix millisecond timestamp embedded in a UUIDv7.
Only UUID version 7 (Unix Epoch time-based) carries a meaningful
timestamp. All other versions return None.
The timestamp is a 48-bit value representing milliseconds since the Unix epoch (1970-01-01 00:00:00 UTC).
§Examples
use uuid::Uuid;
let uuid = Uuid::nil();
assert_eq!(uuid.timestamp(), None);Trait Implementations§
impl Copy for Uuid
Source§impl<'de> Deserialize<'de> for Uuid
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Uuid
serde only.Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Uuid, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Uuid, D::Error>
Source§impl Encode<'_, Postgres> for Uuid
Available on crate feature sqlx only.
impl Encode<'_, Postgres> for Uuid
sqlx only.Source§fn encode_by_ref(
&self,
buf: &mut PgArgumentBuffer,
) -> Result<IsNull, BoxDynError>
fn encode_by_ref( &self, buf: &mut PgArgumentBuffer, ) -> Result<IsNull, BoxDynError>
§fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer,
) -> Result<IsNull, Box<dyn Error + Send + Sync>>where
Self: Sized,
fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer,
) -> Result<IsNull, Box<dyn Error + Send + Sync>>where
Self: Sized,
self into buf in the expected format for the database.fn produces(&self) -> Option<<DB as Database>::TypeInfo>
fn size_hint(&self) -> usize
impl Eq for Uuid
Source§impl PgHasArrayType for Uuid
Available on crate feature sqlx only.
impl PgHasArrayType for Uuid
sqlx only.fn array_type_info() -> PgTypeInfo
fn array_compatible(ty: &PgTypeInfo) -> bool
impl StructuralPartialEq for Uuid
Auto Trait Implementations§
impl Freeze for Uuid
impl RefUnwindSafe for Uuid
impl Send for Uuid
impl Sync for Uuid
impl Unpin for Uuid
impl UnsafeUnpin for Uuid
impl UnwindSafe for Uuid
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more