More surprises with the `==` operator

The analysis above is verbose. Here is a simple workflow:

let array: [u8; 1] = [0u8; 1];
let array_ref: &[u8; 1] = &[0u8; 1];
let slice: &[u8] = &vec![0u8];
assert!(slice == array_ref); // &[u8]: PartialEq<&[u8; 1]> => [u8]: PartialEq<[u8; 1]>
  1. Write down the left-hand-side and right-hand-side types: &[u8] == &[u8; 1]

  2. Turn it into the trait bound form[1]: &[u8]: PartialEq<&[u8; 1]>
    or impl form: impl PartialEq<&[u8; 1]> for &[u8]

  3. search for &[

    impl<A, B, const N: usize> PartialEq<[A; N]> for &[B]
    impl<T, U, A> PartialEq<Vec<U, A>> for &[T]
    

    neither are wanted

  4. search for common generic impls and deduce

    impl<A, B> PartialEq<&B> for &A
    where
        A: PartialEq<B> + ?Sized,
        B: ?Sized,
    

    thus we can remove & on both sides: [u8]: PartialEq<[u8; 1]>
    then we'll find impl<A, B, const N: usize> PartialEq<[A; N]> for [B]


  1. Make sure you get the the correct a == b desugaring:
    <TypeOfA as PartialEq<TypeOfB>>::eq(&a, &b) ↩︎

2 Likes