• Home
  • Raw
  • Download

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(),
99 Uuid::from_bytes([0xFF; 16]) in max()
102 /// Creates a UUID from four field values.
109 /// # use uuid::Uuid;
115 /// let uuid = Uuid::from_fields(d1, d2, d3, &d4);
119 /// uuid.hyphenated().to_string(),
122 pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid { in from_fields() argument
123 Uuid::from_bytes([
143 /// Creates a UUID from four field values in little-endian order.
146 /// into big-endian order. This is based on the endianness of the UUID,
155 /// # use uuid::Uuid;
161 /// let uuid = Uuid::from_fields_le(d1, d2, d3, &d4);
165 /// uuid.hyphenated().to_string(),
168 pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid { in from_fields_le() argument
169 Uuid::from_bytes([
189 /// Creates a UUID from a 128bit value.
196 /// # use uuid::Uuid;
199 /// let uuid = Uuid::from_u128(v);
203 /// uuid.hyphenated().to_string(),
207 Uuid::from_bytes([ in from_u128()
227 /// Creates a UUID from a 128bit value in little-endian order.
230 /// This is based on the endianness of the UUID, rather than the target
239 /// # use uuid::Uuid;
242 /// let uuid = Uuid::from_u128_le(v);
246 /// uuid.hyphenated().to_string(),
250 Uuid::from_bytes([ in from_u128_le()
270 /// Creates a UUID from two 64bit values.
277 /// # use uuid::Uuid;
281 /// let uuid = Uuid::from_u64_pair(hi, lo);
285 /// uuid.hyphenated().to_string(),
289 Uuid::from_bytes([ in from_u64_pair()
309 /// Creates a UUID using the supplied bytes.
320 /// # fn main() -> Result<(), uuid::Error> {
321 /// # use uuid::Uuid;
329 /// let uuid = Uuid::from_slice(&bytes)?;
333 /// uuid.hyphenated().to_string(),
338 pub fn from_slice(b: &[u8]) -> Result<Uuid, Error> { in from_slice() argument
345 Ok(Uuid::from_bytes(bytes)) in from_slice()
348 /// Creates a UUID using the supplied bytes in little endian order.
361 /// # fn main() -> Result<(), uuid::Error> {
362 /// # use uuid::Uuid;
370 /// let uuid = Uuid::from_slice_le(&bytes)?;
373 /// uuid.hyphenated().to_string(),
379 pub fn from_slice_le(b: &[u8]) -> Result<Uuid, Error> { in from_slice_le() argument
386 Ok(Uuid::from_bytes_le(bytes)) in from_slice_le()
389 /// Creates a UUID using the supplied bytes.
396 /// # fn main() -> Result<(), uuid::Error> {
397 /// # use uuid::Uuid;
405 /// let uuid = Uuid::from_bytes(bytes);
408 /// 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,
484 pub fn from_bytes_ref(bytes: &Bytes) -> &Uuid { in from_bytes_ref() argument
485 // SAFETY: `Bytes` and `Uuid` have the same ABI in from_bytes_ref()
486 unsafe { &*(bytes as *const Bytes as *const Uuid) } in from_bytes_ref() constant
502 /// # use uuid::Builder;
510 /// let uuid = Builder::from_bytes(bytes).into_uuid();
514 /// uuid.hyphenated().to_string(),
518 Builder(Uuid::from_bytes(b)) in from_bytes()
530 /// # fn main() -> Result<(), uuid::Error> {
531 /// # use uuid::{Builder, Uuid};
539 /// let uuid = Builder::from_bytes_le(bytes).into_uuid();
543 /// uuid.hyphenated().to_string(),
549 Builder(Uuid::from_bytes_le(b)) in from_bytes_le()
552 /// Creates a `Builder` for a version 1 UUID using the supplied timestamp and node ID.
557 /// Creates a `Builder` for a version 3 UUID using the supplied MD5 hashed bytes.
559 Builder(Uuid::from_bytes(md5_bytes)) in from_md5_bytes()
564 /// Creates a `Builder` for a version 4 UUID using the supplied random bytes.
567 /// set the appropriate bits for the UUID version and variant.
572 /// # use uuid::{Builder, Variant, Version};
578 /// let uuid = Builder::from_random_bytes(random_bytes).into_uuid();
580 /// assert_eq!(Some(Version::Random), uuid.get_version());
581 /// assert_eq!(Variant::RFC4122, uuid.get_variant());
584 Builder(Uuid::from_bytes(random_bytes)) in from_random_bytes()
589 /// Creates a `Builder` for a version 5 UUID using the supplied SHA-1 hashed bytes.
592 /// bits for the UUID version and variant.
594 Builder(Uuid::from_bytes(sha1_bytes)) in from_sha1_bytes()
599 /// Creates a `Builder` for a version 6 UUID using the supplied timestamp and node ID.
601 /// 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());
646 /// Creates a `Builder` for a version 8 UUID using the supplied user-defined bytes.
649 /// bits for the UUID version and variant.
667 /// # use uuid::Builder;
668 /// # fn main() -> Result<(), uuid::Error> {
676 /// let uuid = Builder::from_slice(&bytes)?.into_uuid();
680 /// uuid.hyphenated().to_string(),
686 Ok(Builder(Uuid::from_slice(b)?)) in from_slice()
702 /// # use uuid::Builder;
703 /// # fn main() -> Result<(), uuid::Error> {
711 /// let uuid = Builder::from_slice_le(&bytes)?.into_uuid();
715 /// uuid.hyphenated().to_string(),
721 Ok(Builder(Uuid::from_slice_le(b)?)) in from_slice_le()
731 /// # use uuid::Builder;
737 /// let uuid = Builder::from_fields(d1, d2, d3, &d4).into_uuid();
740 /// uuid.hyphenated().to_string(),
745 Builder(Uuid::from_fields(d1, d2, d3, d4))
755 /// # use uuid::Builder;
761 /// let uuid = Builder::from_fields_le(d1, d2, d3, &d4).into_uuid();
764 /// uuid.hyphenated().to_string(),
769 Builder(Uuid::from_fields_le(d1, d2, d3, d4))
779 /// # use uuid::Builder;
782 /// let uuid = Builder::from_u128(v).into_uuid();
786 /// uuid.hyphenated().to_string(),
790 Builder(Uuid::from_u128(v)) in from_u128()
793 /// Creates a UUID from a 128bit value in little-endian order.
800 /// # use uuid::Builder;
803 /// let uuid = Builder::from_u128_le(v).into_uuid();
807 /// uuid.hyphenated().to_string(),
811 Builder(Uuid::from_u128_le(v)) in from_u128_le()
814 /// Creates a `Builder` with an initial [`Uuid::nil`].
821 /// # use uuid::Builder;
822 /// let uuid = Builder::nil().into_uuid();
826 /// uuid.hyphenated().to_string(),
830 Builder(Uuid::nil()) in nil()
833 /// Specifies the variant of the UUID.
839 /// Specifies the variant of the UUID.
853 /// Specifies the version number of the UUID.
859 /// Specifies the version number of the UUID.
866 /// Get a reference to the underlying [`Uuid`].
873 /// # use uuid::Builder;
881 pub const fn as_uuid(&self) -> &Uuid { in as_uuid() argument
885 /// Convert the builder into a [`Uuid`].
892 /// # use uuid::Builder;
893 /// let uuid = Builder::nil().into_uuid();
896 /// uuid.hyphenated().to_string(),
900 pub const fn into_uuid(self) -> Uuid { in into_uuid() argument