From ea33f9470bc973d1e9c0394e01be0df84272234a Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:39:10 +0000 Subject: [PATCH] fix: impl PartialEq for CompactStr (#4352) I need this for another PR that I'm working on, but I think adding this just makes sense. --- crates/oxc_span/src/atom.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/crates/oxc_span/src/atom.rs b/crates/oxc_span/src/atom.rs index 9d6ba3f15e628..40e5f42ec5e6a 100644 --- a/crates/oxc_span/src/atom.rs +++ b/crates/oxc_span/src/atom.rs @@ -297,6 +297,18 @@ impl PartialEq for &str { } } +impl PartialEq for str { + fn eq(&self, other: &CompactStr) -> bool { + self == other.as_str() + } +} + +impl PartialEq for CompactStr { + fn eq(&self, other: &str) -> bool { + self.as_str() == other + } +} + impl Index for CompactStr { type Output = str; @@ -332,3 +344,17 @@ impl Serialize for CompactStr { serializer.serialize_str(self.as_str()) } } + +#[cfg(test)] +mod test { + use super::CompactStr; + + #[test] + fn test_compactstr_eq() { + let foo = CompactStr::new("foo"); + assert_eq!(foo, "foo"); + assert_eq!(&foo, "foo"); + assert_eq!("foo", foo); + assert_eq!("foo", &foo); + } +}