Currently:
Vec<T> implements PartialEq for &'_ [B; N], &'_ [B], &'_ mut [B], and [B; N] (see https://doc.rust-lang.org/std/vec/struct.Vec.html#impl-PartialEq%3C%26%27_%20%5BB%3B%20N%5D%3E)[T; N] implements PartialEq for &'b [B], &'b mut [B], [B; N], and [B] (see https://doc.rust-lang.org/std/primitive.array.html#impl-PartialEq%3C%26%27b%20%5BB%5D%3E)&'b [B], &'b mut [B], and [B] implement PartialEq<[A; N]> (see https://doc.rust-lang.org/std/primitive.slice.html#impl-PartialEq%3C%5BA%3B%20N%5D%3E)[A] implements PartialEq<[B]> (see https://doc.rust-lang.org/std/primitive.slice.html#impl-PartialEq%3C%5BA%3B%20N%5D%3E)But neither [T; N], &'_ [T], &'_ mut [T], nor [T] implement PartialEq<Vec<T>>. This means that equality tests have to be pointlessly flipped. Is there any reason for this behavior? Would it be possible to fix this?
Is there any reason for this behavior?
Yes, the orphan rules. One is defined in libcore, the other is not.
Would it be possible to fix this?
Not really.
The RFCs repo isn't really intended for questions. I encourage you to instead search rust-lang/rust repo or the general internet, then post questions on the user's forum or Discord.
You can also check out the Rust source code and make the change yourself to see the compiler errors.
My change seems to build & pass the tests...:
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs
index b4a9da84787..78dab89be79 100644
--- a/src/liballoc/vec.rs
+++ b/src/liballoc/vec.rs
@@ -2340,6 +2340,8 @@ macro_rules! __impl_slice_eq1 {
__impl_slice_eq1! { [] Vec<A>, Vec<B>, }
__impl_slice_eq1! { [] Vec<A>, &[B], }
__impl_slice_eq1! { [] Vec<A>, &mut [B], }
+__impl_slice_eq1! { [] &[A], Vec<B>, }
+__impl_slice_eq1! { [] &mut [A], Vec<B>, }
__impl_slice_eq1! { [] Cow<'_, [A]>, &[B], A: Clone }
__impl_slice_eq1! { [] Cow<'_, [A]>, &mut [B], A: Clone }
__impl_slice_eq1! { [] Cow<'_, [A]>, Vec<B>, A: Clone }
Hmm. You know, there were changes in Rust 1.41 that changed some code in this area. See RFC 2451 for more details. Perhaps it _is_ possible now? If your code compiles, passes tests, and you can write the equality tests you wanted to, it's worth opening a PR to rust-lang/rust.
Sounds good!
Would it also be possible to impl PartialEq<[B]> for Vec<A> and PartialEq<Vec<B> for [A]>?
@mbrubeck when I tried similar for Option, it caused a massive amount of fallout due to type inference failures. For example, assert_eq!(foo(), None) is no longer unique and requires a turbofish. I assume something similar would happen here.
I can't see any reason that an impl for Vec<A> and [B] would cause inference problems while the existing impl for Vec<A> and &[B] does not.
Also, there should be working assert_eq/ne! tests for each possibility, so that shouldn't be an issue.
Would it also be possible to
impl PartialEq<[B]> for Vec<A>andPartialEq<Vec<B> for [A]>?
Adding these impls to liballoc/vec.rs appears to work fine, without causing any regressions in the compiler code or tests.
This can be closed now, right?
Most helpful comment
My change seems to build & pass the tests...: