`impl PartialOrd<[U]> for [T]`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
This has an accepted ACP, but I'm going to file a separate issue here since I was having issues implementing it and don't want to forget about it. (Like I have for the past year.)
Essentially, the primary issue is the way we go about specializing PartialOrd for slices, which is not symmetric with the way we go about specializing PartialEq for slices.
Right now, PartialEq<[U]> for [T] relies on a corresponding SlicePartialEq<U> for [T], and there's no issue.
However, because Ord doesn't have a generic parameter, SlicePartialOrd also doesn't, and this means that we can't actually specialize PartialOrd<[U]> for [T]. And I don't really know how to fix things without breaking the existing optimizations in the standard library, which is why I haven't touched this yet.
All the relevant code is in library/core/src/slice/cmp.rs:
impl<T, U> PartialEq<[U]> for [T]
where
T: PartialEq<U>,
{
fn eq(&self, other: &[U]) -> bool {
SlicePartialEq::equal(self, other)
}
fn ne(&self, other: &[U]) -> bool {
SlicePartialEq::not_equal(self, other)
}
}
trait SlicePartialEq<B> {
fn equal(&self, other: &[B]) -> bool;
fn not_equal(&self, other: &[B]) -> bool {
!self.equal(other)
}
}
impl<T: PartialOrd> PartialOrd for [T] {
fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
SlicePartialOrd::partial_compare(self, other)
}
}
trait SlicePartialOrd: Sized {
fn partial_compare(left: &[Self], right: &[Self]) -> Option<Ordering>;
}
The main issue is this impl here:
trait AlwaysApplicableOrd: SliceOrd + Ord {}
impl<A: AlwaysApplicableOrd> SlicePartialOrd for A {
fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
Some(SliceOrd::compare(left, right))
}
}
Which essentially requires that SlicePartialOrd not have a generic parameter, since SliceOrd and Ord don't.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in library/core/src/slice/cmp.rs and compare the SlicePartialEq, SlicePartialOrd, SliceOrd, and AlwaysApplicableOrd implementations. Review the accepted ACP for the intended direction, then determine a design that supports PartialOrd<[U]> for [T] without breaking the existing slice comparison optimizations. Done means the specialization is implemented consistently with PartialEq and its behavior is covered by the relevant standard-library tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100