Trait signature_core::lib::cmp::PartialEq 1.0.0[−][src]
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 != bif 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 == bimpliesb == a; and -
Transitive: if
A: PartialEq<B>andB: PartialEq<C>andA: PartialEq<C>, thena == bandb == cimpliesa == 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 derived on structs, two
instances are equal if all fields are equal, and not equal if any fields
are not equal. When derived 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 BookFormats to be compared with Books.
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
1.55.0[src]impl<B, C> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C> where
C: PartialEq<C>,
B: PartialEq<B>,
impl<B, C> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C> where
C: PartialEq<C>,
B: PartialEq<B>, 1.26.0[src]impl<Idx> PartialEq<RangeToInclusive<Idx>> for RangeToInclusive<Idx> where
Idx: PartialEq<Idx>,
impl<Idx> PartialEq<RangeToInclusive<Idx>> for RangeToInclusive<Idx> where
Idx: PartialEq<Idx>, 1.26.0[src]impl<Idx> PartialEq<RangeInclusive<Idx>> for RangeInclusive<Idx> where
Idx: PartialEq<Idx>,
impl<Idx> PartialEq<RangeInclusive<Idx>> for RangeInclusive<Idx> where
Idx: PartialEq<Idx>, impl<Y, R> PartialEq<GeneratorState<Y, R>> for GeneratorState<Y, R> where
R: PartialEq<R>,
Y: PartialEq<Y>,
impl<Y, R> PartialEq<GeneratorState<Y, R>> for GeneratorState<Y, R> where
R: PartialEq<R>,
Y: PartialEq<Y>, 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
C: PartialEq<C>,
D: PartialEq<D>,
A: PartialEq<A>,
B: PartialEq<B>,
E: PartialEq<E>,
K: PartialEq<K> + ?Sized,
F: PartialEq<F>,
I: PartialEq<I>,
H: PartialEq<H>,
G: PartialEq<G>,
J: PartialEq<J>,
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
C: PartialEq<C>,
D: PartialEq<D>,
A: PartialEq<A>,
B: PartialEq<B>,
E: PartialEq<E>,
K: PartialEq<K> + ?Sized,
F: PartialEq<F>,
I: PartialEq<I>,
H: PartialEq<H>,
G: PartialEq<G>,
J: PartialEq<J>, 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
C: PartialEq<C>,
D: PartialEq<D>,
A: PartialEq<A>,
B: PartialEq<B>,
E: PartialEq<E>,
K: PartialEq<K>,
F: PartialEq<F>,
I: PartialEq<I>,
H: PartialEq<H>,
G: PartialEq<G>,
J: PartialEq<J>,
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
C: PartialEq<C>,
D: PartialEq<D>,
A: PartialEq<A>,
B: PartialEq<B>,
E: PartialEq<E>,
K: PartialEq<K>,
F: PartialEq<F>,
I: PartialEq<I>,
H: PartialEq<H>,
G: PartialEq<G>,
J: PartialEq<J>,
L: PartialEq<L> + ?Sized, Equality for two Arcs.
Two Arcs 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 Arcs 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 Arcs.
Two Arcs are unequal if their inner values are unequal.
If T also implements Eq (implying reflexivity of equality),
two Arcs that point to the same value are never unequal.
Examples
use std::sync::Arc; let five = Arc::new(5); assert!(five != Arc::new(6));
Equality for two Rcs.
Two Rcs 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 Rcs 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 Rcs.
Two Rcs are unequal if their inner values are unequal.
If T also implements Eq (implying reflexivity of equality),
two Rcs that point to the same allocation are
never unequal.
Examples
use std::rc::Rc; let five = Rc::new(5); assert!(five != Rc::new(6));
impl PartialEq<InvalidKeyLength> for InvalidKeyLength
impl PartialEq<InvalidKeyLength> for InvalidKeyLengthimpl<'_, O1, O2, T1, T2> PartialEq<BitSlice<O2, T2>> for &'_ mut BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<'_, O1, O2, T1, T2> PartialEq<BitSlice<O2, T2>> for &'_ mut BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder, impl<M1, M2, O, T1, T2> PartialEq<BitPtrRange<M2, O, T2>> for BitPtrRange<M1, O, T1> where
O: BitOrder,
T1: BitStore,
T2: BitStore,
M1: Mutability,
M2: Mutability,
impl<M1, M2, O, T1, T2> PartialEq<BitPtrRange<M2, O, T2>> for BitPtrRange<M1, O, T1> where
O: BitOrder,
T1: BitStore,
T2: BitStore,
M1: Mutability,
M2: Mutability, impl<'_, O1, O2, T1, T2> PartialEq<&'_ mut BitSlice<O2, T2>> for BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<'_, O1, O2, T1, T2> PartialEq<&'_ mut BitSlice<O2, T2>> for BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder, impl<'_, O1, O2, T1, T2> PartialEq<BitSlice<O2, T2>> for &'_ BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<'_, O1, O2, T1, T2> PartialEq<BitSlice<O2, T2>> for &'_ BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder, impl<O1, O2, T1, T2> PartialEq<BitSlice<O2, T2>> for BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<O1, O2, T1, T2> PartialEq<BitSlice<O2, T2>> for BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder, Tests if two BitSlices are semantically — not bitwise — equal.
It is valid to compare slices of different ordering or memory types.
The equality condition requires that they have the same length and that at each index, the two slices have the same bit value.
impl<'_, '_, M1, M2, O1, O2, T1, T2> PartialEq<BitRef<'_, M2, O2, T2>> for BitRef<'_, M1, O1, T1> where
T1: BitStore,
T2: BitStore,
M1: Mutability,
M2: Mutability,
O1: BitOrder,
O2: BitOrder,
impl<'_, '_, M1, M2, O1, O2, T1, T2> PartialEq<BitRef<'_, M2, O2, T2>> for BitRef<'_, M1, O1, T1> where
T1: BitStore,
T2: BitStore,
M1: Mutability,
M2: Mutability,
O1: BitOrder,
O2: BitOrder, Test equality of proxy references by the value of their proxied bit.
To test equality by address, decay to a BitPtr with into_bitptr.
impl<'_, O1, O2, T1, T2> PartialEq<&'_ BitSlice<O2, T2>> for BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<'_, O1, O2, T1, T2> PartialEq<&'_ BitSlice<O2, T2>> for BitSlice<O1, T1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder, impl<M1, M2, O, T1, T2> PartialEq<BitPtr<M2, O, T2>> for BitPtr<M1, O, T1> where
O: BitOrder,
T1: BitStore,
T2: BitStore,
M1: Mutability,
M2: Mutability,
impl<M1, M2, O, T1, T2> PartialEq<BitPtr<M2, O, T2>> for BitPtr<M1, O, T1> where
O: BitOrder,
T1: BitStore,
T2: BitStore,
M1: Mutability,
M2: Mutability, impl<O1, O2, V, T> PartialEq<BitArray<O2, V>> for BitSlice<O1, T> where
T: BitStore,
V: BitViewSized,
O1: BitOrder,
O2: BitOrder,
impl<O1, O2, V, T> PartialEq<BitArray<O2, V>> for BitSlice<O1, T> where
T: BitStore,
V: BitViewSized,
O1: BitOrder,
O2: BitOrder, impl<M1, M2, T1, T2> PartialEq<Address<M2, T2>> for Address<M1, T1> where
M1: Mutability,
M2: Mutability,
impl<M1, M2, T1, T2> PartialEq<Address<M2, T2>> for Address<M1, T1> where
M1: Mutability,
M2: Mutability, Implementors
impl<'_, A, B, const N: usize> PartialEq<&'_ [B]> for signature_core::lib::Vec<A, N> where
A: PartialEq<B>, impl<'_, A, B, const N: usize> PartialEq<&'_ mut [B]> for signature_core::lib::Vec<A, N> where
A: PartialEq<B>, impl<'_, A, B, const N: usize, const M: usize> PartialEq<&'_ [B; M]> for signature_core::lib::Vec<A, N> where
A: PartialEq<B>, impl<A, B, const N1: usize, const N2: usize> PartialEq<Vec<B, N2>> for signature_core::lib::Vec<A, N1> where
A: PartialEq<B>, impl<A, B, const N: usize, const M: usize> PartialEq<[B; M]> for signature_core::lib::Vec<A, N> where
A: PartialEq<B>, impl<K, V, S, A> PartialEq<HashMap<K, V, S, A>> for signature_core::lib::HashMap<K, V, S, A> where
S: BuildHasher,
A: Allocator + Clone,
K: Eq + Hash,
V: PartialEq<V>, impl<T, S, A> PartialEq<HashSet<T, S, A>> for signature_core::lib::HashSet<T, S, A> where
S: BuildHasher,
T: Eq + Hash,
A: Allocator + Clone,