From 66504b7574557ef8de202df59b4368bc6d9f9caf Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 3 Nov 2021 19:16:16 +1000 Subject: [PATCH 1/2] make conversion methods consistent across the API --- benches/format_str.rs | 12 +- src/builder.rs | 175 +++++--- src/error.rs | 1 + src/external/serde_support.rs | 49 +-- src/fmt.rs | 742 ++++++---------------------------- src/lib.rs | 110 +++-- src/parser.rs | 9 +- src/v1.rs | 6 +- src/v3.rs | 6 +- src/v4.rs | 9 +- src/v5.rs | 4 +- 11 files changed, 347 insertions(+), 776 deletions(-) diff --git a/benches/format_str.rs b/benches/format_str.rs index 818d4185..fab89297 100644 --- a/benches/format_str.rs +++ b/benches/format_str.rs @@ -10,7 +10,7 @@ fn hyphenated(b: &mut Bencher) { let uuid = Uuid::parse_str("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4").unwrap(); b.iter(|| { let mut buffer = [0_u8; 36]; - write!(&mut buffer as &mut [_], "{:x}", uuid.to_hyphenated()).unwrap(); + write!(&mut buffer as &mut [_], "{:x}", uuid.hyphenated()).unwrap(); buffer }); } @@ -20,7 +20,7 @@ fn simple(b: &mut Bencher) { let uuid = Uuid::parse_str("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4").unwrap(); b.iter(|| { let mut buffer = [0_u8; 32]; - write!(&mut buffer as &mut [_], "{:x}", uuid.to_simple()).unwrap(); + write!(&mut buffer as &mut [_], "{:x}", uuid.simple()).unwrap(); buffer }) } @@ -30,7 +30,7 @@ fn urn(b: &mut Bencher) { let uuid = Uuid::parse_str("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4").unwrap(); b.iter(|| { let mut buffer = [0_u8; 36 + 9]; - write!(&mut buffer as &mut [_], "{:x}", uuid.to_urn()).unwrap(); + write!(&mut buffer as &mut [_], "{:x}", uuid.urn()).unwrap(); buffer }) } @@ -40,7 +40,7 @@ fn encode_hyphen(b: &mut Bencher) { let uuid = Uuid::parse_str("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4").unwrap(); b.iter(|| { let mut buffer = [0_u8; 36]; - uuid.to_hyphenated().encode_lower(&mut buffer); + uuid.hyphenated().encode_lower(&mut buffer); buffer }); } @@ -50,7 +50,7 @@ fn encode_simple(b: &mut Bencher) { let uuid = Uuid::parse_str("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4").unwrap(); b.iter(|| { let mut buffer = [0_u8; 32]; - uuid.to_simple().encode_lower(&mut buffer); + uuid.simple().encode_lower(&mut buffer); buffer }) } @@ -60,7 +60,7 @@ fn encode_urn(b: &mut Bencher) { let uuid = Uuid::parse_str("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4").unwrap(); b.iter(|| { let mut buffer = [0_u8; 36 + 9]; - uuid.to_urn().encode_lower(&mut buffer); + uuid.urn().encode_lower(&mut buffer); buffer }) } diff --git a/src/builder.rs b/src/builder.rs index 6fc39909..a52b03d6 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -17,6 +17,10 @@ use crate::{error::*, Bytes, Uuid, Variant, Version}; /// A builder struct for creating a UUID. /// +/// This type is useful if you need to mutate individual fields of a [`Uuid`] +/// while constructing it. Since the [`Uuid`] type is `Copy`, it doesn't offer +/// any methods to mutate in place. They live on the `Builder` instead. +/// /// # Examples /// /// Creating a v4 UUID from externally generated bytes: @@ -29,14 +33,14 @@ use crate::{error::*, Bytes, Uuid, Variant, Version}; /// # ]; /// let random_bytes = rng(); /// -/// let uuid = Builder::from_random_bytes(random_bytes).build(); +/// let uuid = Builder::from_random_bytes(random_bytes).into_uuid(); /// /// assert_eq!(Some(Version::Random), uuid.get_version()); /// assert_eq!(Variant::RFC4122, uuid.get_variant()); /// ``` #[allow(missing_copy_implementations)] #[derive(Debug)] -pub struct Builder(Bytes); +pub struct Builder(Uuid); impl Uuid { /// The 'nil UUID'. @@ -56,7 +60,7 @@ impl Uuid { /// /// assert_eq!( /// "00000000-0000-0000-0000-000000000000", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// ``` pub const fn nil() -> Self { @@ -80,7 +84,7 @@ impl Uuid { /// /// assert_eq!( /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// ``` pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid { @@ -124,7 +128,7 @@ impl Uuid { /// /// assert_eq!( /// "a4a3a2a1-b2b1-c2c1-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// ``` pub const fn from_fields_le( @@ -167,7 +171,7 @@ impl Uuid { /// /// assert_eq!( /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// ``` pub const fn from_u128(v: u128) -> Self { @@ -210,7 +214,7 @@ impl Uuid { /// /// assert_eq!( /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// ``` pub const fn from_u128_le(v: u128) -> Self { @@ -249,7 +253,7 @@ impl Uuid { /// /// assert_eq!( /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// ``` pub const fn from_u64_pair(high_bits: u64, low_bits: u64) -> Self { @@ -297,7 +301,7 @@ impl Uuid { /// /// assert_eq!( /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// # Ok(()) /// # } @@ -341,7 +345,7 @@ impl Uuid { /// let uuid = Uuid::from_slice_le(&bytes)?; /// /// assert_eq!( - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// "a4a3a2a1-b2b1-c2c1-d1d2-d3d4d5d6d7d8" /// ); /// # Ok(()) @@ -380,7 +384,7 @@ impl Uuid { /// let uuid = Uuid::from_bytes(bytes); /// /// assert_eq!( - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8" /// ); /// # Ok(()) @@ -412,7 +416,7 @@ impl Uuid { /// /// assert_eq!( /// "a4a3a2a1-b2b1-c2c1-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// # Ok(()) /// # } @@ -423,6 +427,43 @@ impl Uuid { b[11], b[12], b[13], b[14], b[15], ]) } + + /// Creates a reference to a UUID from a reference to the supplied bytes. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// # fn main() -> Result<(), Box> { + /// # use uuid::Uuid; + /// let bytes = [ + /// 0xa1, 0xa2, 0xa3, 0xa4, + /// 0xb1, 0xb2, + /// 0xc1, 0xc2, + /// 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, + /// ]; + /// + /// let uuid = Uuid::from_bytes_ref(&bytes); + /// + /// assert_eq!( + /// uuid.hyphenated().to_string(), + /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8" + /// ); + /// + /// assert!(std::ptr::eq( + /// uuid as *const Uuid as *const u8, + /// &bytes as *const [u8; 16] as *const u8, + /// )); + /// # Ok(()) + /// # } + /// ``` + pub fn from_bytes_ref(bytes: &Bytes) -> &Uuid { + // SAFETY: `Bytes` and `Uuid` have the same ABI + unsafe { + &*(bytes as *const Bytes as *const Uuid) + } + } } impl Builder { @@ -445,11 +486,11 @@ impl Builder { /// /// assert_eq!( /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// ``` pub const fn from_bytes(b: Bytes) -> Self { - Builder(Uuid::from_bytes(b).0) + Builder(Uuid::from_bytes(b)) } /// Creates a `Builder` using the supplied bytes in little endian order. @@ -474,13 +515,13 @@ impl Builder { /// /// assert_eq!( /// "a4a3a2a1-b2b1-c2c1-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// # Ok(()) /// # } /// ``` pub const fn from_bytes_le(b: Bytes) -> Self { - Builder(Uuid::from_bytes_le(b).0) + Builder(Uuid::from_bytes_le(b)) } /// Creates a `Builder` using the supplied random bytes. @@ -498,13 +539,13 @@ impl Builder { /// # 145, 63, 62, /// # ]; /// let random_bytes = rng(); - /// let uuid = Builder::from_random_bytes(random_bytes).build(); + /// let uuid = Builder::from_random_bytes(random_bytes).into_uuid(); /// /// assert_eq!(Some(Version::Random), uuid.get_version()); /// assert_eq!(Variant::RFC4122, uuid.get_variant()); /// ``` pub const fn from_random_bytes(b: Bytes) -> Self { - Builder(b) + Builder(Uuid::from_bytes(b)) .with_variant(Variant::RFC4122) .with_version(Version::Random) } @@ -533,13 +574,13 @@ impl Builder { /// /// assert_eq!( /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// # Ok(()) /// # } /// ``` pub fn from_slice(b: &[u8]) -> Result { - Ok(Builder(*Uuid::from_slice(b)?.as_bytes())) + Ok(Builder(Uuid::from_slice(b)?)) } /// Creates a `Builder` using the supplied bytes in little endian order. @@ -568,13 +609,13 @@ impl Builder { /// /// assert_eq!( /// "a4a3a2a1-b2b1-c2c1-d1d2-d3d4d5d6d7d8", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// # Ok(()) /// # } /// ``` pub fn from_slice_le(b: &[u8]) -> Result { - Ok(Builder(*Uuid::from_slice_le(b)?.as_bytes())) + Ok(Builder(Uuid::from_slice_le(b)?)) } /// Creates a `Builder` from four field values. @@ -593,12 +634,12 @@ impl Builder { /// let uuid = Builder::from_fields(d1, d2, d3, &d4).into_uuid(); /// /// assert_eq!( - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8" /// ); /// ``` pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self { - Builder::from_bytes(*Uuid::from_fields(d1, d2, d3, d4).as_bytes()) + Builder(Uuid::from_fields(d1, d2, d3, d4)) } /// Creates a `Builder` from four field values. @@ -617,7 +658,7 @@ impl Builder { /// let uuid = Builder::from_fields_le(d1, d2, d3, &d4).into_uuid(); /// /// assert_eq!( - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// "a4a3a2a1-b2b1-c2c1-d1d2-d3d4d5d6d7d8" /// ); /// ``` @@ -627,12 +668,49 @@ impl Builder { d3: u16, d4: &[u8; 8], ) -> Self { - Builder::from_bytes(*Uuid::from_fields_le(d1, d2, d3, d4).as_bytes()) + Builder(Uuid::from_fields_le(d1, d2, d3, d4)) } /// Creates a `Builder` from a 128bit value. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// # use uuid::Builder; + /// let v = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8u128; + /// + /// let uuid = Builder::from_u128(v).into_uuid(); + /// + /// assert_eq!( + /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", + /// uuid.hyphenated().to_string(), + /// ); + /// ``` pub const fn from_u128(v: u128) -> Self { - Builder::from_bytes(*Uuid::from_u128(v).as_bytes()) + Builder(Uuid::from_u128(v)) + } + + /// Creates a UUID from a 128bit value in little-endian order. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// # use uuid::Builder; + /// let v = 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1u128; + /// + /// let uuid = Builder::from_u128_le(v).into_uuid(); + /// + /// assert_eq!( + /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", + /// uuid.hyphenated().to_string(), + /// ); + /// ``` + pub const fn from_u128_le(v: u128) -> Self { + Builder(Uuid::from_u128_le(v)) } /// Creates a `Builder` with an initial [`Uuid::nil`]. @@ -647,11 +725,11 @@ impl Builder { /// /// assert_eq!( /// "00000000-0000-0000-0000-000000000000", - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// ); /// ``` pub const fn nil() -> Self { - Builder([0; 16]) + Builder(Uuid::nil()) } /// Specifies the variant of the UUID. @@ -662,9 +740,9 @@ impl Builder { /// Specifies the variant of the UUID. pub const fn with_variant(mut self, v: Variant) -> Self { - let byte = self.0[8]; + let byte = (self.0).0[8]; - self.0[8] = match v { + (self.0).0[8] = match v { Variant::NCS => byte & 0x7f, Variant::RFC4122 => (byte & 0x3f) | 0x80, Variant::Microsoft => (byte & 0x1f) | 0xc0, @@ -682,31 +760,12 @@ impl Builder { /// Specifies the version number of the UUID. pub const fn with_version(mut self, v: Version) -> Self { - self.0[6] = (self.0[6] & 0x0f) | ((v as u8) << 4); + (self.0).0[6] = ((self.0).0[6] & 0x0f) | ((v as u8) << 4); self } - /// Hands over the internal constructed [`Uuid`]. - /// - /// # Examples - /// - /// Basic usage: - /// - /// ``` - /// # use uuid::Builder; - /// let uuid = Builder::nil().build(); - /// - /// assert_eq!( - /// uuid.to_hyphenated().to_string(), - /// "00000000-0000-0000-0000-000000000000" - /// ); - /// ``` - pub fn build(&mut self) -> Uuid { - Uuid::from_bytes(self.0) - } - - /// Convert the builder into a [`Uuid`]. + /// Get a reference to the underlying [`Uuid`]. /// /// # Examples /// @@ -716,13 +775,13 @@ impl Builder { /// # use uuid::Builder; /// let builder = Builder::nil(); /// - /// let uuid1 = builder.to_uuid(); - /// let uuid2 = builder.to_uuid(); + /// let uuid1 = builder.as_uuid(); + /// let uuid2 = builder.as_uuid(); /// /// assert_eq!(uuid1, uuid2); /// ``` - pub const fn to_uuid(&self) -> Uuid { - Uuid::from_bytes(self.0) + pub const fn as_uuid(&self) -> &Uuid { + &self.0 } /// Convert the builder into a [`Uuid`]. @@ -736,11 +795,11 @@ impl Builder { /// let uuid = Builder::nil().into_uuid(); /// /// assert_eq!( - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// "00000000-0000-0000-0000-000000000000" /// ); /// ``` pub const fn into_uuid(self) -> Uuid { - Uuid::from_bytes(self.0) + self.0 } } diff --git a/src/error.rs b/src/error.rs index 2e2f55e9..3b7fe172 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,4 @@ +#[rustfmt::skip] #[path = "../shared/error.rs"] mod imp; diff --git a/src/external/serde_support.rs b/src/external/serde_support.rs index c14bc4c5..4df850dd 100644 --- a/src/external/serde_support.rs +++ b/src/external/serde_support.rs @@ -12,8 +12,7 @@ use crate::{ error::*, fmt::{ - Braced, BracedRef, Hyphenated, HyphenatedRef, Simple, SimpleRef, Urn, - UrnRef, + Braced, Hyphenated, Simple, Urn, }, std::fmt, Uuid, @@ -30,7 +29,7 @@ impl Serialize for Uuid { ) -> Result { if serializer.is_human_readable() { serializer.serialize_str( - self.to_hyphenated() + self.hyphenated() .encode_lower(&mut Uuid::encode_buffer()), ) } else { @@ -48,15 +47,6 @@ impl Serialize for Hyphenated { } } -impl Serialize for HyphenatedRef<'_> { - fn serialize( - &self, - serializer: S, - ) -> Result { - serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer())) - } -} - impl Serialize for Simple { fn serialize( &self, @@ -66,15 +56,6 @@ impl Serialize for Simple { } } -impl Serialize for SimpleRef<'_> { - fn serialize( - &self, - serializer: S, - ) -> Result { - serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer())) - } -} - impl Serialize for Urn { fn serialize( &self, @@ -84,15 +65,6 @@ impl Serialize for Urn { } } -impl Serialize for UrnRef<'_> { - fn serialize( - &self, - serializer: S, - ) -> Result { - serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer())) - } -} - impl Serialize for Braced { fn serialize( &self, @@ -102,15 +74,6 @@ impl Serialize for Braced { } } -impl Serialize for BracedRef<'_> { - fn serialize( - &self, - serializer: S, - ) -> Result { - serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer())) - } -} - impl<'de> Deserialize<'de> for Uuid { fn deserialize>( deserializer: D, @@ -242,7 +205,7 @@ mod serde_tests { let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4"; let u = Uuid::parse_str(uuid_str).unwrap(); serde_test::assert_ser_tokens( - &u.to_hyphenated(), + &u.hyphenated(), &[Token::Str(uuid_str)], ); } @@ -251,21 +214,21 @@ mod serde_tests { fn test_serialize_simple() { let uuid_str = "f9168c5eceb24faab6bf329bf39fa1e4"; let u = Uuid::parse_str(uuid_str).unwrap(); - serde_test::assert_ser_tokens(&u.to_simple(), &[Token::Str(uuid_str)]); + serde_test::assert_ser_tokens(&u.simple(), &[Token::Str(uuid_str)]); } #[test] fn test_serialize_urn() { let uuid_str = "urn:uuid:f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4"; let u = Uuid::parse_str(uuid_str).unwrap(); - serde_test::assert_ser_tokens(&u.to_urn(), &[Token::Str(uuid_str)]); + serde_test::assert_ser_tokens(&u.urn(), &[Token::Str(uuid_str)]); } #[test] fn test_serialize_braced() { let uuid_str = "{f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4}"; let u = Uuid::parse_str(uuid_str).unwrap(); - serde_test::assert_ser_tokens(&u.to_braced(), &[Token::Str(uuid_str)]); + serde_test::assert_ser_tokens(&u.braced(), &[Token::Str(uuid_str)]); } #[test] diff --git a/src/fmt.rs b/src/fmt.rs index 981d0516..be90bafc 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -43,9 +43,9 @@ impl fmt::Display for Variant { impl fmt::LowerHex for Uuid { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if f.alternate() { - fmt::LowerHex::fmt(&self.to_simple_ref(), f) + fmt::LowerHex::fmt(self.as_simple(), f) } else { - fmt::LowerHex::fmt(&self.to_hyphenated_ref(), f) + fmt::LowerHex::fmt(self.as_hyphenated(), f) } } } @@ -54,9 +54,9 @@ impl fmt::UpperHex for Uuid { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if f.alternate() { - fmt::UpperHex::fmt(&self.to_simple_ref(), f) + fmt::UpperHex::fmt(self.as_simple(), f) } else { - fmt::UpperHex::fmt(&self.to_hyphenated_ref(), f) + fmt::UpperHex::fmt(self.as_hyphenated(), f) } } } @@ -67,95 +67,73 @@ impl fmt::UpperHex for Uuid { /// /// [`Uuid`]: ../struct.Uuid.html #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[repr(transparent)] pub struct Hyphenated(Uuid); -/// An adapter for formatting an [`Uuid`] as a hyphenated string. -/// -/// Takes a reference of the [`Uuid`]. -/// -/// [`Uuid`]: ../struct.Uuid.html -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct HyphenatedRef<'a>(&'a Uuid); - /// An adapter for formatting an [`Uuid`] as a simple string. /// /// Takes an owned instance of the [`Uuid`]. /// /// [`Uuid`]: ../struct.Uuid.html #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[repr(transparent)] pub struct Simple(Uuid); -/// An adapter for formatting an [`Uuid`] as a simple string. -/// -/// Takes a reference of the [`Uuid`]. -/// -/// [`Uuid`]: ../struct.Uuid.html -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct SimpleRef<'a>(&'a Uuid); - /// An adapter for formatting an [`Uuid`] as a URN string. /// /// Takes an owned instance of the [`Uuid`]. /// /// [`Uuid`]: ../struct.Uuid.html #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[repr(transparent)] pub struct Urn(Uuid); -/// An adapter for formatting an [`Uuid`] as a URN string. -/// -/// Takes a reference of the [`Uuid`]. -/// -/// [`Uuid`]: ../struct.Uuid.html -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct UrnRef<'a>(&'a Uuid); - /// An adapter for formatting an [`Uuid`] as a braced hyphenated string. /// /// Takes an owned instance of the [`Uuid`]. /// /// [`Uuid`]: ../struct.Uuid.html #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[repr(transparent)] pub struct Braced(Uuid); -/// An adapter for formatting an [`Uuid`] as a braced hyphenated string. -/// -/// Takes a reference of the [`Uuid`]. -/// -/// [`Uuid`]: ../struct.Uuid.html -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct BracedRef<'a>(&'a Uuid); - impl Uuid { /// Get a [`Hyphenated`] formatter. /// /// [`Hyphenated`]: adapter/struct.Hyphenated.html #[inline] - pub const fn to_hyphenated(self) -> Hyphenated { - Hyphenated::from_uuid(self) + pub const fn hyphenated(self) -> Hyphenated { + Hyphenated(self) } - /// Get a borrowed [`HyphenatedRef`] formatter. + /// Get a borrowed [`Hyphenated`] formatter. /// - /// [`HyphenatedRef`]: adapter/struct.HyphenatedRef.html + /// [`Hyphenated`]: adapter/struct.Hyphenated.html #[inline] - pub const fn to_hyphenated_ref(&self) -> HyphenatedRef<'_> { - HyphenatedRef::from_uuid_ref(self) + pub fn as_hyphenated(&self) -> &Hyphenated { + // SAFETY: `Uuid` and `Hyphenated` have the same ABI + unsafe { + &*(self as *const Uuid as *const Hyphenated) + } } /// Get a [`Simple`] formatter. /// /// [`Simple`]: adapter/struct.Simple.html #[inline] - pub const fn to_simple(self) -> Simple { - Simple::from_uuid(self) + pub const fn simple(self) -> Simple { + Simple(self) } - /// Get a borrowed [`SimpleRef`] formatter. + /// Get a borrowed [`Simple`] formatter. /// - /// [`SimpleRef`]: adapter/struct.SimpleRef.html + /// [`Simple`]: adapter/struct.Simple.html #[inline] - pub const fn to_simple_ref(&self) -> SimpleRef<'_> { - SimpleRef::from_uuid_ref(self) + pub fn as_simple(&self) -> &Simple { + // SAFETY: `Uuid` and `Simple` have the same ABI + unsafe { + &*(self as *const Uuid as *const Simple) + } } /// Get a [`Urn`] formatter. @@ -163,17 +141,20 @@ impl Uuid { /// [`Uuid`]: ../struct.Uuid.html /// [`Urn`]: adapter/struct.Urn.html #[inline] - pub const fn to_urn(self) -> Urn { - Urn::from_uuid(self) + pub const fn urn(self) -> Urn { + Urn(self) } - /// Get a borrowed [`UrnRef`] formatter. + /// Get a borrowed [`Urn`] formatter. /// /// [`Uuid`]: ../struct.Uuid.html - /// [`UrnRef`]: adapter/struct.UrnRef.html + /// [`Urn`]: adapter/struct.Urn.html #[inline] - pub const fn to_urn_ref(&self) -> UrnRef<'_> { - UrnRef::from_uuid_ref(self) + pub fn as_urn(&self) -> &Urn { + // SAFETY: `Uuid` and `Urn` have the same ABI + unsafe { + &*(self as *const Uuid as *const Urn) + } } /// Get a [`Braced`] formatter. @@ -181,17 +162,20 @@ impl Uuid { /// [`Uuid`]: ../struct.Uuid.html /// [`Braced`]: adapter/struct.Braced.html #[inline] - pub const fn to_braced(self) -> Braced { - Braced::from_uuid(self) + pub const fn braced(self) -> Braced { + Braced(self) } - /// Get a borrowed [`BracedRef`] formatter. + /// Get a borrowed [`Braced`] formatter. /// /// [`Uuid`]: ../struct.Uuid.html - /// [`BracedRef`]: adapter/struct.BracedRef.html + /// [`Braced`]: adapter/struct.Braced.html #[inline] - pub const fn to_braced_ref(&self) -> BracedRef<'_> { - BracedRef::from_uuid_ref(self) + pub fn as_braced(&self) -> &Braced { + // SAFETY: `Uuid` and `Braced` have the same ABI + unsafe { + &*(self as *const Uuid as *const Braced) + } } } @@ -300,14 +284,14 @@ impl Hyphenated { /// /// // the encoded portion is returned /// assert_eq!( - /// uuid.to_hyphenated() + /// uuid.hyphenated() /// .encode_lower(&mut Uuid::encode_buffer()), /// "936da01f-9abd-4d9d-80c7-02af85c822a8" /// ); /// /// // the buffer is mutated directly, and trailing contents remains /// let mut buf = [b'!'; 40]; - /// uuid.to_hyphenated().encode_lower(&mut buf); + /// uuid.hyphenated().encode_lower(&mut buf); /// assert_eq!( /// &buf as &[_], /// b"936da01f-9abd-4d9d-80c7-02af85c822a8!!!!" as &[_] @@ -350,14 +334,14 @@ impl Hyphenated { /// /// // the encoded portion is returned /// assert_eq!( - /// uuid.to_hyphenated() + /// uuid.hyphenated() /// .encode_upper(&mut Uuid::encode_buffer()), /// "936DA01F-9ABD-4D9D-80C7-02AF85C822A8" /// ); /// /// // the buffer is mutated directly, and trailing contents remains /// let mut buf = [b'!'; 40]; - /// uuid.to_hyphenated().encode_upper(&mut buf); + /// uuid.hyphenated().encode_upper(&mut buf); /// assert_eq!( /// &buf as &[_], /// b"936DA01F-9ABD-4D9D-80C7-02AF85C822A8!!!!" as &[_] @@ -371,140 +355,32 @@ impl Hyphenated { encode(buffer, 0, 0, &self.0, true, true) } - /// Consumes the [`Hyphenated`], returning the underlying [`Uuid`]. + /// Get a reference to the underlying [`Uuid`]. /// /// # Examples /// /// ```rust /// use uuid::Uuid; /// - /// let hyphenated = Uuid::nil().to_hyphenated(); - /// assert_eq!(hyphenated.into_inner(), Uuid::nil()); + /// let hyphenated = Uuid::nil().hyphenated(); + /// assert_eq!(*hyphenated.as_uuid(), Uuid::nil()); /// ``` - pub const fn into_inner(self) -> Uuid { - self.0 + pub const fn as_uuid(&self) -> &Uuid { + &self.0 } -} - -impl<'a> HyphenatedRef<'a> { - /// The length of a hyphenated [`Uuid`] string. - /// - /// [`Uuid`]: ../struct.Uuid.html - pub const LENGTH: usize = 36; - /// Creates a [`HyphenatedRef`] from a [`Uuid`] reference. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// [`HyphenatedRef`]: struct.HyphenatedRef.html - pub const fn from_uuid_ref(uuid: &'a Uuid) -> Self { - HyphenatedRef(uuid) - } - - /// Writes the [`Uuid`] as a lower-case hyphenated string to - /// `buffer`, and returns the subslice of the buffer that contains the - /// encoded UUID. - /// - /// This is slightly more efficient than using the formatting - /// infrastructure as it avoids virtual calls, and may avoid - /// double buffering. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// - /// # Panics - /// - /// Panics if the buffer is not large enough: it must have length at least - /// [`LENGTH`]. [`Uuid::encode_buffer`] can be used to get a - /// sufficiently-large temporary buffer. - /// - /// [`LENGTH`]: #associatedconstant.LENGTH - /// [`Uuid::encode_buffer`]: ../struct.Uuid.html#method.encode_buffer - /// - /// # Examples - /// - /// ```rust - /// use uuid::Uuid; - /// - /// fn main() -> Result<(), uuid::Error> { - /// let uuid = Uuid::parse_str("936DA01f9abd4d9d80c702af85c822a8")?; - /// - /// // the encoded portion is returned - /// assert_eq!( - /// uuid.to_hyphenated() - /// .encode_lower(&mut Uuid::encode_buffer()), - /// "936da01f-9abd-4d9d-80c7-02af85c822a8" - /// ); - /// - /// // the buffer is mutated directly, and trailing contents remains - /// let mut buf = [b'!'; 40]; - /// uuid.to_hyphenated().encode_lower(&mut buf); - /// assert_eq!( - /// uuid.to_hyphenated().encode_lower(&mut buf), - /// "936da01f-9abd-4d9d-80c7-02af85c822a8" - /// ); - /// assert_eq!( - /// &buf as &[_], - /// b"936da01f-9abd-4d9d-80c7-02af85c822a8!!!!" as &[_] - /// ); - /// - /// Ok(()) - /// } - /// ``` - /// */ - pub fn encode_lower<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { - encode(buffer, 0, 0, self.0, true, false) - } - - /// Writes the [`Uuid`] as an upper-case hyphenated string to - /// `buffer`, and returns the subslice of the buffer that contains the - /// encoded UUID. - /// - /// This is slightly more efficient than using the formatting - /// infrastructure as it avoids virtual calls, and may avoid - /// double buffering. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// - /// # Panics - /// - /// Panics if the buffer is not large enough: it must have length at least - /// [`LENGTH`]. [`Uuid::encode_buffer`] can be used to get a - /// sufficiently-large temporary buffer. - /// - /// [`LENGTH`]: #associatedconstant.LENGTH - /// [`Uuid::encode_buffer`]: ../struct.Uuid.html#method.encode_buffer + /// Consumes the [`Hyphenated`], returning the underlying [`Uuid`]. /// /// # Examples /// /// ```rust /// use uuid::Uuid; /// - /// fn main() -> Result<(), uuid::Error> { - /// let uuid = Uuid::parse_str("936da01f9abd4d9d80c702af85c822a8")?; - /// - /// // the encoded portion is returned - /// assert_eq!( - /// uuid.to_hyphenated() - /// .encode_upper(&mut Uuid::encode_buffer()), - /// "936DA01F-9ABD-4D9D-80C7-02AF85C822A8" - /// ); - /// - /// // the buffer is mutated directly, and trailing contents remains - /// let mut buf = [b'!'; 40]; - /// assert_eq!( - /// uuid.to_hyphenated().encode_upper(&mut buf), - /// "936DA01F-9ABD-4D9D-80C7-02AF85C822A8" - /// ); - /// assert_eq!( - /// &buf as &[_], - /// b"936DA01F-9ABD-4D9D-80C7-02AF85C822A8!!!!" as &[_] - /// ); - /// - /// Ok(()) - /// } + /// let hyphenated = Uuid::nil().hyphenated(); + /// assert_eq!(hyphenated.into_uuid(), Uuid::nil()); /// ``` - /// */ - pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { - encode(buffer, 0, 0, self.0, true, true) + pub const fn into_uuid(self) -> Uuid { + self.0 } } @@ -551,14 +427,14 @@ impl Braced { /// /// // the encoded portion is returned /// assert_eq!( - /// uuid.to_braced() + /// uuid.braced() /// .encode_lower(&mut Uuid::encode_buffer()), /// "{936da01f-9abd-4d9d-80c7-02af85c822a8}" /// ); /// /// // the buffer is mutated directly, and trailing contents remains /// let mut buf = [b'!'; 40]; - /// uuid.to_braced().encode_lower(&mut buf); + /// uuid.braced().encode_lower(&mut buf); /// assert_eq!( /// &buf as &[_], /// b"{936da01f-9abd-4d9d-80c7-02af85c822a8}!!" as &[_] @@ -603,14 +479,14 @@ impl Braced { /// /// // the encoded portion is returned /// assert_eq!( - /// uuid.to_braced() + /// uuid.braced() /// .encode_upper(&mut Uuid::encode_buffer()), /// "{936DA01F-9ABD-4D9D-80C7-02AF85C822A8}" /// ); /// /// // the buffer is mutated directly, and trailing contents remains /// let mut buf = [b'!'; 40]; - /// uuid.to_braced().encode_upper(&mut buf); + /// uuid.braced().encode_upper(&mut buf); /// assert_eq!( /// &buf as &[_], /// b"{936DA01F-9ABD-4D9D-80C7-02AF85C822A8}!!" as &[_] @@ -626,137 +502,32 @@ impl Braced { encode(buffer, 1, 1, &self.0, true, true) } - /// Consumes the [`Braced`], returning the underlying [`Uuid`]. + /// Get a reference to the underlying [`Uuid`]. /// /// # Examples /// /// ```rust /// use uuid::Uuid; /// - /// let braced = Uuid::nil().to_braced(); - /// assert_eq!(braced.into_inner(), Uuid::nil()); + /// let braced = Uuid::nil().braced(); + /// assert_eq!(*braced.as_uuid(), Uuid::nil()); /// ``` - pub const fn into_inner(self) -> Uuid { - self.0 + pub const fn as_uuid(&self) -> &Uuid { + &self.0 } -} -impl<'a> BracedRef<'a> { - /// The length of a braced [`Uuid`] string. - /// - /// [`Uuid`]: ../struct.Uuid.html - pub const LENGTH: usize = 38; - - /// Creates a [`BracedRef`] from a [`Uuid`] reference. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// [`BracedRef`]: struct.BracedRef.html - pub const fn from_uuid_ref(uuid: &'a Uuid) -> Self { - BracedRef(uuid) - } - - /// Writes the [`Uuid`] as a lower-case hyphenated string surrounded by - /// braces to `buffer`, and returns the subslice of the buffer that contains - /// the encoded UUID. - /// - /// This is slightly more efficient than using the formatting - /// infrastructure as it avoids virtual calls, and may avoid - /// double buffering. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// - /// # Panics - /// - /// Panics if the buffer is not large enough: it must have length at least - /// [`LENGTH`]. [`Uuid::encode_buffer`] can be used to get a - /// sufficiently-large temporary buffer. - /// - /// [`LENGTH`]: #associatedconstant.LENGTH - /// [`Uuid::encode_buffer`]: ../struct.Uuid.html#method.encode_buffer - /// - /// # Examples - /// - /// ```rust - /// use uuid::Uuid; - /// - /// fn main() -> Result<(), uuid::Error> { - /// let uuid = Uuid::parse_str("936DA01f9abd4d9d80c702af85c822a8")?; - /// - /// // the encoded portion is returned - /// assert_eq!( - /// uuid.to_braced() - /// .encode_lower(&mut Uuid::encode_buffer()), - /// "{936da01f-9abd-4d9d-80c7-02af85c822a8}" - /// ); - /// - /// // the buffer is mutated directly, and trailing contents remains - /// let mut buf = [b'!'; 40]; - /// uuid.to_braced().encode_lower(&mut buf); - /// assert_eq!( - /// &buf as &[_], - /// b"{936da01f-9abd-4d9d-80c7-02af85c822a8}!!" as &[_] - /// ); - /// - /// Ok(()) - /// } - /// ``` - /// */ - pub fn encode_lower<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { - buffer[0] = b'{'; - buffer[37] = b'}'; - encode(buffer, 1, 1, self.0, true, false) - } - - /// Writes the [`Uuid`] as an upper-case hyphenated string surrounded by - /// braces to `buffer`, and returns the subslice of the buffer that contains - /// the encoded UUID. - /// - /// This is slightly more efficient than using the formatting - /// infrastructure as it avoids virtual calls, and may avoid - /// double buffering. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// - /// # Panics - /// - /// Panics if the buffer is not large enough: it must have length at least - /// [`LENGTH`]. [`Uuid::encode_buffer`] can be used to get a - /// sufficiently-large temporary buffer. - /// - /// [`LENGTH`]: #associatedconstant.LENGTH - /// [`Uuid::encode_buffer`]: ../struct.Uuid.html#method.encode_buffer + /// Consumes the [`Braced`], returning the underlying [`Uuid`]. /// /// # Examples /// /// ```rust /// use uuid::Uuid; /// - /// fn main() -> Result<(), uuid::Error> { - /// let uuid = Uuid::parse_str("936da01f9abd4d9d80c702af85c822a8")?; - /// - /// // the encoded portion is returned - /// assert_eq!( - /// uuid.to_braced() - /// .encode_upper(&mut Uuid::encode_buffer()), - /// "{936DA01F-9ABD-4D9D-80C7-02AF85C822A8}" - /// ); - /// - /// // the buffer is mutated directly, and trailing contents remains - /// let mut buf = [b'!'; 40]; - /// uuid.to_braced().encode_upper(&mut buf); - /// assert_eq!( - /// &buf as &[_], - /// b"{936DA01F-9ABD-4D9D-80C7-02AF85C822A8}!!" as &[_] - /// ); - /// - /// Ok(()) - /// } + /// let braced = Uuid::nil().braced(); + /// assert_eq!(braced.into_uuid(), Uuid::nil()); /// ``` - /// */ - pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { - buffer[0] = b'{'; - buffer[37] = b'}'; - encode(buffer, 1, 1, self.0, true, true) + pub const fn into_uuid(self) -> Uuid { + self.0 } } @@ -802,14 +573,14 @@ impl Simple { /// /// // the encoded portion is returned /// assert_eq!( - /// uuid.to_simple().encode_lower(&mut Uuid::encode_buffer()), + /// uuid.simple().encode_lower(&mut Uuid::encode_buffer()), /// "936da01f9abd4d9d80c702af85c822a8" /// ); /// /// // the buffer is mutated directly, and trailing contents remains /// let mut buf = [b'!'; 36]; /// assert_eq!( - /// uuid.to_simple().encode_lower(&mut buf), + /// uuid.simple().encode_lower(&mut buf), /// "936da01f9abd4d9d80c702af85c822a8" /// ); /// assert_eq!( @@ -849,14 +620,14 @@ impl Simple { /// /// // the encoded portion is returned /// assert_eq!( - /// uuid.to_simple().encode_upper(&mut Uuid::encode_buffer()), + /// uuid.simple().encode_upper(&mut Uuid::encode_buffer()), /// "936DA01F9ABD4D9D80C702AF85C822A8" /// ); /// /// // the buffer is mutated directly, and trailing contents remains /// let mut buf = [b'!'; 36]; /// assert_eq!( - /// uuid.to_simple().encode_upper(&mut buf), + /// uuid.simple().encode_upper(&mut buf), /// "936DA01F9ABD4D9D80C702AF85C822A8" /// ); /// assert_eq!( @@ -872,131 +643,32 @@ impl Simple { encode(buffer, 0, 0, &self.0, false, true) } - /// Consumes the [`Simple`], returning the underlying [`Uuid`]. + /// Get a reference to the underlying [`Uuid`]. /// /// # Examples /// /// ```rust /// use uuid::Uuid; /// - /// let simple = Uuid::nil().to_simple(); - /// assert_eq!(simple.into_inner(), Uuid::nil()); + /// let simple = Uuid::nil().simple(); + /// assert_eq!(*simple.as_uuid(), Uuid::nil()); /// ``` - pub const fn into_inner(self) -> Uuid { - self.0 + pub const fn as_uuid(&self) -> &Uuid { + &self.0 } -} -impl<'a> SimpleRef<'a> { - /// The length of a simple [`Uuid`] string. - /// - /// [`Uuid`]: ../struct.Uuid.html - pub const LENGTH: usize = 32; - - /// Creates a [`SimpleRef`] from a [`Uuid`] reference. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// [`SimpleRef`]: struct.SimpleRef.html - pub const fn from_uuid_ref(uuid: &'a Uuid) -> Self { - SimpleRef(uuid) - } - - /// Writes the [`Uuid`] as a lower-case simple string to `buffer`, - /// and returns the subslice of the buffer that contains the encoded UUID. - /// - /// This is slightly more efficient than using the formatting - /// infrastructure as it avoids virtual calls, and may avoid - /// double buffering. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// - /// # Panics - /// - /// Panics if the buffer is not large enough: it must have length at least - /// [`LENGTH`]. [`Uuid::encode_buffer`] can be used to get a - /// sufficiently-large temporary buffer. - /// - /// [`LENGTH`]: #associatedconstant.LENGTH - /// [`Uuid::encode_buffer`]: ../struct.Uuid.html#method.encode_buffer - /// - /// # Examples - /// - /// ```rust - /// use uuid::Uuid; - /// - /// fn main() -> Result<(), uuid::Error> { - /// let uuid = Uuid::parse_str("936DA01f9abd4d9d80c702af85c822a8")?; - /// - /// // the encoded portion is returned - /// assert_eq!( - /// uuid.to_simple().encode_lower(&mut Uuid::encode_buffer()), - /// "936da01f9abd4d9d80c702af85c822a8" - /// ); - /// - /// // the buffer is mutated directly, and trailing contents remains - /// let mut buf = [b'!'; 36]; - /// assert_eq!( - /// uuid.to_simple().encode_lower(&mut buf), - /// "936da01f9abd4d9d80c702af85c822a8" - /// ); - /// assert_eq!( - /// &buf as &[_], - /// b"936da01f9abd4d9d80c702af85c822a8!!!!" as &[_] - /// ); - /// - /// Ok(()) - /// } - /// ``` - /// */ - pub fn encode_lower<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { - encode(buffer, 0, 0, self.0, false, false) - } - - /// Writes the [`Uuid`] as an upper-case simple string to `buffer`, - /// and returns the subslice of the buffer that contains the encoded UUID. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// - /// # Panics - /// - /// Panics if the buffer is not large enough: it must have length at least - /// [`LENGTH`]. [`Uuid::encode_buffer`] can be used to get a - /// sufficiently-large temporary buffer. - /// - /// [`LENGTH`]: #associatedconstant.LENGTH - /// [`Uuid::encode_buffer`]: ../struct.Uuid.html#method.encode_buffer + /// Consumes the [`Simple`], returning the underlying [`Uuid`]. /// /// # Examples /// /// ```rust /// use uuid::Uuid; /// - /// fn main() -> Result<(), uuid::Error> { - /// let uuid = Uuid::parse_str("936da01f9abd4d9d80c702af85c822a8")?; - /// - /// // the encoded portion is returned - /// assert_eq!( - /// uuid.to_simple().encode_upper(&mut Uuid::encode_buffer()), - /// "936DA01F9ABD4D9D80C702AF85C822A8" - /// ); - /// - /// // the buffer is mutated directly, and trailing contents remains - /// let mut buf = [b'!'; 36]; - /// assert_eq!( - /// uuid.to_simple().encode_upper(&mut buf), - /// "936DA01F9ABD4D9D80C702AF85C822A8" - /// ); - /// assert_eq!( - /// &buf as &[_], - /// b"936DA01F9ABD4D9D80C702AF85C822A8!!!!" as &[_] - /// ); - /// - /// Ok(()) - /// } + /// let simple = Uuid::nil().simple(); + /// assert_eq!(simple.into_uuid(), Uuid::nil()); /// ``` - /// */ - pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { - encode(buffer, 0, 0, self.0, false, true) + pub const fn into_uuid(self) -> Uuid { + self.0 } } @@ -1043,15 +715,15 @@ impl Urn { /// /// // the encoded portion is returned /// assert_eq!( - /// uuid.to_urn().encode_lower(&mut Uuid::encode_buffer()), + /// uuid.urn().encode_lower(&mut Uuid::encode_buffer()), /// "urn:uuid:936da01f-9abd-4d9d-80c7-02af85c822a8" /// ); /// /// // the buffer is mutated directly, and trailing contents remains /// let mut buf = [b'!'; 49]; - /// uuid.to_urn().encode_lower(&mut buf); + /// uuid.urn().encode_lower(&mut buf); /// assert_eq!( - /// uuid.to_urn().encode_lower(&mut buf), + /// uuid.urn().encode_lower(&mut buf), /// "urn:uuid:936da01f-9abd-4d9d-80c7-02af85c822a8" /// ); /// assert_eq!( @@ -1097,14 +769,14 @@ impl Urn { /// /// // the encoded portion is returned /// assert_eq!( - /// uuid.to_urn().encode_upper(&mut Uuid::encode_buffer()), + /// uuid.urn().encode_upper(&mut Uuid::encode_buffer()), /// "urn:uuid:936DA01F-9ABD-4D9D-80C7-02AF85C822A8" /// ); /// /// // the buffer is mutated directly, and trailing contents remains /// let mut buf = [b'!'; 49]; /// assert_eq!( - /// uuid.to_urn().encode_upper(&mut buf), + /// uuid.urn().encode_upper(&mut buf), /// "urn:uuid:936DA01F-9ABD-4D9D-80C7-02AF85C822A8" /// ); /// assert_eq!( @@ -1121,140 +793,32 @@ impl Urn { encode(buffer, 9, 0, &self.0, true, true) } - /// Consumes the [`Urn`], returning the underlying [`Uuid`]. - /// - /// # Examples - /// - /// ```rust - /// use uuid::Uuid; - /// - /// let urn = Uuid::nil().to_urn(); - /// assert_eq!(urn.into_inner(), Uuid::nil()); - /// ``` - pub const fn into_inner(self) -> Uuid { - self.0 - } -} - -impl<'a> UrnRef<'a> { - /// The length of a URN [`Uuid`] string. - /// - /// [`Uuid`]: ../struct.Uuid.html - pub const LENGTH: usize = 45; - - /// Creates a [`UrnRef`] from a [`Uuid`] reference. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// [`UrnRef`]: struct.UrnRef.html - pub const fn from_uuid_ref(uuid: &'a Uuid) -> Self { - UrnRef(uuid) - } - - /// Writes the [`Uuid`] as a lower-case URN string to - /// `buffer`, and returns the subslice of the buffer that contains the - /// encoded UUID. - /// - /// This is slightly more efficient than using the formatting - /// infrastructure as it avoids virtual calls, and may avoid - /// double buffering. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// - /// # Panics - /// - /// Panics if the buffer is not large enough: it must have length at least - /// [`LENGTH`]. [`Uuid::encode_buffer`] can be used to get a - /// sufficiently-large temporary buffer. - /// - /// [`LENGTH`]: #associatedconstant.LENGTH - /// [`Uuid::encode_buffer`]: ../struct.Uuid.html#method.encode_buffer + /// Get a reference to the underlying [`Uuid`]. /// /// # Examples /// /// ```rust /// use uuid::Uuid; /// - /// fn main() -> Result<(), uuid::Error> { - /// let uuid = Uuid::parse_str("936DA01f9abd4d9d80c702af85c822a8")?; - /// - /// // the encoded portion is returned - /// assert_eq!( - /// uuid.to_urn().encode_lower(&mut Uuid::encode_buffer()), - /// "urn:uuid:936da01f-9abd-4d9d-80c7-02af85c822a8" - /// ); - /// - /// // the buffer is mutated directly, and trailing contents remains - /// let mut buf = [b'!'; 49]; - /// uuid.to_urn().encode_lower(&mut buf); - /// assert_eq!( - /// uuid.to_urn().encode_lower(&mut buf), - /// "urn:uuid:936da01f-9abd-4d9d-80c7-02af85c822a8" - /// ); - /// assert_eq!( - /// &buf as &[_], - /// b"urn:uuid:936da01f-9abd-4d9d-80c7-02af85c822a8!!!!" as &[_] - /// ); - /// - /// Ok(()) - /// } + /// let urn = Uuid::nil().urn(); + /// assert_eq!(*urn.as_uuid(), Uuid::nil()); /// ``` - /// */ - pub fn encode_lower<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { - buffer[..9].copy_from_slice(b"urn:uuid:"); - encode(buffer, 9, 0, self.0, true, false) + pub const fn as_uuid(&self) -> &Uuid { + &self.0 } - /// Writes the [`Uuid`] as an upper-case URN string to - /// `buffer`, and returns the subslice of the buffer that contains the - /// encoded UUID. - /// - /// This is slightly more efficient than using the formatting - /// infrastructure as it avoids virtual calls, and may avoid - /// double buffering. - /// - /// [`Uuid`]: ../struct.Uuid.html - /// - /// # Panics - /// - /// Panics if the buffer is not large enough: it must have length at least - /// [`LENGTH`]. [`Uuid::encode_buffer`] can be used to get a - /// sufficiently-large temporary buffer. - /// - /// [`LENGTH`]: #associatedconstant.LENGTH - /// [`Uuid::encode_buffer`]: ../struct.Uuid.html#method.encode_buffer + /// Consumes the [`Urn`], returning the underlying [`Uuid`]. /// /// # Examples /// /// ```rust /// use uuid::Uuid; /// - /// fn main() -> Result<(), uuid::Error> { - /// let uuid = Uuid::parse_str("936da01f9abd4d9d80c702af85c822a8")?; - /// - /// // the encoded portion is returned - /// assert_eq!( - /// uuid.to_urn().encode_upper(&mut Uuid::encode_buffer()), - /// "urn:uuid:936DA01F-9ABD-4D9D-80C7-02AF85C822A8" - /// ); - /// - /// // the buffer is mutated directly, and trailing contents remains - /// let mut buf = [b'!'; 49]; - /// assert_eq!( - /// uuid.to_urn().encode_upper(&mut buf), - /// "urn:uuid:936DA01F-9ABD-4D9D-80C7-02AF85C822A8" - /// ); - /// assert_eq!( - /// &buf as &[_], - /// b"urn:uuid:936DA01F-9ABD-4D9D-80C7-02AF85C822A8!!!!" as &[_] - /// ); - /// - /// Ok(()) - /// } + /// let urn = Uuid::nil().urn(); + /// assert_eq!(urn.into_uuid(), Uuid::nil()); /// ``` - /// */ - pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { - buffer[..9].copy_from_slice(b"urn:uuid:"); - encode(buffer, 9, 0, self.0, true, true) + pub const fn into_uuid(self) -> Uuid { + self.0 } } @@ -1288,14 +852,14 @@ macro_rules! impl_fmt_from { impl From for $T { #[inline] fn from(f: Uuid) -> Self { - $T::from_uuid(f) + $T(f) } } impl From<$T> for Uuid { #[inline] fn from(f: $T) -> Self { - f.into_inner() + f.into_uuid() } } @@ -1346,13 +910,9 @@ macro_rules! impl_fmt_from { impl_fmt_traits! { Hyphenated<>, - HyphenatedRef<'a>, Simple<>, - SimpleRef<'a>, Urn<>, - UrnRef<'a>, - Braced<>, - BracedRef<'a> + Braced<> } #[cfg(test)] @@ -1362,7 +922,7 @@ mod tests { #[test] fn hyphenated_trailing() { let mut buf = [b'x'; 100]; - let len = Uuid::nil().to_hyphenated().encode_lower(&mut buf).len(); + let len = Uuid::nil().hyphenated().encode_lower(&mut buf).len(); assert_eq!(len, super::Hyphenated::LENGTH); assert!(buf[len..].iter().all(|x| *x == b'x')); } @@ -1370,15 +930,15 @@ mod tests { #[test] fn hyphenated_ref_trailing() { let mut buf = [b'x'; 100]; - let len = Uuid::nil().to_hyphenated().encode_lower(&mut buf).len(); - assert_eq!(len, super::HyphenatedRef::LENGTH); + let len = Uuid::nil().as_hyphenated().encode_lower(&mut buf).len(); + assert_eq!(len, super::Hyphenated::LENGTH); assert!(buf[len..].iter().all(|x| *x == b'x')); } #[test] fn simple_trailing() { let mut buf = [b'x'; 100]; - let len = Uuid::nil().to_simple().encode_lower(&mut buf).len(); + let len = Uuid::nil().simple().encode_lower(&mut buf).len(); assert_eq!(len, super::Simple::LENGTH); assert!(buf[len..].iter().all(|x| *x == b'x')); } @@ -1386,15 +946,15 @@ mod tests { #[test] fn simple_ref_trailing() { let mut buf = [b'x'; 100]; - let len = Uuid::nil().to_simple().encode_lower(&mut buf).len(); - assert_eq!(len, super::SimpleRef::LENGTH); + let len = Uuid::nil().as_simple().encode_lower(&mut buf).len(); + assert_eq!(len, super::Simple::LENGTH); assert!(buf[len..].iter().all(|x| *x == b'x')); } #[test] fn urn_trailing() { let mut buf = [b'x'; 100]; - let len = Uuid::nil().to_urn().encode_lower(&mut buf).len(); + let len = Uuid::nil().urn().encode_lower(&mut buf).len(); assert_eq!(len, super::Urn::LENGTH); assert!(buf[len..].iter().all(|x| *x == b'x')); } @@ -1402,15 +962,15 @@ mod tests { #[test] fn urn_ref_trailing() { let mut buf = [b'x'; 100]; - let len = Uuid::nil().to_urn().encode_lower(&mut buf).len(); - assert_eq!(len, super::UrnRef::LENGTH); + let len = Uuid::nil().as_urn().encode_lower(&mut buf).len(); + assert_eq!(len, super::Urn::LENGTH); assert!(buf[len..].iter().all(|x| *x == b'x')); } #[test] fn braced_trailing() { let mut buf = [b'x'; 100]; - let len = Uuid::nil().to_braced().encode_lower(&mut buf).len(); + let len = Uuid::nil().braced().encode_lower(&mut buf).len(); assert_eq!(len, super::Braced::LENGTH); assert!(buf[len..].iter().all(|x| *x == b'x')); } @@ -1418,110 +978,56 @@ mod tests { #[test] fn braced_ref_trailing() { let mut buf = [b'x'; 100]; - let len = Uuid::nil().to_braced().encode_lower(&mut buf).len(); - assert_eq!(len, super::BracedRef::LENGTH); + let len = Uuid::nil().as_braced().encode_lower(&mut buf).len(); + assert_eq!(len, super::Braced::LENGTH); assert!(buf[len..].iter().all(|x| *x == b'x')); } #[test] #[should_panic] fn hyphenated_too_small() { - Uuid::nil().to_hyphenated().encode_lower(&mut [0; 35]); - } - - #[test] - #[should_panic] - fn hyphenated_ref_too_small() { - Uuid::nil().to_hyphenated_ref().encode_lower(&mut [0; 35]); + Uuid::nil().hyphenated().encode_lower(&mut [0; 35]); } #[test] #[should_panic] fn simple_too_small() { - Uuid::nil().to_simple().encode_lower(&mut [0; 31]); - } - #[test] - #[should_panic] - fn simple_ref_too_small() { - Uuid::nil().to_simple_ref().encode_lower(&mut [0; 31]); + Uuid::nil().simple().encode_lower(&mut [0; 31]); } #[test] #[should_panic] fn urn_too_small() { - Uuid::nil().to_urn().encode_lower(&mut [0; 44]); - } - - #[test] - #[should_panic] - fn urn_ref_too_small() { - Uuid::nil().to_urn_ref().encode_lower(&mut [0; 44]); + Uuid::nil().urn().encode_lower(&mut [0; 44]); } #[test] #[should_panic] fn braced_too_small() { - Uuid::nil().to_braced().encode_lower(&mut [0; 37]); - } - #[test] - #[should_panic] - fn braced_ref_too_small() { - Uuid::nil().to_braced_ref().encode_lower(&mut [0; 37]); + Uuid::nil().braced().encode_lower(&mut [0; 37]); } #[test] fn hyphenated_to_inner() { - let hyphenated = Uuid::nil().to_hyphenated(); + let hyphenated = Uuid::nil().hyphenated(); assert_eq!(Uuid::from(hyphenated), Uuid::nil()); } - #[test] - fn hyphenated_ref_to_inner() { - let uuid = Uuid::nil(); - let hyphenated_ref = uuid.to_hyphenated_ref(); - assert_eq!(<&Uuid>::from(hyphenated_ref), &Uuid::nil()); - } - #[test] fn simple_to_inner() { - let simple = Uuid::nil().to_simple(); + let simple = Uuid::nil().simple(); assert_eq!(Uuid::from(simple), Uuid::nil()); } - #[test] - fn simple_ref_to_inner() { - let uuid = Uuid::nil(); - let simple_ref = uuid.to_simple_ref(); - assert_eq!(simple_ref.as_ref(), &Uuid::nil()); - } - #[test] fn urn_to_inner() { - let urn = Uuid::nil().to_urn(); + let urn = Uuid::nil().urn(); assert_eq!(Uuid::from(urn), Uuid::nil()); } - #[test] - fn urn_ref_to_inner() { - use std::borrow::Borrow; - let uuid = Uuid::nil(); - let urn_ref = uuid.to_urn_ref(); - let borrowed: &Uuid = urn_ref.borrow(); - assert_eq!(borrowed, &Uuid::nil()); - } - #[test] fn braced_to_inner() { - let braced = Uuid::nil().to_braced(); + let braced = Uuid::nil().braced(); assert_eq!(Uuid::from(braced), Uuid::nil()); } - - #[test] - fn braced_ref_to_inner() { - use std::borrow::Borrow; - let uuid = Uuid::nil(); - let braced_ref = uuid.to_braced_ref(); - let borrowed: &Uuid = braced_ref.borrow(); - assert_eq!(borrowed, &Uuid::nil()); - } } diff --git a/src/lib.rs b/src/lib.rs index 7e23d557..3566bdab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -132,7 +132,7 @@ //! # fn main() -> Result<(), Box> { //! let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?; //! -//! println!("{}", my_uuid.to_urn()); +//! println!("{}", my_uuid.urn()); //! # Ok(()) //! # } //! ``` @@ -266,7 +266,7 @@ pub enum Variant { /// # fn main() -> Result<(), Box> { /// let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?; /// -/// println!("{}", my_uuid.to_urn()); +/// println!("{}", my_uuid.urn()); /// # Ok(()) /// # } /// ``` @@ -290,7 +290,7 @@ pub enum Variant { /// A UUID can be formatted in one of a few ways: /// /// * [`simple`](#method.to_simple): `a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8`. -/// * [`hyphenated`](#method.to_hyphenated): +/// * [`hyphenated`](#method.hyphenated()): /// `a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8`. /// * [`urn`](#method.to_urn): `urn:uuid:A1A2A3A4-B1B2-C1C2-D1D2-D3D4D5D6D7D8`. /// @@ -319,7 +319,7 @@ pub enum Variant { /// /// assert_eq!( /// "urn:uuid:a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", -/// my_uuid.to_urn().to_string(), +/// my_uuid.urn().to_string(), /// ); /// # Ok(()) /// # } @@ -389,6 +389,20 @@ impl Uuid { /// Callers should only trust the value returned by this method if they /// trust the UUID itself. /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// # use uuid::{Uuid, Variant}; + /// # fn main() -> Result<(), Box> { + /// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?; + /// + /// assert_eq!(Variant::RFC4122, my_uuid.get_variant()); + /// # Ok(()) + /// # } + /// ``` + /// /// # References /// /// * [Variant in RFC4122](http://tools.ietf.org/html/rfc4122#section-4.1.1) @@ -410,6 +424,20 @@ impl Uuid { /// This represents the algorithm used to generate the contents. /// This method is the future-proof alternative to [`Uuid::get_version`]. /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// # use uuid::Uuid; + /// # fn main() -> Result<(), Box> { + /// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?; + /// + /// assert_eq!(3, my_uuid.get_version_num()); + /// # Ok(()) + /// # } + /// ``` + /// /// # References /// /// * [Version in RFC4122](https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.3) @@ -425,6 +453,20 @@ impl Uuid { /// you can also use [`Uuid::get_version_num`] to unconditionally return a /// number. /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// # use uuid::{Uuid, Version}; + /// # fn main() -> Result<(), Box> { + /// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?; + /// + /// assert_eq!(Some(Version::Md5), my_uuid.get_version()); + /// # Ok(()) + /// # } + /// ``` + /// /// # References /// /// * [Version in RFC4122](https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.3) @@ -667,18 +709,18 @@ impl Uuid { /// let uuid = Uuid::nil(); /// /// assert_eq!( - /// uuid.to_simple().encode_lower(&mut Uuid::encode_buffer()), + /// uuid.simple().encode_lower(&mut Uuid::encode_buffer()), /// "00000000000000000000000000000000" /// ); /// /// assert_eq!( - /// uuid.to_hyphenated() + /// uuid.hyphenated() /// .encode_lower(&mut Uuid::encode_buffer()), /// "00000000-0000-0000-0000-000000000000" /// ); /// /// assert_eq!( - /// uuid.to_urn().encode_lower(&mut Uuid::encode_buffer()), + /// uuid.urn().encode_lower(&mut Uuid::encode_buffer()), /// "urn:uuid:00000000-0000-0000-0000-000000000000" /// ); /// ``` @@ -764,7 +806,7 @@ mod tests { let s = uuid.to_string(); let mut buffer = String::new(); - assert_eq!(s, uuid.to_hyphenated().to_string()); + assert_eq!(s, uuid.hyphenated().to_string()); check!(buffer, "{}", uuid, 36, |c| c.is_lowercase() || c.is_digit(10) @@ -840,19 +882,19 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_predefined_namespaces() { assert_eq!( - Uuid::NAMESPACE_DNS.to_hyphenated().to_string(), + Uuid::NAMESPACE_DNS.hyphenated().to_string(), "6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); assert_eq!( - Uuid::NAMESPACE_URL.to_hyphenated().to_string(), + Uuid::NAMESPACE_URL.hyphenated().to_string(), "6ba7b811-9dad-11d1-80b4-00c04fd430c8" ); assert_eq!( - Uuid::NAMESPACE_OID.to_hyphenated().to_string(), + Uuid::NAMESPACE_OID.hyphenated().to_string(), "6ba7b812-9dad-11d1-80b4-00c04fd430c8" ); assert_eq!( - Uuid::NAMESPACE_X500.to_hyphenated().to_string(), + Uuid::NAMESPACE_X500.hyphenated().to_string(), "6ba7b814-9dad-11d1-80b4-00c04fd430c8" ); } @@ -895,7 +937,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_to_simple_string() { let uuid1 = new(); - let s = uuid1.to_simple().to_string(); + let s = uuid1.simple().to_string(); assert_eq!(s.len(), 32); assert!(s.chars().all(|c| c.is_digit(16))); @@ -903,9 +945,9 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn test_to_hyphenated_string() { + fn test_hyphenated_string() { let uuid1 = new(); - let s = uuid1.to_hyphenated().to_string(); + let s = uuid1.hyphenated().to_string(); assert!(s.len() == 36); assert!(s.chars().all(|c| c.is_digit(16) || c == '-')); @@ -937,26 +979,26 @@ mod tests { check!(buf, "{:#x}", u, 32, |c| c.is_lowercase() || c.is_digit(10)); check!(buf, "{:#X}", u, 32, |c| c.is_uppercase() || c.is_digit(10)); - check!(buf, "{:X}", u.to_hyphenated(), 36, |c| c.is_uppercase() + check!(buf, "{:X}", u.hyphenated(), 36, |c| c.is_uppercase() || c.is_digit(10) || c == '-'); - check!(buf, "{:X}", u.to_simple(), 32, |c| c.is_uppercase() + check!(buf, "{:X}", u.simple(), 32, |c| c.is_uppercase() || c.is_digit(10)); - check!(buf, "{:#X}", u.to_hyphenated(), 36, |c| c.is_uppercase() + check!(buf, "{:#X}", u.hyphenated(), 36, |c| c.is_uppercase() || c.is_digit(10) || c == '-'); - check!(buf, "{:#X}", u.to_simple(), 32, |c| c.is_uppercase() + check!(buf, "{:#X}", u.simple(), 32, |c| c.is_uppercase() || c.is_digit(10)); - check!(buf, "{:x}", u.to_hyphenated(), 36, |c| c.is_lowercase() + check!(buf, "{:x}", u.hyphenated(), 36, |c| c.is_lowercase() || c.is_digit(10) || c == '-'); - check!(buf, "{:x}", u.to_simple(), 32, |c| c.is_lowercase() + check!(buf, "{:x}", u.simple(), 32, |c| c.is_lowercase() || c.is_digit(10)); - check!(buf, "{:#x}", u.to_hyphenated(), 36, |c| c.is_lowercase() + check!(buf, "{:#x}", u.hyphenated(), 36, |c| c.is_lowercase() || c.is_digit(10) || c == '-'); - check!(buf, "{:#x}", u.to_simple(), 32, |c| c.is_lowercase() + check!(buf, "{:#x}", u.simple(), 32, |c| c.is_lowercase() || c.is_digit(10)); } @@ -964,7 +1006,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_to_urn_string() { let uuid1 = new(); - let ss = uuid1.to_urn().to_string(); + let ss = uuid1.urn().to_string(); let s = &ss[9..]; assert!(ss.starts_with("urn:uuid:")); @@ -977,8 +1019,8 @@ mod tests { fn test_to_simple_string_matching() { let uuid1 = new(); - let hs = uuid1.to_hyphenated().to_string(); - let ss = uuid1.to_simple().to_string(); + let hs = uuid1.hyphenated().to_string(); + let ss = uuid1.simple().to_string(); let hsn = hs.chars().filter(|&c| c != '-').collect::(); @@ -990,7 +1032,7 @@ mod tests { fn test_string_roundtrip() { let uuid = new(); - let hs = uuid.to_hyphenated().to_string(); + let hs = uuid.hyphenated().to_string(); let uuid_hs = Uuid::parse_str(&hs).unwrap(); assert_eq!(uuid_hs, uuid); @@ -1010,7 +1052,7 @@ mod tests { let u = Uuid::from_fields(d1, d2, d3, &d4); let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; - let result = u.to_simple().to_string(); + let result = u.simple().to_string(); assert_eq!(result, expected); } @@ -1025,7 +1067,7 @@ mod tests { let u = Uuid::from_fields_le(d1, d2, d3, &d4); let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; - let result = u.to_simple().to_string(); + let result = u.simple().to_string(); assert_eq!(result, expected); } @@ -1101,7 +1143,7 @@ mod tests { let u = Uuid::from_u128(v_in); let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; - let result = u.to_simple().to_string(); + let result = u.simple().to_string(); assert_eq!(result, expected); } @@ -1113,7 +1155,7 @@ mod tests { let u = Uuid::from_u128_le(v_in); let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; - let result = u.to_simple().to_string(); + let result = u.simple().to_string(); assert_eq!(result, expected); } @@ -1126,7 +1168,7 @@ mod tests { let u = Uuid::from_u64_pair(high_in, low_in); let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; - let result = u.to_simple().to_string(); + let result = u.simple().to_string(); assert_eq!(result, expected); } @@ -1187,7 +1229,7 @@ mod tests { let u = Uuid::from_slice(&b).unwrap(); let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; - assert_eq!(u.to_simple().to_string(), expected); + assert_eq!(u.simple().to_string(), expected); } #[test] @@ -1201,7 +1243,7 @@ mod tests { let u = Uuid::from_bytes(b); let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; - assert_eq!(u.to_simple().to_string(), expected); + assert_eq!(u.simple().to_string(), expected); } #[test] diff --git a/src/parser.rs b/src/parser.rs index 28600635..064aa966 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -19,6 +19,7 @@ use crate::{ Uuid, }; +#[rustfmt::skip] #[path = "../shared/parser.rs"] mod imp; @@ -330,7 +331,7 @@ mod tests { #[test] fn test_roundtrip_hyphenated() { let uuid_orig = new(); - let orig_str = uuid_orig.to_hyphenated().to_string(); + let orig_str = uuid_orig.hyphenated().to_string(); let uuid_out = Uuid::parse_str(&orig_str).unwrap(); assert_eq!(uuid_orig, uuid_out); } @@ -338,7 +339,7 @@ mod tests { #[test] fn test_roundtrip_simple() { let uuid_orig = new(); - let orig_str = uuid_orig.to_simple().to_string(); + let orig_str = uuid_orig.simple().to_string(); let uuid_out = Uuid::parse_str(&orig_str).unwrap(); assert_eq!(uuid_orig, uuid_out); } @@ -346,7 +347,7 @@ mod tests { #[test] fn test_roundtrip_urn() { let uuid_orig = new(); - let orig_str = uuid_orig.to_urn().to_string(); + let orig_str = uuid_orig.urn().to_string(); let uuid_out = Uuid::parse_str(&orig_str).unwrap(); assert_eq!(uuid_orig, uuid_out); } @@ -354,7 +355,7 @@ mod tests { #[test] fn test_roundtrip_braced() { let uuid_orig = new(); - let orig_str = uuid_orig.to_braced().to_string(); + let orig_str = uuid_orig.braced().to_string(); let uuid_out = Uuid::parse_str(&orig_str).unwrap(); assert_eq!(uuid_orig, uuid_out); } diff --git a/src/v1.rs b/src/v1.rs index c54a7b8e..0219adf7 100644 --- a/src/v1.rs +++ b/src/v1.rs @@ -175,7 +175,7 @@ impl Uuid { /// let uuid = Uuid::new_v1(ts, &[1, 2, 3, 4, 5, 6]); /// /// assert_eq!( - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// "f3b4958c-52a1-11e7-802a-010203040506" /// ); /// ``` @@ -192,7 +192,7 @@ impl Uuid { /// let uuid = Uuid::new_v1(ts, &[1, 2, 3, 4, 5, 6]); /// /// assert_eq!( - /// uuid.to_hyphenated().to_string(), + /// uuid.hyphenated().to_string(), /// "5943ee37-0000-1000-8000-010203040506" /// ); /// ``` @@ -325,7 +325,7 @@ mod tests { assert_eq!(uuid.get_version(), Some(Version::Mac)); assert_eq!(uuid.get_variant(), Variant::RFC4122); assert_eq!( - uuid.to_hyphenated().to_string(), + uuid.hyphenated().to_string(), "20616934-4ba2-11e7-8000-010203040506" ); diff --git a/src/v3.rs b/src/v3.rs index d9fcf1d8..2fa06114 100644 --- a/src/v3.rs +++ b/src/v3.rs @@ -47,7 +47,7 @@ impl Uuid { .set_variant(Variant::RFC4122) .set_version(Version::Md5); - builder.build() + builder.into_uuid() } } @@ -155,10 +155,10 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn test_to_hyphenated_string() { + fn test_hyphenated_string() { for &(ref ns, ref name, ref expected) in FIXTURE { let uuid = Uuid::new_v3(*ns, name.as_bytes()); - assert_eq!(uuid.to_hyphenated().to_string(), *expected); + assert_eq!(uuid.hyphenated().to_string(), *expected); } } } diff --git a/src/v4.rs b/src/v4.rs index f122c13a..093bc0e7 100644 --- a/src/v4.rs +++ b/src/v4.rs @@ -1,4 +1,4 @@ -use crate::{Uuid, Variant, Version}; +use crate::Uuid; impl Uuid { /// Creates a random UUID. @@ -27,10 +27,7 @@ impl Uuid { /// [`getrandom`]: https://crates.io/crates/getrandom /// [from_random_bytes]: struct.Builder.html#method.from_random_bytes pub fn new_v4() -> Uuid { - crate::Builder::from_bytes(crate::rng::bytes()) - .set_variant(Variant::RFC4122) - .set_version(Version::Random) - .build() + crate::Builder::from_random_bytes(crate::rng::bytes()).into_uuid() } } @@ -38,6 +35,8 @@ impl Uuid { mod tests { use super::*; + use crate::{Variant, Version}; + #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; diff --git a/src/v5.rs b/src/v5.rs index 116eba9c..7e4ab326 100644 --- a/src/v5.rs +++ b/src/v5.rs @@ -46,7 +46,7 @@ impl Uuid { .set_variant(Variant::RFC4122) .set_version(Version::Sha1); - builder.build() + builder.into_uuid() } } @@ -158,7 +158,7 @@ mod tests { for &(ref ns, ref name, ref expected) in FIXTURE { let uuid = Uuid::new_v5(*ns, name.as_bytes()); - assert_eq!(uuid.to_hyphenated().to_string(), *expected) + assert_eq!(uuid.hyphenated().to_string(), *expected) } } From 80d4a386e92ab4d3856f8537ed06869f0c5fd0aa Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 3 Nov 2021 19:38:03 +1000 Subject: [PATCH 2/2] fix broken and missing doc links --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3566bdab..aa7c5bc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -290,9 +290,11 @@ pub enum Variant { /// A UUID can be formatted in one of a few ways: /// /// * [`simple`](#method.to_simple): `a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8`. -/// * [`hyphenated`](#method.hyphenated()): +/// * [`hyphenated`](#method.hyphenated): /// `a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8`. /// * [`urn`](#method.to_urn): `urn:uuid:A1A2A3A4-B1B2-C1C2-D1D2-D3D4D5D6D7D8`. +/// * [`braced`](#method.braced): +/// `{a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8}`. /// /// The default representation when formatting a UUID with `Display` is /// hyphenated: