From c4ea69e9ac386a20c25cf1dd806a4dd22e6a610a Mon Sep 17 00:00:00 2001 From: Hector Salazar Date: Fri, 15 Jan 2021 20:58:45 -0600 Subject: [PATCH] Allow access of inner for adapters #482 --- src/adapter/mod.rs | 127 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 1 deletion(-) diff --git a/src/adapter/mod.rs b/src/adapter/mod.rs index 3ab06e978..c36ef026a 100644 --- a/src/adapter/mod.rs +++ b/src/adapter/mod.rs @@ -12,7 +12,7 @@ //! Adapters for various formats for UUIDs use crate::prelude::*; -use crate::std::{fmt, str}; +use crate::std::{borrow::Borrow, fmt, str}; #[cfg(feature = "serde")] pub mod compact; @@ -291,6 +291,20 @@ impl Hyphenated { pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { encode(buffer, 0, &self.0, true, true) } + + /// Consumes the [`Hyphenated`], returning the underlying [`Uuid`]. + /// + /// # Examples + /// + /// ```rust + /// use uuid::Uuid; + /// + /// let hyphenated = Uuid::nil().to_hyphenated(); + /// assert_eq!(hyphenated.into_inner(), Uuid::nil()); + /// ``` + pub const fn into_inner(self) -> Uuid { + self.0 + } } impl<'a> HyphenatedRef<'a> { @@ -526,6 +540,20 @@ impl Simple { pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str { encode(buffer, 0, &self.0, false, true) } + + /// Consumes the [`Simple`], returning the underlying [`Uuid`]. + /// + /// # Examples + /// + /// ```rust + /// use uuid::Uuid; + /// + /// let simple = Uuid::nil().to_simple(); + /// assert_eq!(simple.into_inner(), Uuid::nil()); + /// ``` + pub const fn into_inner(self) -> Uuid { + self.0 + } } impl<'a> SimpleRef<'a> { @@ -761,6 +789,20 @@ impl Urn { buffer[..9].copy_from_slice(b"urn:uuid:"); encode(buffer, 9, &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> { @@ -920,6 +962,27 @@ macro_rules! impl_adapter_from { $T::from_uuid(f) } } + + impl From<$T> for Uuid { + #[inline] + fn from(f: $T) -> Self { + f.into_inner() + } + } + + impl AsRef for $T { + #[inline] + fn as_ref(&self) -> &Uuid { + &self.0 + } + } + + impl Borrow for $T { + #[inline] + fn borrow(&self) -> &Uuid { + &self.0 + } + } }; ($T:ident<$a:lifetime>) => { impl<$a> From<&$a Uuid> for $T<$a> { @@ -928,6 +991,27 @@ macro_rules! impl_adapter_from { $T::from_uuid_ref(f) } } + + impl<$a> From<$T<$a>> for &$a Uuid { + #[inline] + fn from(f: $T<$a>) -> &$a Uuid { + f.0 + } + } + + impl<$a> AsRef for $T<$a> { + #[inline] + fn as_ref(&self) -> &$a Uuid { + self.0 + } + } + + impl<$a> Borrow for $T<$a> { + #[inline] + fn borrow(&self) -> &$a Uuid { + self.0 + } + } }; } @@ -1024,4 +1108,45 @@ mod tests { fn urn_ref_too_small() { Uuid::nil().to_urn_ref().encode_lower(&mut [0; 44]); } + + #[test] + fn hyphenated_to_inner() { + let hyphenated = Uuid::nil().to_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(); + 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(); + 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()); + } }