Skip to content

Commit

Permalink
Implement getter for NonNilUuid and Update tests to reflect the cha…
Browse files Browse the repository at this point in the history
…nges
  • Loading branch information
ab22593k committed Jan 13, 2025
1 parent 853ec46 commit 6860831
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
10 changes: 6 additions & 4 deletions src/external/arbitrary_support.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{non_nil::NonNilUuid, std::convert::TryInto, Builder, Uuid};
use crate::{
non_nil::NonNilUuid,
std::convert::{TryFrom, TryInto},
Builder, Uuid,
};

use arbitrary::{Arbitrary, Unstructured};

Expand All @@ -19,9 +23,7 @@ impl Arbitrary<'_> for Uuid {
impl arbitrary::Arbitrary<'_> for NonNilUuid {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let uuid = Uuid::arbitrary(u)?;

// Generated `Uuid`s are never nil since we set version/variant bits
Ok(NonNilUuid::from(uuid))
Self::try_from(uuid).map_err(|_| arbitrary::Error::NotEnoughData)
}

fn size_hint(_: usize) -> (usize, Option<usize>) {
Expand Down
15 changes: 5 additions & 10 deletions src/external/serde_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// except according to those terms.

use crate::{
convert::TryFrom,
error::*,
fmt::{Braced, Hyphenated, Simple, Urn},
non_nil::NonNilUuid,
Expand Down Expand Up @@ -143,7 +144,8 @@ impl<'de> Deserialize<'de> for NonNilUuid {
D: serde::Deserializer<'de>,
{
let uuid = Uuid::deserialize(deserializer)?;
Ok(NonNilUuid::from(uuid))

NonNilUuid::try_from(uuid).map_err(|_| de::Error::custom("Uuid cannot be nil"))
}
}

Expand Down Expand Up @@ -754,19 +756,12 @@ mod serde_tests {
}

#[test]
fn test_serialize_non_nil_uuid() {
fn test_serde_non_nil_uuid() {
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
let uuid = Uuid::parse_str(uuid_str).unwrap();
let non_nil_uuid = NonNilUuid::from(uuid);
let non_nil_uuid = NonNilUuid::try_from(uuid).unwrap();

serde_test::assert_ser_tokens(&non_nil_uuid.readable(), &[Token::Str(uuid_str)]);
}
#[test]
fn test_deserialize_non_nil_uuid() {
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
let uuid = Uuid::parse_str(uuid_str).unwrap();
let non_nil_uuid = NonNilUuid::from(uuid);

serde_test::assert_de_tokens(&non_nil_uuid.readable(), &[Token::Str(uuid_str)]);
}
}
47 changes: 28 additions & 19 deletions src/non_nil.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A wrapper type for nil UUIDs that provides a more memory-efficient
//! `Option<NonNilUuid>` representation.
use core::convert::TryFrom;
use std::{fmt, num::NonZeroU128};

use crate::Uuid;
Expand All @@ -18,15 +19,24 @@ impl fmt::Display for NonNilUuid {
}
}

impl NonNilUuid {
/// Returns the underlying `Uuid`.
#[inline]
pub const fn get(self) -> NonZeroU128 {
self.0
}
}

impl From<NonNilUuid> for Uuid {
/// Converts a [`NonNilUuid`] back into a [`Uuid`].
///
/// # Examples
/// ```
/// use uuid::{non_nil::NonNilUuid, Uuid};
/// use std::convert::TryFrom;
///
/// let uuid = Uuid::from_u128(0x0123456789abcdef0123456789abcdef);
/// let non_nil = NonNilUuid::from(uuid);
/// let non_nil = NonNilUuid::try_from(uuid).unwrap();
/// let uuid_again = Uuid::from(non_nil);
///
/// assert_eq!(uuid, uuid_again);
Expand All @@ -36,23 +46,23 @@ impl From<NonNilUuid> for Uuid {
}
}

impl From<Uuid> for NonNilUuid {
/// Converts a [`Uuid`] into a [`NonNilUuid`].
///
/// # Panics
/// Panics if the input UUID is nil (all zeros).
impl TryFrom<Uuid> for NonNilUuid {
type Error = &'static str;

/// Attempts to convert a [`Uuid`] into a [`NonNilUuid`].
///
/// # Examples
/// ```
/// use uuid::{non_nil::NonNilUuid, Uuid};
/// use std::convert::TryFrom;
///
/// let uuid = Uuid::from_u128(0x0123456789abcdef0123456789abcdef);
/// let non_nil = NonNilUuid::from(uuid);
/// let non_nil = NonNilUuid::try_from(uuid).unwrap();
/// ```
fn from(uuid: Uuid) -> Self {
fn try_from(uuid: Uuid) -> Result<Self, Self::Error> {
NonZeroU128::new(uuid.as_u128())
.map(Self)
.expect("Attempted to convert nil Uuid to NonNilUuid")
.ok_or("Attempted to convert nil Uuid to NonNilUuid")
}
}

Expand All @@ -61,24 +71,23 @@ mod tests {
use super::*;

#[test]
fn test_nonzero_uuid_option_size() {
fn test_non_nil_with_option_size() {
assert_eq!(
std::mem::size_of::<Option<NonNilUuid>>(),
std::mem::size_of::<Uuid>()
);
}

#[test]
fn test_new_with_non_nil() {
fn test_non_nil() {
let uuid = Uuid::from_u128(0x0123456789abcdef0123456789abcdef);
let nn_uuid = NonNilUuid::from(uuid);
assert_eq!(Uuid::from(nn_uuid), uuid);
}
let nn_uuid = NonNilUuid::try_from(uuid);

#[test]
#[should_panic(expected = "Attempted to convert nil Uuid to NonNilUuid")]
fn test_new_with_nil() {
let nil_uuid = Uuid::from_u128(0x0);
let _ = NonNilUuid::from(nil_uuid);
assert!(nn_uuid.is_ok());
assert_eq!(Uuid::from(nn_uuid.unwrap()), uuid);

let nil_uuid = Uuid::nil();
let nn_uuid = NonNilUuid::try_from(nil_uuid);
assert!(nn_uuid.is_err());
}
}

0 comments on commit 6860831

Please sign in to comment.