Add a manual debug implementation for NonNilUUid

This commit is contained in:
rickdewater 2025-02-24 12:14:43 +01:00
parent bf5b0b84d2
commit 56eb7172a5

View File

@ -33,9 +33,15 @@ use crate::{
/// may change. It is currently only guaranteed that `NonNilUuid` and `Option<NonNilUuid>`
/// 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:?}"));
}
}