Trait frame_support::dispatch::PartialEq
1.0.0 · source · [−]pub trait PartialEq<Rhs = Self> where
Rhs: ?Sized, {
fn eq(&self, other: &Rhs) -> bool;
fn ne(&self, other: &Rhs) -> bool { ... }
}
Expand description
Trait for equality comparisons which are partial equivalence relations.
x.eq(y)
can also be written x == y
, and x.ne(y)
can be written x != y
.
We use the easier-to-read infix notation in the remainder of this documentation.
This trait allows for partial equality, for types that do not have a full
equivalence relation. For example, in floating point numbers NaN != NaN
,
so floating point types implement PartialEq
but not Eq
.
Implementations must ensure that eq
and ne
are consistent with each other:
a != b
if and only if!(a == b)
(ensured by the default implementation).
If PartialOrd
or Ord
are also implemented for Self
and Rhs
, their methods must also
be consistent with PartialEq
(see the documentation of those traits for the exact
requirements). It’s easy to accidentally make them disagree by deriving some of the traits and
manually implementing others.
The equality relation ==
must satisfy the following conditions
(for all a
, b
, c
of type A
, B
, C
):
-
Symmetric: if
A: PartialEq<B>
andB: PartialEq<A>
, thena == b
impliesb == a
; and -
Transitive: if
A: PartialEq<B>
andB: PartialEq<C>
andA: PartialEq<C>
, thena == b
andb == c
impliesa == c
.
Note that the B: PartialEq<A>
(symmetric) and A: PartialEq<C>
(transitive) impls are not forced to exist, but these requirements apply
whenever they do exist.
Derivable
This trait can be used with #[derive]
. When derive
d on structs, two
instances are equal if all fields are equal, and not equal if any fields
are not equal. When derive
d on enums, each variant is equal to itself
and not equal to the other variants.
How can I implement PartialEq
?
An example implementation for a domain in which two books are considered the same book if their ISBN matches, even if the formats differ:
enum BookFormat {
Paperback,
Hardback,
Ebook,
}
struct Book {
isbn: i32,
format: BookFormat,
}
impl PartialEq for Book {
fn eq(&self, other: &Self) -> bool {
self.isbn == other.isbn
}
}
let b1 = Book { isbn: 3, format: BookFormat::Paperback };
let b2 = Book { isbn: 3, format: BookFormat::Ebook };
let b3 = Book { isbn: 10, format: BookFormat::Paperback };
assert!(b1 == b2);
assert!(b1 != b3);
How can I compare two different types?
The type you can compare with is controlled by PartialEq
’s type parameter.
For example, let’s tweak our previous code a bit:
// The derive implements <BookFormat> == <BookFormat> comparisons
#[derive(PartialEq)]
enum BookFormat {
Paperback,
Hardback,
Ebook,
}
struct Book {
isbn: i32,
format: BookFormat,
}
// Implement <Book> == <BookFormat> comparisons
impl PartialEq<BookFormat> for Book {
fn eq(&self, other: &BookFormat) -> bool {
self.format == *other
}
}
// Implement <BookFormat> == <Book> comparisons
impl PartialEq<Book> for BookFormat {
fn eq(&self, other: &Book) -> bool {
*self == other.format
}
}
let b1 = Book { isbn: 3, format: BookFormat::Paperback };
assert!(b1 == BookFormat::Paperback);
assert!(BookFormat::Ebook != b1);
By changing impl PartialEq for Book
to impl PartialEq<BookFormat> for Book
,
we allow BookFormat
s to be compared with Book
s.
A comparison like the one above, which ignores some fields of the struct,
can be dangerous. It can easily lead to an unintended violation of the
requirements for a partial equivalence relation. For example, if we kept
the above implementation of PartialEq<Book>
for BookFormat
and added an
implementation of PartialEq<Book>
for Book
(either via a #[derive]
or
via the manual implementation from the first example) then the result would
violate transitivity:
#[derive(PartialEq)]
enum BookFormat {
Paperback,
Hardback,
Ebook,
}
#[derive(PartialEq)]
struct Book {
isbn: i32,
format: BookFormat,
}
impl PartialEq<BookFormat> for Book {
fn eq(&self, other: &BookFormat) -> bool {
self.format == *other
}
}
impl PartialEq<Book> for BookFormat {
fn eq(&self, other: &Book) -> bool {
*self == other.format
}
}
fn main() {
let b1 = Book { isbn: 1, format: BookFormat::Paperback };
let b2 = Book { isbn: 2, format: BookFormat::Paperback };
assert!(b1 == BookFormat::Paperback);
assert!(BookFormat::Paperback == b2);
// The following should hold by transitivity but doesn't.
assert!(b1 == b2); // <-- PANICS
}
Examples
let x: u32 = 0;
let y: u32 = 1;
assert_eq!(x == y, false);
assert_eq!(x.eq(&y), false);
Required methods
Provided methods
Implementations on Foreign Types
impl<T, const LANES: usize> PartialEq<Simd<T, LANES>> for Simd<T, LANES> where
T: SimdElement + PartialEq<T>,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> PartialEq<Simd<T, LANES>> for Simd<T, LANES> where
T: SimdElement + PartialEq<T>,
LaneCount<LANES>: SupportedLaneCount,
1.20.0 · sourceimpl<T> PartialEq<ManuallyDrop<T>> for ManuallyDrop<T> where
T: PartialEq<T> + ?Sized,
impl<T> PartialEq<ManuallyDrop<T>> for ManuallyDrop<T> where
T: PartialEq<T> + ?Sized,
1.55.0 · sourceimpl<B, C> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C> where
B: PartialEq<B>,
C: PartialEq<C>,
impl<B, C> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C> where
B: PartialEq<B>,
C: PartialEq<C>,
1.26.0 · sourceimpl<Idx> PartialEq<RangeInclusive<Idx>> for RangeInclusive<Idx> where
Idx: PartialEq<Idx>,
impl<Idx> PartialEq<RangeInclusive<Idx>> for RangeInclusive<Idx> where
Idx: PartialEq<Idx>,
impl<A, B, C, D, E, F, G, H, I, J, K> PartialEq<(A, B, C, D, E, F, G, H, I, J, K)> for (A, B, C, D, E, F, G, H, I, J, K) where
A: PartialEq<A>,
B: PartialEq<B>,
C: PartialEq<C>,
D: PartialEq<D>,
E: PartialEq<E>,
F: PartialEq<F>,
G: PartialEq<G>,
H: PartialEq<H>,
I: PartialEq<I>,
J: PartialEq<J>,
K: PartialEq<K> + ?Sized,
impl<A, B, C, D, E, F, G, H, I, J, K> PartialEq<(A, B, C, D, E, F, G, H, I, J, K)> for (A, B, C, D, E, F, G, H, I, J, K) where
A: PartialEq<A>,
B: PartialEq<B>,
C: PartialEq<C>,
D: PartialEq<D>,
E: PartialEq<E>,
F: PartialEq<F>,
G: PartialEq<G>,
H: PartialEq<H>,
I: PartialEq<I>,
J: PartialEq<J>,
K: PartialEq<K> + ?Sized,
impl<A, B, C, D, E, F, G, H, I, J, K, L> PartialEq<(A, B, C, D, E, F, G, H, I, J, K, L)> for (A, B, C, D, E, F, G, H, I, J, K, L) where
A: PartialEq<A>,
B: PartialEq<B>,
C: PartialEq<C>,
D: PartialEq<D>,
E: PartialEq<E>,
F: PartialEq<F>,
G: PartialEq<G>,
H: PartialEq<H>,
I: PartialEq<I>,
J: PartialEq<J>,
K: PartialEq<K>,
L: PartialEq<L> + ?Sized,
impl<A, B, C, D, E, F, G, H, I, J, K, L> PartialEq<(A, B, C, D, E, F, G, H, I, J, K, L)> for (A, B, C, D, E, F, G, H, I, J, K, L) where
A: PartialEq<A>,
B: PartialEq<B>,
C: PartialEq<C>,
D: PartialEq<D>,
E: PartialEq<E>,
F: PartialEq<F>,
G: PartialEq<G>,
H: PartialEq<H>,
I: PartialEq<I>,
J: PartialEq<J>,
K: PartialEq<K>,
L: PartialEq<L> + ?Sized,
impl<Y, R> PartialEq<GeneratorState<Y, R>> for GeneratorState<Y, R> where
Y: PartialEq<Y>,
R: PartialEq<R>,
impl<Y, R> PartialEq<GeneratorState<Y, R>> for GeneratorState<Y, R> where
Y: PartialEq<Y>,
R: PartialEq<R>,
impl<T, const LANES: usize> PartialEq<Mask<T, LANES>> for Mask<T, LANES> where
T: MaskElement + PartialEq<T>,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> PartialEq<Mask<T, LANES>> for Mask<T, LANES> where
T: MaskElement + PartialEq<T>,
LaneCount<LANES>: SupportedLaneCount,
1.26.0 · sourceimpl<Idx> PartialEq<RangeToInclusive<Idx>> for RangeToInclusive<Idx> where
Idx: PartialEq<Idx>,
impl<Idx> PartialEq<RangeToInclusive<Idx>> for RangeToInclusive<Idx> where
Idx: PartialEq<Idx>,
Equality for two Rc
s.
Two Rc
s are equal if their inner values are equal, even if they are
stored in different allocation.
If T
also implements Eq
(implying reflexivity of equality),
two Rc
s that point to the same allocation are
always equal.
Examples
use std::rc::Rc;
let five = Rc::new(5);
assert!(five == Rc::new(5));
Inequality for two Rc
s.
Two Rc
s are unequal if their inner values are unequal.
If T
also implements Eq
(implying reflexivity of equality),
two Rc
s that point to the same allocation are
never unequal.
Examples
use std::rc::Rc;
let five = Rc::new(5);
assert!(five != Rc::new(6));
Equality for two Arc
s.
Two Arc
s are equal if their inner values are equal, even if they are
stored in different allocation.
If T
also implements Eq
(implying reflexivity of equality),
two Arc
s that point to the same allocation are always equal.
Examples
use std::sync::Arc;
let five = Arc::new(5);
assert!(five == Arc::new(5));
Inequality for two Arc
s.
Two Arc
s are unequal if their inner values are unequal.
If T
also implements Eq
(implying reflexivity of equality),
two Arc
s that point to the same value are never unequal.
Examples
use std::sync::Arc;
let five = Arc::new(5);
assert!(five != Arc::new(6));
impl<M, F> PartialEq<WithFilter<M, F>> for WithFilter<M, F> where
M: PartialEq<M>,
F: PartialEq<F>,
impl<M, F> PartialEq<WithFilter<M, F>> for WithFilter<M, F> where
M: PartialEq<M>,
F: PartialEq<F>,
impl<A, B> PartialEq<EitherWriter<A, B>> for EitherWriter<A, B> where
A: PartialEq<A>,
B: PartialEq<B>,
impl<A, B> PartialEq<EitherWriter<A, B>> for EitherWriter<A, B> where
A: PartialEq<A>,
B: PartialEq<B>,
impl<'a, S> PartialEq<ANSIGenericString<'a, S>> for ANSIGenericString<'a, S> where
S: 'a + PartialEq<S> + ToOwned + ?Sized,
<S as ToOwned>::Owned: Debug,
impl<'a, S> PartialEq<ANSIGenericString<'a, S>> for ANSIGenericString<'a, S> where
S: 'a + PartialEq<S> + ToOwned + ?Sized,
<S as ToOwned>::Owned: Debug,
impl<'a, S> PartialEq<ANSIGenericStrings<'a, S>> for ANSIGenericStrings<'a, S> where
S: 'a + PartialEq<S> + ToOwned + ?Sized,
<S as ToOwned>::Owned: Debug,
impl<'a, S> PartialEq<ANSIGenericStrings<'a, S>> for ANSIGenericStrings<'a, S> where
S: 'a + PartialEq<S> + ToOwned + ?Sized,
<S as ToOwned>::Owned: Debug,
impl<Hash> PartialEq<StorageChangeSet<Hash>> for StorageChangeSet<Hash> where
Hash: PartialEq<Hash>,
impl<Hash> PartialEq<StorageChangeSet<Hash>> for StorageChangeSet<Hash> where
Hash: PartialEq<Hash>,
impl<T, N> PartialEq<GenericArray<T, N>> for GenericArray<T, N> where
T: PartialEq<T>,
N: ArrayLength<T>,
impl<T, N> PartialEq<GenericArray<T, N>> for GenericArray<T, N> where
T: PartialEq<T>,
N: ArrayLength<T>,
We hide fields largely so that only compairing the compressed forms works.
impl<T, N> PartialEq<GenericArray<T, N>> for GenericArray<T, N> where
T: PartialEq<T>,
N: ArrayLength<T>,
impl<T, N> PartialEq<GenericArray<T, N>> for GenericArray<T, N> where
T: PartialEq<T>,
N: ArrayLength<T>,
impl PartialEq<u32x4> for u32x4
impl PartialEq<u32x4> for u32x4
impl<T, E> PartialEq<TryChunksError<T, E>> for TryChunksError<T, E> where
T: PartialEq<T>,
E: PartialEq<E>,
impl<T, E> PartialEq<TryChunksError<T, E>> for TryChunksError<T, E> where
T: PartialEq<T>,
E: PartialEq<E>,
impl<H, KF, T, M> PartialEq<MemoryDB<H, KF, T, M>> for MemoryDB<H, KF, T, M> where
H: Hasher,
KF: KeyFunction<H>,
T: Eq + MaybeDebug,
M: MemTracker<T> + PartialEq<M>,
<KF as KeyFunction<H>>::Key: Eq,
<KF as KeyFunction<H>>::Key: MaybeDebug,
impl<H, KF, T, M> PartialEq<MemoryDB<H, KF, T, M>> for MemoryDB<H, KF, T, M> where
H: Hasher,
KF: KeyFunction<H>,
T: Eq + MaybeDebug,
M: MemTracker<T> + PartialEq<M>,
<KF as KeyFunction<H>>::Key: Eq,
<KF as KeyFunction<H>>::Key: MaybeDebug,
This doesn’t test if they are in the same state, only if they contains the same data at this state