-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Make slice comparisons const #143925
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
Open
oli-obk
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
oli-obk:slice-const-partialeq
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Make slice comparisons const #143925
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,10 @@ use crate::num::NonZero; | |
use crate::ops::ControlFlow; | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<T, U> PartialEq<[U]> for [T] | ||
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")] | ||
impl<T, U> const PartialEq<[U]> for [T] | ||
where | ||
T: PartialEq<U>, | ||
T: ~const PartialEq<U>, | ||
{ | ||
fn eq(&self, other: &[U]) -> bool { | ||
SlicePartialEq::equal(self, other) | ||
|
@@ -94,6 +95,8 @@ impl<T: PartialOrd> PartialOrd for [T] { | |
|
||
#[doc(hidden)] | ||
// intermediate trait for specialization of slice's PartialEq | ||
#[const_trait] | ||
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")] | ||
trait SlicePartialEq<B> { | ||
fn equal(&self, other: &[B]) -> bool; | ||
|
||
|
@@ -103,9 +106,10 @@ trait SlicePartialEq<B> { | |
} | ||
|
||
// Generic slice equality | ||
impl<A, B> SlicePartialEq<B> for [A] | ||
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")] | ||
impl<A, B> const SlicePartialEq<B> for [A] | ||
where | ||
A: PartialEq<B>, | ||
A: ~const PartialEq<B>, | ||
{ | ||
default fn equal(&self, other: &[B]) -> bool { | ||
if self.len() != other.len() { | ||
|
@@ -115,11 +119,14 @@ where | |
// Implemented as explicit indexing rather | ||
// than zipped iterators for performance reasons. | ||
// See PR https://github.com/rust-lang/rust/pull/116846 | ||
for idx in 0..self.len() { | ||
// FIXME(const_hack): make this a `for idx in 0..self.len()` loop. | ||
let mut idx = 0; | ||
while idx < self.len() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should get a |
||
// bound checks are optimized away | ||
if self[idx] != other[idx] { | ||
return false; | ||
} | ||
idx += 1; | ||
} | ||
|
||
true | ||
|
@@ -128,9 +135,10 @@ where | |
|
||
// When each element can be compared byte-wise, we can compare all the bytes | ||
// from the whole size in one call to the intrinsics. | ||
impl<A, B> SlicePartialEq<B> for [A] | ||
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")] | ||
impl<A, B> const SlicePartialEq<B> for [A] | ||
where | ||
A: BytewiseEq<B>, | ||
A: ~const BytewiseEq<B>, | ||
{ | ||
fn equal(&self, other: &[B]) -> bool { | ||
if self.len() != other.len() { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's clearly something interesting going on here, please add a comment explaining what.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with this being a temporary solution. To me, we should change
#[derive_const(PartialEq)]
to something like#[derive(PartialEq(const))]
(syntax to-be-bikeshedded), then we can have#[derive(PartialEq(const, unstable))]
in all standard library crates.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does it know which feature gate to use...?
We're fairly close to actually enforcing feature stability on impls (#140399) so this question matters soon, IIUC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it doesn't. All derived const trait impls are under the
derive_const
feature gate