Struct uuid::Uuid

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

A Universally Unique Identifier (UUID).

Implementations§

source§

impl Uuid

source

pub const fn nil() -> Self

The ‘nil UUID’.

The nil UUID is special form of UUID that is specified to have all 128 bits set to zero, as defined in IETF RFC 4122 Section 4.1.7.

Examples

Basic usage:

use uuid::Uuid;

let uuid = Uuid::nil();

assert_eq!(
    uuid.to_hyphenated().to_string(),
    "00000000-0000-0000-0000-000000000000"
);
source

pub fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8]) -> Result<Uuid, Error>

Creates a UUID from four field values in big-endian order.

Errors

This function will return an error if d4’s length is not 8 bytes.

Examples

Basic usage:

use uuid::Uuid;

let d4 = [12, 3, 9, 56, 54, 43, 8, 9];

let uuid = Uuid::from_fields(42, 12, 5, &d4);
let uuid = uuid.map(|uuid| uuid.to_hyphenated().to_string());

let expected_uuid =
    Ok(String::from("0000002a-000c-0005-0c03-0938362b0809"));

assert_eq!(expected_uuid, uuid);
source

pub fn from_fields_le( d1: u32, d2: u16, d3: u16, d4: &[u8] ) -> Result<Uuid, Error>

Creates a UUID from four field values in little-endian order.

The bytes in the d1, d2 and d3 fields will be converted into big-endian order.

Examples
use uuid::Uuid;

let d1 = 0xAB3F1097u32;
let d2 = 0x501Eu16;
let d3 = 0xB736u16;
let d4 = [12, 3, 9, 56, 54, 43, 8, 9];

let uuid = Uuid::from_fields_le(d1, d2, d3, &d4);
let uuid = uuid.map(|uuid| uuid.to_hyphenated().to_string());

let expected_uuid =
    Ok(String::from("97103fab-1e50-36b7-0c03-0938362b0809"));

assert_eq!(expected_uuid, uuid);
source

pub const fn from_u128(v: u128) -> Self

Creates a UUID from a 128bit value in big-endian order.

source

pub const fn from_u128_le(v: u128) -> Self

Creates a UUID from a 128bit value in little-endian order.

source

pub fn from_slice(b: &[u8]) -> Result<Uuid, Error>

Creates a UUID using the supplied big-endian bytes.

Errors

This function will return an error if b has any length other than 16.

Examples

Basic usage:

use uuid::Uuid;

let bytes = [4, 54, 67, 12, 43, 2, 98, 76, 32, 50, 87, 5, 1, 33, 43, 87];

let uuid = Uuid::from_slice(&bytes);
let uuid = uuid.map(|uuid| uuid.to_hyphenated().to_string());

let expected_uuid =
    Ok(String::from("0436430c-2b02-624c-2032-570501212b57"));

assert_eq!(expected_uuid, uuid);

An incorrect number of bytes:

use uuid::Uuid;

let bytes = [4, 54, 67, 12, 43, 2, 98, 76];

let uuid = Uuid::from_slice(&bytes);

assert!(uuid.is_err());
source

pub const fn from_bytes(bytes: Bytes) -> Uuid

Creates a UUID using the supplied big-endian bytes.

source§

impl Uuid

source

pub fn parse_str(input: &str) -> Result<Uuid, Error>

Parses a Uuid from a string of hexadecimal digits with optional hyphens.

Any of the formats generated by this module (simple, hyphenated, urn) are supported by this parsing function.

source§

impl Uuid

source

pub const fn to_hyphenated(self) -> Hyphenated

Get a Hyphenated formatter.

source

pub const fn to_hyphenated_ref(&self) -> HyphenatedRef<'_>

Get a borrowed HyphenatedRef formatter.

source

pub const fn to_simple(self) -> Simple

Get a Simple formatter.

source

pub const fn to_simple_ref(&self) -> SimpleRef<'_>

Get a borrowed SimpleRef formatter.

source

pub const fn to_urn(self) -> Urn

Get a Urn formatter.

source

pub const fn to_urn_ref(&self) -> UrnRef<'_>

Get a borrowed UrnRef formatter.

source§

impl Uuid

source

pub fn new_v4() -> Uuid

Creates a random UUID.

This uses the getrandom crate to utilise the operating system’s RNG as the source of random numbers. If you’d like to use a custom generator, don’t use this method: generate random bytes using your custom generator and pass them to the uuid::Builder::from_bytes function instead.

Note that usage of this method requires the v4 feature of this crate to be enabled.

Examples

Basic usage:

use uuid::Uuid;

let uuid = Uuid::new_v4();
source§

impl Uuid

source

pub const NAMESPACE_DNS: Self = _

UUID namespace for Domain Name System (DNS).

source

pub const NAMESPACE_OID: Self = _

UUID namespace for ISO Object Identifiers (OIDs).

source

pub const NAMESPACE_URL: Self = _

UUID namespace for Uniform Resource Locators (URLs).

source

pub const NAMESPACE_X500: Self = _

UUID namespace for X.500 Distinguished Names (DNs).

source

pub fn get_variant(&self) -> Option<Variant>

Returns the variant of the UUID structure.

This determines the interpretation of the structure of the UUID. Currently only the RFC4122 variant is generated by this module.

source

pub const fn get_version_num(&self) -> usize

Returns the version number of the UUID.

This represents the algorithm used to generate the contents.

Currently only the Random (V4) algorithm is supported by this module. There are security and privacy implications for using older versions - see Wikipedia: Universally Unique Identifier for details.

source

pub fn get_version(&self) -> Option<Version>

Returns the version of the UUID.

This represents the algorithm used to generate the contents

source

pub fn as_fields(&self) -> (u32, u16, u16, &[u8; 8])

Returns the four field values of the UUID in big-endian order.

These values can be passed to the from_fields() method to get the original Uuid back.

  • The first field value represents the first group of (eight) hex digits, taken as a big-endian u32 value. For V1 UUIDs, this field represents the low 32 bits of the timestamp.
  • The second field value represents the second group of (four) hex digits, taken as a big-endian u16 value. For V1 UUIDs, this field represents the middle 16 bits of the timestamp.
  • The third field value represents the third group of (four) hex digits, taken as a big-endian u16 value. The 4 most significant bits give the UUID version, and for V1 UUIDs, the last 12 bits represent the high 12 bits of the timestamp.
  • The last field value represents the last two groups of four and twelve hex digits, taken in order. The first 1-3 bits of this indicate the UUID variant, and for V1 UUIDs, the next 13-15 bits indicate the clock sequence and the last 48 bits indicate the node ID.
Examples
use uuid::Uuid;

fn main() -> Result<(), uuid::Error> {
    let uuid = Uuid::nil();
    assert_eq!(uuid.as_fields(), (0, 0, 0, &[0u8; 8]));

    let uuid = Uuid::parse_str("936DA01F-9ABD-4D9D-80C7-02AF85C822A8")?;
    assert_eq!(
        uuid.as_fields(),
        (
            0x936DA01F,
            0x9ABD,
            0x4D9D,
            b"\x80\xC7\x02\xAF\x85\xC8\x22\xA8"
        )
    );

    Ok(())
}
source

pub fn to_fields_le(&self) -> (u32, u16, u16, &[u8; 8])

Returns the four field values of the UUID in little-endian order.

The bytes in the returned integer fields will be converted from big-endian order.

Examples
use uuid::Uuid;

fn main() -> Result<(), uuid::Error> {
    let uuid = Uuid::parse_str("936DA01F-9ABD-4D9D-80C7-02AF85C822A8")?;
    assert_eq!(
        uuid.to_fields_le(),
        (
            0x1FA06D93,
            0xBD9A,
            0x9D4D,
            b"\x80\xC7\x02\xAF\x85\xC8\x22\xA8"
        )
    );
    Ok(())
}
source

pub fn as_u128(&self) -> u128

Returns a 128bit value containing the UUID data.

The bytes in the UUID will be packed into a u128, like the Uuid::as_bytes method.

Examples
use uuid::Uuid;

fn main() -> Result<(), uuid::Error> {
    let uuid = Uuid::parse_str("936DA01F-9ABD-4D9D-80C7-02AF85C822A8")?;
    assert_eq!(
        uuid.as_u128(),
        0x936DA01F9ABD4D9D80C702AF85C822A8,
    );
    Ok(())
}
source

pub fn to_u128_le(&self) -> u128

Returns a 128bit little-endian value containing the UUID data.

The bytes in the UUID will be reversed and packed into a u128. Note that this will produce a different result than Uuid::to_fields_le, because the entire UUID is reversed, rather than reversing the individual fields in-place.

Examples
use uuid::Uuid;

fn main() -> Result<(), uuid::Error> {
    let uuid = Uuid::parse_str("936DA01F-9ABD-4D9D-80C7-02AF85C822A8")?;

    assert_eq!(
        uuid.to_u128_le(),
        0xA822C885AF02C7809D4DBD9A1FA06D93,
    );
    Ok(())
}
source

pub const fn as_bytes(&self) -> &Bytes

Returns an array of 16 octets containing the UUID data.

source

pub fn is_nil(&self) -> bool

Tests if the UUID is nil.

source

pub const fn encode_buffer() -> [u8; 45]

A buffer that can be used for encode_... calls, that is guaranteed to be long enough for any of the adapters.

Examples
use uuid::Uuid;

let uuid = Uuid::nil();

assert_eq!(
    uuid.to_simple().encode_lower(&mut Uuid::encode_buffer()),
    "00000000000000000000000000000000"
);

assert_eq!(
    uuid.to_hyphenated()
        .encode_lower(&mut Uuid::encode_buffer()),
    "00000000-0000-0000-0000-000000000000"
);

assert_eq!(
    uuid.to_urn().encode_lower(&mut Uuid::encode_buffer()),
    "urn:uuid:00000000-0000-0000-0000-000000000000"
);

Trait Implementations§

source§

impl Clone for Uuid

source§

fn clone(&self) -> Uuid

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Uuid

source§

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

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

impl Default for Uuid

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Display for Uuid

source§

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

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

impl<'a> From<&'a Uuid> for HyphenatedRef<'a>

source§

fn from(f: &'a Uuid) -> Self

Converts to this type from the input type.
source§

impl<'a> From<&'a Uuid> for SimpleRef<'a>

source§

fn from(f: &'a Uuid) -> Self

Converts to this type from the input type.
source§

impl<'a> From<&'a Uuid> for UrnRef<'a>

source§

fn from(f: &'a Uuid) -> Self

Converts to this type from the input type.
source§

impl From<Uuid> for Hyphenated

source§

fn from(f: Uuid) -> Self

Converts to this type from the input type.
source§

impl From<Uuid> for Simple

source§

fn from(f: Uuid) -> Self

Converts to this type from the input type.
source§

impl From<Uuid> for Urn

source§

fn from(f: Uuid) -> Self

Converts to this type from the input type.
source§

impl FromStr for Uuid

§

type Err = Error

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

fn from_str(uuid_str: &str) -> Result<Self, Self::Err>

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

impl Hash for Uuid

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl LowerHex for Uuid

source§

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

Formats the value using the given formatter.
source§

impl Ord for Uuid

source§

fn cmp(&self, other: &Uuid) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Uuid> for Uuid

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Uuid> for Uuid

source§

fn partial_cmp(&self, other: &Uuid) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl UpperHex for Uuid

source§

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

Formats the value using the given formatter.
source§

impl Copy for Uuid

source§

impl Eq for Uuid

source§

impl StructuralEq for Uuid

source§

impl StructuralPartialEq for Uuid

Auto Trait Implementations§

§

impl RefUnwindSafe for Uuid

§

impl Send for Uuid

§

impl Sync for Uuid

§

impl Unpin for Uuid

§

impl UnwindSafe for Uuid

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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.