Skip to main content

Uuid

Struct Uuid 

Source
pub struct Uuid(/* private fields */);
Expand description

A 128-bit UUID (RFC 9562).

Implementations§

Source§

impl Uuid

Source

pub fn new_v4() -> Uuid

Available on crate feature 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);
Source

pub fn new_v7() -> Uuid

Available on crate feature 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

Source

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");
Source

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());
Source

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());
Source

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]);
Source

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());
Source

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);
Source

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");
Source

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");
Source

pub const fn version(&self) -> Version

Return the Version of this UUID.

§Examples
use uuid::{Uuid, Version};

assert_eq!(Uuid::nil().version(), Version::Nil);
assert_eq!(Uuid::max().version(), Version::Max);
Source

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§

Source§

impl Clone for Uuid

Source§

fn clone(&self) -> Uuid

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Uuid

Source§

impl Debug for Uuid

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decode<'_, Postgres> for Uuid

Available on crate feature sqlx only.
Source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

Decode a new value of this type using a raw value from the database.
Source§

impl<'de> Deserialize<'de> for Uuid

Available on crate feature serde only.
Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Uuid, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Uuid

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the UUID as a lowercase 8-4-4-4-12 hyphenated string.

§Examples
let uuid = uuid::Uuid::nil();
assert_eq!(uuid.to_string(), "00000000-0000-0000-0000-000000000000");
Source§

impl Encode<'_, Postgres> for Uuid

Available on crate feature sqlx only.
Source§

fn encode_by_ref( &self, buf: &mut PgArgumentBuffer, ) -> Result<IsNull, BoxDynError>

Writes the value of self into buf without moving self. Read more
§

fn encode( self, buf: &mut <DB as Database>::ArgumentBuffer, ) -> Result<IsNull, Box<dyn Error + Send + Sync>>
where Self: Sized,

Writes the value of self into buf in the expected format for the database.
§

fn produces(&self) -> Option<<DB as Database>::TypeInfo>

§

fn size_hint(&self) -> usize

Source§

impl Eq for Uuid

Source§

impl FromStr for Uuid

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Uuid, Error>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Uuid

Source§

fn eq(&self, other: &Uuid) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PgHasArrayType for Uuid

Available on crate feature sqlx only.
Source§

fn array_type_info() -> PgTypeInfo

§

fn array_compatible(ty: &PgTypeInfo) -> bool

Source§

impl Serialize for Uuid

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Uuid

Source§

impl Type<Postgres> for Uuid

Available on crate feature sqlx only.
Source§

fn type_info() -> PgTypeInfo

Returns the canonical SQL type for this Rust type. Read more
§

fn compatible(ty: &<DB as Database>::TypeInfo) -> bool

Determines if this Rust type is compatible with the given SQL type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more