Skip to content

Commit

Permalink
refactor(ast): simplify ContentEq trait definition. (#5468)
Browse files Browse the repository at this point in the history
Addresses the concerns brought up in #5427
  • Loading branch information
rzvxa committed Sep 5, 2024
1 parent ccc8a27 commit 9f6e0ed
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 88 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_ast_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn assert_generated_derives(attrs: &[syn::Attribute]) -> TokenStream2 {
} else if ident == "GetSpanMut" {
(quote!(::oxc_span::GetSpanMut), TokenStream2::default())
} else if ident == "ContentEq" {
(quote!(::oxc_span::cmp::ContentEq), quote!(<()>))
(quote!(::oxc_span::cmp::ContentEq), TokenStream2::default())
} else if ident == "ContentHash" {
(quote!(::oxc_span::hash::ContentHash), TokenStream2::default())
} else {
Expand Down
24 changes: 0 additions & 24 deletions crates/oxc_span/src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,30 +180,6 @@ impl<'a> ContentEq for Atom<'a> {
}
}

impl<'a> ContentEq<Atom<'a>> for &str {
fn content_eq(&self, other: &Atom<'a>) -> bool {
self == other
}
}

impl<'a> ContentEq<str> for Atom<'a> {
fn content_eq(&self, other: &str) -> bool {
self == other
}
}

impl<'a> ContentEq<Atom<'a>> for Cow<'_, str> {
fn content_eq(&self, other: &Atom<'a>) -> bool {
self == other
}
}

impl<'a> ContentEq<&Atom<'a>> for Cow<'_, str> {
fn content_eq(&self, other: &&Atom<'a>) -> bool {
self == other
}
}

impl<'a> ContentHash for Atom<'a> {
fn content_hash<H: hash::Hasher>(&self, state: &mut H) {
hash::Hash::hash(self, state);
Expand Down
66 changes: 3 additions & 63 deletions crates/oxc_span/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
///
/// One should always prefer using the [PartialEq] over this since implementations of this trait
/// inherently are slower or in the best-case scenario as fast as the [PartialEq] comparison.
pub trait ContentEq<Rhs: ?Sized = Self> {
pub trait ContentEq {
/// This method tests for contents of `self` and `other` to be equal.
#[must_use]
fn content_eq(&self, other: &Rhs) -> bool;
fn content_eq(&self, other: &Self) -> bool;

/// This method tests for contents of `self` and `other` not to be equal.
/// The default implementation is almost always
/// sufficient, and should not be overridden without very good reason.
#[inline]
#[must_use]
fn content_ne(&self, other: &Rhs) -> bool {
fn content_ne(&self, other: &Self) -> bool {
!self.content_eq(other)
}
}
Expand All @@ -32,66 +32,6 @@ impl ContentEq for () {
}
}

/// Blanket implementation for references
impl<A: ?Sized, B: ?Sized> ContentEq<&B> for &A
where
A: ContentEq<B>,
{
#[inline]
fn content_eq(&self, other: &&B) -> bool {
ContentEq::content_eq(*self, *other)
}
#[inline]
fn content_ne(&self, other: &&B) -> bool {
ContentEq::content_ne(*self, *other)
}
}

/// Blanket implementation for mutable references
impl<A: ?Sized, B: ?Sized> ContentEq<&mut B> for &mut A
where
A: ContentEq<B>,
{
#[inline]
fn content_eq(&self, other: &&mut B) -> bool {
ContentEq::content_eq(*self, *other)
}
#[inline]
fn content_ne(&self, other: &&mut B) -> bool {
ContentEq::content_ne(*self, *other)
}
}

/// Blanket implementation for mixed references
impl<A: ?Sized, B: ?Sized> ContentEq<&mut B> for &A
where
A: ContentEq<B>,
{
#[inline]
fn content_eq(&self, other: &&mut B) -> bool {
ContentEq::content_eq(*self, *other)
}
#[inline]
fn content_ne(&self, other: &&mut B) -> bool {
ContentEq::content_ne(*self, *other)
}
}

/// Blanket implementation for mixed references
impl<A: ?Sized, B: ?Sized> ContentEq<&B> for &mut A
where
A: ContentEq<B>,
{
#[inline]
fn content_eq(&self, other: &&B) -> bool {
ContentEq::content_eq(*self, *other)
}
#[inline]
fn content_ne(&self, other: &&B) -> bool {
ContentEq::content_ne(*self, *other)
}
}

/// Blanket implementation for [Option] types
impl<T: ContentEq> ContentEq for Option<T> {
#[inline]
Expand Down

0 comments on commit 9f6e0ed

Please sign in to comment.