Trait ContentEq

Source
pub trait ContentEq {
    // Required method
    fn content_eq(&self, other: &Self) -> bool;

    // Provided method
    fn content_ne(&self, other: &Self) -> bool { ... }
}
Expand description

This trait works similarly to PartialEq but it gives the liberty of checking the equality of the content loosely.

This would mean the implementor can skip some parts of the content while doing equality checks. As an example, In AST types we ignore fields such as crate::Span.

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.

Required Methods§

Source

fn content_eq(&self, other: &Self) -> bool

This method tests for contents of self and other to be equal.

Provided Methods§

Source

fn content_ne(&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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ContentEq for &str

Source§

fn content_eq(&self, other: &&str) -> bool

Source§

fn content_ne(&self, other: &&str) -> bool

Source§

impl ContentEq for bool

Source§

fn content_eq(&self, other: &bool) -> bool

Source§

fn content_ne(&self, other: &bool) -> bool

Source§

impl ContentEq for char

Source§

fn content_eq(&self, other: &char) -> bool

Source§

fn content_ne(&self, other: &char) -> bool

Source§

impl ContentEq for f64

Compare f64 as bits instead of using ==.

Result is the same as partial_eq (==), with the following exceptions:

  • +0 and -0 are not content_eq (they are partial_eq).
  • f64::NAN and f64::NAN are content_eq (they are not partial_eq).

https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=5f9ec4b26128363a660e27582d1de7cd

§NaN

Comparison of NaN is complicated. From Rust’s docs for f64:

Note that IEEE 754 doesn’t define just a single NaN value; a plethora of bit patterns are considered to be NaN.

https://doc.rust-lang.org/std/primitive.f64.html#associatedconstant.NAN

If either value is NaN, f64::content_eq only returns true if both are the same NaN, with the same bit pattern. This means, for example:

f64::NAN.content_eq(f64::NAN) == true
f64::NAN.content_eq(-f64::NAN) == false
f64::NAN.content_eq(--f64::NAN) == true

Any other NaNs which are created through an arithmetic operation, rather than explicitly with f64::NAN, are not guaranteed to equal f64::NAN.

// This results in `false` on at least some flavors of `x84_64`,
// but that's not specified - could also result in `true`!
(-1f64).sqrt().content_eq(f64::NAN) == false
Source§

fn content_eq(&self, other: &Self) -> bool

Source§

impl ContentEq for i8

Source§

fn content_eq(&self, other: &i8) -> bool

Source§

fn content_ne(&self, other: &i8) -> bool

Source§

impl ContentEq for i16

Source§

fn content_eq(&self, other: &i16) -> bool

Source§

fn content_ne(&self, other: &i16) -> bool

Source§

impl ContentEq for i32

Source§

fn content_eq(&self, other: &i32) -> bool

Source§

fn content_ne(&self, other: &i32) -> bool

Source§

impl ContentEq for i64

Source§

fn content_eq(&self, other: &i64) -> bool

Source§

fn content_ne(&self, other: &i64) -> bool

Source§

impl ContentEq for i128

Source§

fn content_eq(&self, other: &i128) -> bool

Source§

fn content_ne(&self, other: &i128) -> bool

Source§

impl ContentEq for isize

Source§

fn content_eq(&self, other: &isize) -> bool

Source§

fn content_ne(&self, other: &isize) -> bool

Source§

impl ContentEq for u8

Source§

fn content_eq(&self, other: &u8) -> bool

Source§

fn content_ne(&self, other: &u8) -> bool

Source§

impl ContentEq for u16

Source§

fn content_eq(&self, other: &u16) -> bool

Source§

fn content_ne(&self, other: &u16) -> bool

Source§

impl ContentEq for u32

Source§

fn content_eq(&self, other: &u32) -> bool

Source§

fn content_ne(&self, other: &u32) -> bool

Source§

impl ContentEq for u64

Source§

fn content_eq(&self, other: &u64) -> bool

Source§

fn content_ne(&self, other: &u64) -> bool

Source§

impl ContentEq for u128

Source§

fn content_eq(&self, other: &u128) -> bool

Source§

fn content_ne(&self, other: &u128) -> bool

Source§

impl ContentEq for ()

Source§

fn content_eq(&self, _other: &()) -> bool

Source§

fn content_ne(&self, _other: &()) -> bool

Source§

impl ContentEq for usize

Source§

fn content_eq(&self, other: &usize) -> bool

Source§

fn content_ne(&self, other: &usize) -> bool

Source§

impl<T: ContentEq> ContentEq for Option<T>

Blanket implementation for Option types

Source§

fn content_eq(&self, other: &Self) -> bool

Source§

impl<T: ContentEq> ContentEq for Box<'_, T>

Blanket implementation for oxc_allocator::Box types

Source§

fn content_eq(&self, other: &Self) -> bool

Source§

impl<T: ContentEq> ContentEq for Vec<'_, T>

Blanket implementation for oxc_allocator::Vec types

§Warning

This implementation is slow compared to PartialEq for native types which are Copy (e.g. u32). Prefer comparing the 2 vectors using == if they contain such native types (e.g. Vec<u32>). https://godbolt.org/z/54on5sMWc

Source§

fn content_eq(&self, other: &Self) -> bool

Implementors§