Lines Matching full:uuid
2 // Copyright 2018 The Uuid Project Developers.
12 //! A Builder type for [`Uuid`]s.
14 //! [`Uuid`]: ../struct.Uuid.html
16 use crate::{error::*, timestamp, Bytes, Uuid, Variant, Version};
18 /// A builder for creating a UUID.
20 /// This type is useful if you need to mutate individual fields of a [`Uuid`]
21 /// while constructing it. Since the [`Uuid`] type is `Copy`, it doesn't offer
24 /// The `Builder` type also always exposes APIs to construct [`Uuid`]s for any
26 /// lower-level API than the methods on [`Uuid`].
30 /// Creating a version 4 UUID from externally generated random bytes:
33 /// # use uuid::{Builder, Version, Variant};
40 /// let uuid = Builder::from_random_bytes(random_bytes).into_uuid();
42 /// assert_eq!(Some(Version::Random), uuid.get_version());
43 /// assert_eq!(Variant::RFC4122, uuid.get_variant());
47 pub struct Builder(Uuid);
49 impl Uuid { implementation
50 /// The 'nil UUID' (all zeros).
52 /// The nil UUID is a special form of UUID that is specified to have all
57 /// * [Nil UUID in RFC4122](https://tools.ietf.org/html/rfc4122.html#section-4.1.7)
64 /// # use uuid::Uuid;
65 /// let uuid = Uuid::nil();
69 /// uuid.hyphenated().to_string(),
73 Uuid::from_bytes([0; 16]) in nil()
76 /// The 'max UUID' (all ones).
78 /// The max UUID is a special form of UUID that is specified to have all
83 …/// * [Max UUID in Draft RFC: New UUID Formats, Version 4](https://datatracker.ietf.org/doc/html/d…
90 /// # use uuid::Uuid;
91 /// let uuid = Uuid::max();
95 /// uuid.hyphenated().to_string(),
100 Uuid::from_bytes([0xFF; 16]) in max()
103 /// Creates a UUID from four field values.
110 /// # use uuid::Uuid;
116 /// let uuid = Uuid::from_fields(d1, d2, d3, &d4);
120 /// uuid.hyphenated().to_string(),
123 pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid { in from_fields() argument
124 Uuid::from_bytes([
144 /// Creates a UUID from four field values in little-endian order.
147 /// into big-endian order. This is based on the endianness of the UUID,
156 /// # use uuid::Uuid;
162 /// let uuid = Uuid::from_fields_le(d1, d2, d3, &d4);
166 /// uuid.hyphenated().to_string(),
169 pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid { in from_fields_le() argument
170 Uuid::from_bytes([
190 /// Creates a UUID from a 128bit value.
197 /// # use uuid::Uuid;
200 /// let uuid = Uuid::from_u128(v);
204 /// uuid.hyphenated().to_string(),
208 Uuid::from_bytes([ in from_u128()
228 /// Creates a UUID from a 128bit value in little-endian order.
231 /// This is based on the endianness of the UUID, rather than the target
240 /// # use uuid::Uuid;
243 /// let uuid = Uuid::from_u128_le(v);
247 /// uuid.hyphenated().to_string(),
251 Uuid::from_bytes([ in from_u128_le()
271 /// Creates a UUID from two 64bit values.
278 /// # use uuid::Uuid;
282 /// let uuid = Uuid::from_u64_pair(hi, lo);
286 /// uuid.hyphenated().to_string(),
290 Uuid::from_bytes([ in from_u64_pair()
310 /// Creates a UUID using the supplied bytes.
321 /// # fn main() -> Result<(), uuid::Error> {
322 /// # use uuid::Uuid;
330 /// let uuid = Uuid::from_slice(&bytes)?;
334 /// uuid.hyphenated().to_string(),
339 pub fn from_slice(b: &[u8]) -> Result<Uuid, Error> { in from_slice() argument
346 Ok(Uuid::from_bytes(bytes)) in from_slice()
349 /// Creates a UUID using the supplied bytes in little endian order.
362 /// # fn main() -> Result<(), uuid::Error> {
363 /// # use uuid::Uuid;
371 /// let uuid = Uuid::from_slice_le(&bytes)?;
374 /// uuid.hyphenated().to_string(),
380 pub fn from_slice_le(b: &[u8]) -> Result<Uuid, Error> { in from_slice_le() argument
387 Ok(Uuid::from_bytes_le(bytes)) in from_slice_le()
390 /// Creates a UUID using the supplied bytes.
397 /// # fn main() -> Result<(), uuid::Error> {
398 /// # use uuid::Uuid;
406 /// let uuid = Uuid::from_bytes(bytes);
409 /// uuid.hyphenated().to_string(),
415 pub const fn from_bytes(bytes: Bytes) -> Uuid { in from_bytes() argument
416 Uuid(bytes) in from_bytes()
419 /// Creates a UUID using the supplied bytes in little endian order.
428 /// # fn main() -> Result<(), uuid::Error> {
429 /// # use uuid::Uuid;
437 /// let uuid = Uuid::from_bytes_le(bytes);
441 /// uuid.hyphenated().to_string(),
446 pub const fn from_bytes_le(b: Bytes) -> Uuid { in from_bytes_le() argument
447 Uuid([ in from_bytes_le()
453 /// Creates a reference to a UUID from a reference to the supplied bytes.
460 /// # fn main() -> Result<(), uuid::Error> {
461 /// # use uuid::Uuid;
469 /// let uuid = Uuid::from_bytes_ref(&bytes);
472 /// uuid.hyphenated().to_string(),
477 /// uuid as *const Uuid as *const u8,
483 pub fn from_bytes_ref(bytes: &Bytes) -> &Uuid { in from_bytes_ref() argument
484 // SAFETY: `Bytes` and `Uuid` have the same ABI in from_bytes_ref()
485 unsafe { &*(bytes as *const Bytes as *const Uuid) } in from_bytes_ref() constant
501 /// # use uuid::Builder;
509 /// let uuid = Builder::from_bytes(bytes).into_uuid();
513 /// uuid.hyphenated().to_string(),
517 Builder(Uuid::from_bytes(b)) in from_bytes()
529 /// # fn main() -> Result<(), uuid::Error> {
530 /// # use uuid::{Builder, Uuid};
538 /// let uuid = Builder::from_bytes_le(bytes).into_uuid();
542 /// uuid.hyphenated().to_string(),
548 Builder(Uuid::from_bytes_le(b)) in from_bytes_le()
551 /// Creates a `Builder` for a version 1 UUID using the supplied timestamp and node ID.
556 /// Creates a `Builder` for a version 3 UUID using the supplied MD5 hashed bytes.
558 Builder(Uuid::from_bytes(md5_bytes)) in from_md5_bytes()
563 /// Creates a `Builder` for a version 4 UUID using the supplied random bytes.
566 /// set the appropriate bits for the UUID version and variant.
571 /// # use uuid::{Builder, Variant, Version};
577 /// let uuid = Builder::from_random_bytes(random_bytes).into_uuid();
579 /// assert_eq!(Some(Version::Random), uuid.get_version());
580 /// assert_eq!(Variant::RFC4122, uuid.get_variant());
583 Builder(Uuid::from_bytes(random_bytes)) in from_random_bytes()
588 /// Creates a `Builder` for a version 5 UUID using the supplied SHA-1 hashed bytes.
591 /// bits for the UUID version and variant.
593 Builder(Uuid::from_bytes(sha1_bytes)) in from_sha1_bytes()
598 /// Creates a `Builder` for a version 6 UUID using the supplied timestamp and node ID.
600 /// This method will encode the ticks, counter, and node ID in a sortable UUID.
612 /// Creates a `Builder` for a version 7 UUID using the supplied Unix timestamp and random bytes.
618 /// Creating a UUID using the current system timestamp:
624 /// # use uuid::{Builder, Uuid, Variant, Version, Timestamp, NoContext};
632 …/// let uuid = Builder::from_unix_timestamp_millis(ts.as_millis().try_into()?, &random_bytes).into…
634 /// assert_eq!(Some(Version::SortRand), uuid.get_version());
635 /// assert_eq!(Variant::RFC4122, uuid.get_variant());
647 /// Creates a `Builder` for a version 8 UUID using the supplied user-defined bytes.
650 /// bits for the UUID version and variant.
669 /// # use uuid::Builder;
670 /// # fn main() -> Result<(), uuid::Error> {
678 /// let uuid = Builder::from_slice(&bytes)?.into_uuid();
682 /// uuid.hyphenated().to_string(),
688 Ok(Builder(Uuid::from_slice(b)?)) in from_slice()
704 /// # use uuid::Builder;
705 /// # fn main() -> Result<(), uuid::Error> {
713 /// let uuid = Builder::from_slice_le(&bytes)?.into_uuid();
717 /// uuid.hyphenated().to_string(),
723 Ok(Builder(Uuid::from_slice_le(b)?)) in from_slice_le()
733 /// # use uuid::Builder;
739 /// let uuid = Builder::from_fields(d1, d2, d3, &d4).into_uuid();
742 /// uuid.hyphenated().to_string(),
747 Builder(Uuid::from_fields(d1, d2, d3, d4))
757 /// # use uuid::Builder;
763 /// let uuid = Builder::from_fields_le(d1, d2, d3, &d4).into_uuid();
766 /// uuid.hyphenated().to_string(),
771 Builder(Uuid::from_fields_le(d1, d2, d3, d4))
781 /// # use uuid::Builder;
784 /// let uuid = Builder::from_u128(v).into_uuid();
788 /// uuid.hyphenated().to_string(),
792 Builder(Uuid::from_u128(v)) in from_u128()
795 /// Creates a UUID from a 128bit value in little-endian order.
802 /// # use uuid::Builder;
805 /// let uuid = Builder::from_u128_le(v).into_uuid();
809 /// uuid.hyphenated().to_string(),
813 Builder(Uuid::from_u128_le(v)) in from_u128_le()
816 /// Creates a `Builder` with an initial [`Uuid::nil`].
823 /// # use uuid::Builder;
824 /// let uuid = Builder::nil().into_uuid();
828 /// uuid.hyphenated().to_string(),
832 Builder(Uuid::nil()) in nil()
835 /// Specifies the variant of the UUID.
841 /// Specifies the variant of the UUID.
855 /// Specifies the version number of the UUID.
861 /// Specifies the version number of the UUID.
868 /// Get a reference to the underlying [`Uuid`].
875 /// # use uuid::Builder;
883 pub const fn as_uuid(&self) -> &Uuid { in as_uuid() argument
887 /// Convert the builder into a [`Uuid`].
894 /// # use uuid::Builder;
895 /// let uuid = Builder::nil().into_uuid();
898 /// uuid.hyphenated().to_string(),
902 pub const fn into_uuid(self) -> Uuid { in into_uuid() argument