Skip to main content

Uuid

Struct Uuid 

Source
pub struct Uuid(/* private fields */);
Available on crate feature uuid only.
Expand description

A 128-bit UUID.

The internal representation is 16 big-endian bytes per RFC 9562.

§Example

use id_forge::uuid::Uuid;

let id = Uuid::v4();
assert_eq!(id.to_string().len(), 36);

Implementations§

Source§

impl Uuid

Source

pub const fn nil() -> Self

The Nil UUID: all 128 bits set to zero (RFC 9562 §5.9).

§Example
use id_forge::uuid::Uuid;

assert_eq!(Uuid::nil().to_string(), "00000000-0000-0000-0000-000000000000");
Examples found in repository?
examples/basic.rs (line 11)
7fn main() {
8    let v4 = Uuid::v4();
9    println!("UUID v4:    {v4} (version={})", v4.version());
10    println!("UUID v7:    {}", Uuid::v7());
11    println!("UUID nil:   {}", Uuid::nil());
12
13    let a = Ulid::new();
14    let b = Ulid::new();
15    println!("ULID a:     {a}");
16    println!("ULID b:     {b} (monotonic: {})", b > a);
17
18    let gen = Snowflake::new(1);
19    let sf = gen.next_id();
20    let (ts_offset, worker, seq) = Snowflake::parts(sf);
21    println!(
22        "Snowflake:  {sf}  (ts+epoch={}, worker={worker}, seq={seq})",
23        ts_offset + gen.epoch_ms()
24    );
25
26    println!("NanoID 21:  {}", nanoid::generate());
27    println!("NanoID 8:   {}", nanoid::with_length(8));
28
29    assert_eq!(v4, Uuid::parse_str(&v4.to_string()).unwrap());
30    assert_eq!(a, Ulid::parse_str(&a.to_string()).unwrap());
31}
Source

pub const fn max() -> Self

The Max UUID: all 128 bits set to one (RFC 9562 §5.10).

§Example
use id_forge::uuid::Uuid;

assert_eq!(Uuid::max().to_string(), "ffffffff-ffff-ffff-ffff-ffffffffffff");
Source

pub fn v4() -> Self

Construct a v4 (random) UUID per RFC 9562 §5.4.

122 random bits with the version nibble set to 0100 and the variant bits set to 10 (RFC 4122 layout).

§Example
use id_forge::uuid::Uuid;

let id = Uuid::v4();
assert_eq!(id.version(), 4);
Examples found in repository?
examples/basic.rs (line 8)
7fn main() {
8    let v4 = Uuid::v4();
9    println!("UUID v4:    {v4} (version={})", v4.version());
10    println!("UUID v7:    {}", Uuid::v7());
11    println!("UUID nil:   {}", Uuid::nil());
12
13    let a = Ulid::new();
14    let b = Ulid::new();
15    println!("ULID a:     {a}");
16    println!("ULID b:     {b} (monotonic: {})", b > a);
17
18    let gen = Snowflake::new(1);
19    let sf = gen.next_id();
20    let (ts_offset, worker, seq) = Snowflake::parts(sf);
21    println!(
22        "Snowflake:  {sf}  (ts+epoch={}, worker={worker}, seq={seq})",
23        ts_offset + gen.epoch_ms()
24    );
25
26    println!("NanoID 21:  {}", nanoid::generate());
27    println!("NanoID 8:   {}", nanoid::with_length(8));
28
29    assert_eq!(v4, Uuid::parse_str(&v4.to_string()).unwrap());
30    assert_eq!(a, Ulid::parse_str(&a.to_string()).unwrap());
31}
Source

pub fn v7() -> Self

Construct a v7 (time-ordered) UUID per RFC 9562 §5.7.

48-bit big-endian millisecond timestamp prefix, 74 random bits, with the version nibble set to 0111 and the RFC 4122 variant bits. Two v7 IDs generated in different milliseconds compare in timestamp order byte-wise.

§Example
use id_forge::uuid::Uuid;

let id = Uuid::v7();
assert_eq!(id.version(), 7);
Examples found in repository?
examples/basic.rs (line 10)
7fn main() {
8    let v4 = Uuid::v4();
9    println!("UUID v4:    {v4} (version={})", v4.version());
10    println!("UUID v7:    {}", Uuid::v7());
11    println!("UUID nil:   {}", Uuid::nil());
12
13    let a = Ulid::new();
14    let b = Ulid::new();
15    println!("ULID a:     {a}");
16    println!("ULID b:     {b} (monotonic: {})", b > a);
17
18    let gen = Snowflake::new(1);
19    let sf = gen.next_id();
20    let (ts_offset, worker, seq) = Snowflake::parts(sf);
21    println!(
22        "Snowflake:  {sf}  (ts+epoch={}, worker={worker}, seq={seq})",
23        ts_offset + gen.epoch_ms()
24    );
25
26    println!("NanoID 21:  {}", nanoid::generate());
27    println!("NanoID 8:   {}", nanoid::with_length(8));
28
29    assert_eq!(v4, Uuid::parse_str(&v4.to_string()).unwrap());
30    assert_eq!(a, Ulid::parse_str(&a.to_string()).unwrap());
31}
Source

pub const fn from_bytes(bytes: &[u8; 16]) -> Self

Wrap a 16-byte big-endian representation.

The bytes are taken as-is; no version or variant bits are touched. Use this to round-trip an externally generated UUID or to reconstruct one from storage.

§Example
use id_forge::uuid::Uuid;

let id = Uuid::v4();
let copy = Uuid::from_bytes(id.as_bytes());
assert_eq!(id, copy);
Source

pub const fn as_bytes(&self) -> &[u8; 16]

Return the raw 16-byte big-endian representation.

Source

pub const fn version(&self) -> u8

Return the version nibble (the high 4 bits of byte 6).

4 for v4, 7 for v7, 0 for Uuid::nil, 15 for Uuid::max.

Examples found in repository?
examples/basic.rs (line 9)
7fn main() {
8    let v4 = Uuid::v4();
9    println!("UUID v4:    {v4} (version={})", v4.version());
10    println!("UUID v7:    {}", Uuid::v7());
11    println!("UUID nil:   {}", Uuid::nil());
12
13    let a = Ulid::new();
14    let b = Ulid::new();
15    println!("ULID a:     {a}");
16    println!("ULID b:     {b} (monotonic: {})", b > a);
17
18    let gen = Snowflake::new(1);
19    let sf = gen.next_id();
20    let (ts_offset, worker, seq) = Snowflake::parts(sf);
21    println!(
22        "Snowflake:  {sf}  (ts+epoch={}, worker={worker}, seq={seq})",
23        ts_offset + gen.epoch_ms()
24    );
25
26    println!("NanoID 21:  {}", nanoid::generate());
27    println!("NanoID 8:   {}", nanoid::with_length(8));
28
29    assert_eq!(v4, Uuid::parse_str(&v4.to_string()).unwrap());
30    assert_eq!(a, Ulid::parse_str(&a.to_string()).unwrap());
31}
Source

pub fn parse_str(input: &str) -> Result<Self, ParseError>

Parse a UUID from its canonical 36-character hyphenated form (e.g. f47ac10b-58cc-4372-a567-0e02b2c3d479).

Parsing is case-insensitive. Returns ParseError if the input is not exactly 36 characters, has hyphens in the wrong positions, or contains a non-hex digit.

§Example
use id_forge::uuid::Uuid;

let id = Uuid::parse_str("f47ac10b-58cc-4372-a567-0e02b2c3d479").unwrap();
assert_eq!(id.to_string(), "f47ac10b-58cc-4372-a567-0e02b2c3d479");
Examples found in repository?
examples/basic.rs (line 29)
7fn main() {
8    let v4 = Uuid::v4();
9    println!("UUID v4:    {v4} (version={})", v4.version());
10    println!("UUID v7:    {}", Uuid::v7());
11    println!("UUID nil:   {}", Uuid::nil());
12
13    let a = Ulid::new();
14    let b = Ulid::new();
15    println!("ULID a:     {a}");
16    println!("ULID b:     {b} (monotonic: {})", b > a);
17
18    let gen = Snowflake::new(1);
19    let sf = gen.next_id();
20    let (ts_offset, worker, seq) = Snowflake::parts(sf);
21    println!(
22        "Snowflake:  {sf}  (ts+epoch={}, worker={worker}, seq={seq})",
23        ts_offset + gen.epoch_ms()
24    );
25
26    println!("NanoID 21:  {}", nanoid::generate());
27    println!("NanoID 8:   {}", nanoid::with_length(8));
28
29    assert_eq!(v4, Uuid::parse_str(&v4.to_string()).unwrap());
30    assert_eq!(a, Ulid::parse_str(&a.to_string()).unwrap());
31}

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 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 FromStr for Uuid

Source§

type Err = ParseError

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

fn from_str(s: &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 Ord for Uuid

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. 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 PartialOrd 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 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Uuid

Source§

impl Eq for Uuid

Source§

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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.