-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Functions in std should accept &[T; N]
whenever they accept &[T]
#21725
Comments
cc @nick29581, maybe something to with coercions? It seems at least (I tried to implement #18465 and met this exact problem) |
I'll have to have a look at the code in the morning to be sure of what's going on. I think the problem though is that we do not coerce when searching for impls, the OTOH, I could be wrong and we should coerce and this is a bug... |
If there's no bug in coercions and impl lookup, then I'm ready to write additional impls for the comparison traits to cover references to fixed-size arrays. |
All types that are "slices in disguise" - `[T]`, `[T; N]`, `Vec<T>` etc and sometimes references to them (see #21725) should be equality comparable to each other. Many of them are already comparable, but some are not and this PR patches the holes in the impls of `PartialEq` for slice-like entities. Ideally, the problem could be solved by an impl roughly looking like ``` impl<T, U, A, B> PartialEq<U> for T where T: BorrowSlice<A>, U: BorrowSlice<B>, A: PartialEq<B> { fn eq(&self, other: &U) -> bool { self.borrow_slice() == other.borrow_slice() } fn ne(&self, other: &U) -> bool { self.borrow_slice() != other.borrow_slice() } } ``` , but the type system would never allow such a simple and obvious solution. Therefore, the patch follows this approach, but implements it with macros and not generics. The applied changes are conservative, in particular, the smart pointers aren't touched at all. But the comparisons for `Box<[T]>`, `Rc<[T]>` and `Arc<[T]>` seem trivial to add. (Should I do it?) This PR partially address #21725 Technically this is a [breaking-change], because the impls for fixed-size arrays are more conservative now. Note: There are blanket impls of `PartialEq` in libcore/cmp.rs (see https://github.com/rust-lang/rust/blob/master/src/libcore/cmp.rs#L432 and below), they strip an *equal* number of references from both operands and then compare the remains. It would be much better to strip *all* the references before the comparison in the blanket impls to reduce the number of concrete impls, but I haven't found a way to do it without specialization/negative bounds. r? @aturon cc @gankro
I believe this is fixed - functions in std take either "Bound-targeting coercions" as a languge feature should have a separate issue in the RFCs repository. |
Most of the time it happens automatically due to DST coercions
&[T; N] => &[T]
, but sometimes additional care is required.One example, where it currently doesn't work, is comparison operators between slices/arrays/vectors:
When #18465 is implemented, the code comparing byte string literals with slices will break. A lot of such code can be found, for example, in libstd/path.
The text was updated successfully, but these errors were encountered: