From 469b4d1b706599f6d46d8234e4294a109bce19f7 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Mon, 6 Jan 2025 18:32:21 +0100 Subject: [PATCH] add tests, impl ord and eq --- lib/cowstr/src/lib.rs | 85 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/lib/cowstr/src/lib.rs b/lib/cowstr/src/lib.rs index 5c9b8a76..9d3d2875 100644 --- a/lib/cowstr/src/lib.rs +++ b/lib/cowstr/src/lib.rs @@ -6,7 +6,7 @@ const _: () = { assert!(std::mem::size_of::>() == std::mem::size_of::()); }; -#[derive(Clone, Debug, Deserialize, Serialize, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Clone, Debug, Deserialize, Serialize, Hash)] #[serde(untagged)] pub enum CowStr<'a> { Borrowed(&'a str), @@ -26,6 +26,12 @@ impl<'a> CowStr<'a> { Self::Owned(str.into()) } + #[inline] + #[must_use] + pub fn is_owned(&self) -> bool { + matches!(self, Self::Owned(..)) + } + #[inline] #[must_use] pub fn into_owned(self) -> CowStr<'static> { @@ -68,3 +74,80 @@ impl From for CowStr<'static> { Self::Owned(value) } } + +impl PartialEq for CowStr<'_> { + #[inline] + fn eq(&self, other: &Self) -> bool { + let lhs: &str = self.borrow(); + let rhs: &str = other.borrow(); + + lhs.eq(rhs) + } +} + +impl Eq for CowStr<'_> {} + +impl PartialOrd for CowStr<'_> { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for CowStr<'_> { + #[inline] + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + let lhs: &str = self.borrow(); + let rhs: &str = other.borrow(); + + lhs.cmp(rhs) + } +} + +#[cfg(test)] +mod test { + use crate::CowStr; + use compact_str::CompactString; + use std::borrow::Borrow; + + const TEST_STR: &str = "hello world"; + + #[test] + fn borrowed() { + let borrowed_1 = CowStr::borrowed(TEST_STR); + let borrowed_2: CowStr<'_> = TEST_STR.into(); + + assert!(!borrowed_1.is_owned()); + assert!(!borrowed_2.is_owned()); + + assert_eq!(borrowed_1, borrowed_2); + assert_eq!(Borrow::::borrow(&borrowed_1), TEST_STR); + } + + #[test] + fn owned() { + let owned_1 = CowStr::owned(TEST_STR); + let owned_2: CowStr<'_> = CompactString::from(TEST_STR).into(); + + assert!(owned_1.is_owned()); + assert!(owned_2.is_owned()); + + assert_eq!(owned_1, owned_2); + assert_eq!(Borrow::::borrow(&owned_1), TEST_STR); + } + + #[test] + fn into_owned() { + let borrowed = CowStr::borrowed(TEST_STR); + let owned = { + let cloned = borrowed.clone(); + assert!(!cloned.is_owned()); + cloned.into_owned() + }; + + assert!(!borrowed.is_owned()); + assert!(owned.is_owned()); + + assert_eq!(borrowed, owned); + } +}