Skip to content

Commit

Permalink
fix: impl PartialEq<str> for CompactStr (#4352)
Browse files Browse the repository at this point in the history
I need this for another PR that I'm working on, but I think adding this just makes sense.
  • Loading branch information
DonIsaac committed Jul 19, 2024
1 parent 5d77b36 commit ea33f94
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/oxc_span/src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ impl PartialEq<CompactStr> for &str {
}
}

impl PartialEq<CompactStr> for str {
fn eq(&self, other: &CompactStr) -> bool {
self == other.as_str()
}
}

impl PartialEq<str> for CompactStr {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}

impl Index<Span> for CompactStr {
type Output = str;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit ea33f94

Please sign in to comment.