From 56eb7172a5b5141882f6768a86234842cebf9833 Mon Sep 17 00:00:00 2001 From: rickdewater Date: Mon, 24 Feb 2025 12:14:43 +0100 Subject: [PATCH 1/2] Add a manual debug implementation for NonNilUUid --- src/non_nil.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/non_nil.rs b/src/non_nil.rs index ee235a1..5507037 100644 --- a/src/non_nil.rs +++ b/src/non_nil.rs @@ -33,9 +33,15 @@ use crate::{ /// may change. It is currently only guaranteed that `NonNilUuid` and `Option` /// are the same size as `Uuid`. #[repr(transparent)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct NonNilUuid(NonZeroU128); +impl fmt::Debug for NonNilUuid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", Uuid::from(*self)) + } +} + impl fmt::Display for NonNilUuid { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", Uuid::from(*self)) @@ -139,4 +145,13 @@ mod tests { assert!(NonNilUuid::try_from(Uuid::nil()).is_err()); assert!(NonNilUuid::new(Uuid::nil()).is_none()); } + + #[test] + fn test_non_nil_formatting() { + let uuid = Uuid::from_u128(0x0123456789abcdef0123456789abcdef); + let non_nil = NonNilUuid::try_from(uuid).unwrap(); + + assert_eq!(format!("{uuid}"), format!("{non_nil}")); + assert_eq!(format!("{uuid:?}"), format!("{non_nil:?}")); + } } From 321ef2c6ad8d56df22d11137473079788c35ce16 Mon Sep 17 00:00:00 2001 From: rickdewater Date: Tue, 25 Feb 2025 12:44:07 +0100 Subject: [PATCH 2/2] Directly call Debug and Display traits instead of using the write macro --- src/non_nil.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/non_nil.rs b/src/non_nil.rs index 5507037..ad417ae 100644 --- a/src/non_nil.rs +++ b/src/non_nil.rs @@ -38,13 +38,13 @@ pub struct NonNilUuid(NonZeroU128); impl fmt::Debug for NonNilUuid { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", Uuid::from(*self)) + fmt::Debug::fmt(&Uuid::from(*self), f) } } impl fmt::Display for NonNilUuid { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", Uuid::from(*self)) + fmt::Display::fmt(&Uuid::from(*self), f) } }