Skip to content

Commit

Permalink
Merge #511
Browse files Browse the repository at this point in the history
511: Allow access of inner Uuid for adapters r=KodrAus a=hecsalazarf

**I'm submitting a** feature.


# Description
Trait implementations on the adapters:
* `From<Adapter> for Uuid`
* `AsRef<Uuid> for Adapter`
* `Borrow<Uuid> for Adapter`

An `Adapter::into_inner` method that takes self and returns a `Uuid`.

# Motivation
Open issue #482

# Tests
All tests pass.

# Related Issue(s)
Closes #482

Co-authored-by: hecsalazarf <36686213+hecsalazarf@users.noreply.github.com>
Co-authored-by: Hector Salazar <hecsalazarf@outlook.com>
  • Loading branch information
3 people authored Jan 16, 2021
2 parents c4238b0 + cf74a80 commit 54d7bec
Showing 1 changed file with 126 additions and 1 deletion.
127 changes: 126 additions & 1 deletion src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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<Uuid> for $T {
#[inline]
fn as_ref(&self) -> &Uuid {
&self.0
}
}

impl Borrow<Uuid> for $T {
#[inline]
fn borrow(&self) -> &Uuid {
&self.0
}
}
};
($T:ident<$a:lifetime>) => {
impl<$a> From<&$a Uuid> for $T<$a> {
Expand All @@ -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<Uuid> for $T<$a> {
#[inline]
fn as_ref(&self) -> &Uuid {
self.0
}
}

impl<$a> Borrow<Uuid> for $T<$a> {
#[inline]
fn borrow(&self) -> &Uuid {
self.0
}
}
};
}

Expand Down Expand Up @@ -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());
}
}

0 comments on commit 54d7bec

Please sign in to comment.