Skip to content

Commit

Permalink
support equality between NonNilUuid and Uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jan 14, 2025
1 parent 4ffd872 commit f570b57
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ pub enum Variant {
/// The `Uuid` type is always guaranteed to be have the same ABI as [`Bytes`].
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
// NOTE: Also check `NonNilUuid` when ading new derives here
#[cfg_attr(
all(uuid_unstable, feature = "zerocopy"),
derive(IntoBytes, FromBytes, KnownLayout, Immutable, Unaligned)
Expand Down
16 changes: 14 additions & 2 deletions src/non_nil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ impl fmt::Display for NonNilUuid {
}
}

impl PartialEq<Uuid> for NonNilUuid {
fn eq(&self, other: &Uuid) -> bool {
self.get() == *other
}
}

impl PartialEq<NonNilUuid> for Uuid {
fn eq(&self, other: &NonNilUuid) -> bool {
*self == other.get()
}
}

impl NonNilUuid {
/// Creates a non-nil UUID if the value is non-nil.
pub const fn new(uuid: Uuid) -> Option<Self> {
Expand Down Expand Up @@ -125,8 +137,8 @@ mod tests {
let uuid = Uuid::from_u128(0x0123456789abcdef0123456789abcdef);

assert_eq!(Uuid::from(NonNilUuid::try_from(uuid).unwrap()), uuid);
assert_eq!(NonNilUuid::new(uuid).unwrap().get(), uuid);
assert_eq!(unsafe { NonNilUuid::new_unchecked(uuid) }.get(), uuid);
assert_eq!(NonNilUuid::new(uuid).unwrap(), uuid);
assert_eq!(unsafe { NonNilUuid::new_unchecked(uuid) }, uuid);

assert!(NonNilUuid::try_from(Uuid::nil()).is_err());
assert!(NonNilUuid::new(Uuid::nil()).is_none());
Expand Down

0 comments on commit f570b57

Please sign in to comment.