Skip to content
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

Stabilize slice::strip_prefix and slice::strip_suffix #77853

Merged
merged 5 commits into from
Jan 7, 2021

Conversation

ijackson
Copy link
Contributor

These two methods are useful. The corresponding methods on str are already stable.

I believe that stablising these now would not get in the way of, in the future, extending these to take a richer pattern API a la str's patterns.

Tracking PR: #73413. I also have an outstanding PR to improve the docs for these two functions and the corresponding ones on str: #75078

I have tried to follow the instructions in the dev guide. The part to do with compiler/rustc_feature did not seem applicable. I assume that's because these are just library features, so there is no corresponding machinery in rustc.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 12, 2020
@jyn514 jyn514 added needs-fcp This change is insta-stable, so needs a completed FCP to proceed. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 12, 2020
@jonas-schievink jonas-schievink added the relnotes Marks issues that should be documented in the release notes of the next release. label Oct 12, 2020
@ijackson ijackson changed the title Stablize slice::strip_prefix and slice::strip_suffix Stabilize slice::strip_prefix and slice::strip_suffix Oct 12, 2020
@Mark-Simulacrum
Copy link
Member

r? @Amanieu for t-libs approval

@Amanieu
Copy link
Member

Amanieu commented Oct 15, 2020

@rfcbot fcp merge

This API seems fine for now, but rust-lang/rfcs#2500 plans to generalize them to take a Pattern as an argument instead.

@rfcbot
Copy link

rfcbot commented Oct 15, 2020

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 15, 2020
@Mark-Simulacrum
Copy link
Member

That seems like it will be at least an inference break - do we want to try to future proof these somehow?

@KodrAus
Copy link
Contributor

KodrAus commented Oct 23, 2020

The stabilized str variants expose the unstable Pattern trait. Is there anything stopping us from doing that here too?

@tesuji
Copy link
Contributor

tesuji commented Oct 23, 2020

@KodrAus std::str::pattern::Pattern is str oriented, .i.e. it only supports string type.
There are plans to generalize Pattern to other types. But those are not implemented.

@ijackson
Copy link
Contributor Author

ijackson commented Oct 23, 2020 via email

@crlf0710 crlf0710 added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 13, 2020
@KodrAus
Copy link
Contributor

KodrAus commented Nov 26, 2020

We discussed this a bit in the recent Libs meeting and felt that if we wanted to bring this forward and stabilize it ahead of reworking the pattern API to be non-str specific then we could reduce some churn by making the signatures generic on a trivial unstable trait that we replace with Pattern when we can.

Something like:

#[unstable] // We never stabilize this trait
pub trait SlicePattern {
    type Item;
    fn as_slice(&self) -> &[Self::Item];
}

impl<T> SlicePattern for [T] {
    type Item = T;

    fn as_slice(&self) -> &[Self::Item] {
        self
    }
}

impl<T> [T] {
    #[stable]
    pub fn strip_prefix<P: SlicePattern<Item = T>>(&self, prefix: P) -> Option<&[T]>;

    #[stable]
    pub fn strip_suffix<P: SlicePattern<Item = T>>(&self, suffix: P) -> Option<&[T]>;
}

We may still get breakage from inference failures, but bring the API closer to what we actually want in the long term, and only need to change the unstable backing trait.

What do you think?

@ijackson
Copy link
Contributor Author

ijackson commented Nov 26, 2020 via email

@ijackson
Copy link
Contributor Author

ijackson commented Dec 3, 2020

Hi. I have updated this now. I wrote a bunch of stuff in the commit message which github likes to hide so here it is for your err delectation and delight?

Stablize slice::strip_prefix and strip_suffix, with SlicePattern

We hope later to extend `core::str::Pattern` to slices too, perhaps as
part of stabilising that.  We want to minimise the amount of type
inference breakage when we do that, so we don't want to stabilise
strip_prefix and strip_suffix taking a simple `&[T]`.

@KodrAus suggested the approach of introducing a new perma-unstable
trait, which reduces this future inference break risk.

I found it necessary to make two impls of this trait, as the unsize
coercion don't apply when hunting for trait implementations.

Since SlicePattern's only method returns a reference, and the whole
trait is just a wrapper for slices, I made the trait type be the
non-reference type [T] or [T;N] rather than the reference.  Otherwise
the trait would have a lifetime parameter.

I marked both the no-op conversion functions `#[inline]`.  I'm not
sure if that is necessary but it seemed at the very least harmless.

@m-ou-se m-ou-se added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Dec 8, 2020
Copy link
Contributor

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ijackson! This looks like a good approach to me.

library/core/src/slice/mod.rs Outdated Show resolved Hide resolved
@Amanieu
Copy link
Member

Amanieu commented Dec 21, 2020

@rust-lang/libs could you fill in checkboxes in #77853 (comment) considering the updated proposal with a trait bound?

library/core/src/slice/mod.rs Outdated Show resolved Hide resolved
@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Dec 27, 2020
@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Jan 6, 2021
/// Patterns in slices - currently, only used by `strip_prefix` and `strip_suffix`. At a future
/// point, we hope to generalise `core::str::Pattern` (which at the time of writing is limited to
/// `str`) to slices, and then this trait will be replaced or abolished.
pub trait SlicePattern {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we seal this trait?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary right now since the trait is still unstable.

fn as_slice(&self) -> &[Self::Item];
}

#[stable(feature = "slice_strip", since = "1.50.0")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stability attributes don't work on trait impls, I think you can just remove them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Amanieu
Copy link
Member

Amanieu commented Jan 7, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Jan 7, 2021

📌 Commit 8b2e79d has been approved by Amanieu

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 7, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jan 7, 2021
Stabilize slice::strip_prefix and slice::strip_suffix

These two methods are useful.  The corresponding methods on `str` are already stable.

I believe that stablising these now would not get in the way of, in the future, extending these to take a richer pattern API a la `str`'s patterns.

Tracking PR: rust-lang#73413.  I also have an outstanding PR to improve the docs for these two functions and the corresponding ones on `str`: rust-lang#75078

I have tried to follow the [instructions in the dev guide](https://rustc-dev-guide.rust-lang.org/stabilization_guide.html#stabilization-pr).  The part to do with `compiler/rustc_feature` did not seem applicable.  I assume that's because these are just library features, so there is no corresponding machinery in rustc.
@bors
Copy link
Contributor

bors commented Jan 7, 2021

⌛ Testing commit 8b2e79d with merge 8f0b945...

@bors
Copy link
Contributor

bors commented Jan 7, 2021

☀️ Test successful - checks-actions
Approved by: Amanieu
Pushing 8f0b945 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 7, 2021
@bors bors merged commit 8f0b945 into rust-lang:master Jan 7, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 7, 2021
@ijackson
Copy link
Contributor Author

ijackson commented Jan 7, 2021

Yay :-). Thanks all

/// ```
#[must_use = "returns the subslice without modifying the original"]
#[unstable(feature = "slice_strip", issue = "73413")]
pub fn strip_prefix(&self, prefix: &[T]) -> Option<&[T]>
#[stable(feature = "slice_strip", since = "1.50.0")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 1.51 right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, perhaps. I have just tried looking for git tags or something to tell me which Rust versions contain 8f0b945, the relevant commit, but there don't seem to be any tags for beta versions. I guess that makes sense. I found that origin/beta does not contain that commit, and the forge tells me beta is 1.50. So should I send a MR to fix this ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is on 1.51.0 milestone according to rustbot.
Yes, someone could send another PR to update the stabilization version.

@ijackson
Copy link
Contributor Author

ijackson commented Jan 13, 2021 via email

ijackson added a commit to ijackson/rust that referenced this pull request Jan 13, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jan 14, 2021
Fix stabilisation version of slice_strip

See rust-lang#77853 (review)

Signed-off-by: Ian Jackson <[email protected]>
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jan 14, 2021
Fix stabilisation version of slice_strip

See rust-lang#77853 (review)

Signed-off-by: Ian Jackson <[email protected]>
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jan 14, 2021
Fix stabilisation version of slice_strip

See rust-lang#77853 (review)

Signed-off-by: Ian Jackson <[email protected]>
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Jan 14, 2021
Fix stabilisation version of slice_strip

See rust-lang#77853 (review)

Signed-off-by: Ian Jackson <[email protected]>
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Jan 14, 2021
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Jan 14, 2021
Fix stabilisation version of slice_strip

See rust-lang#77853 (review)

Signed-off-by: Ian Jackson <[email protected]>
@ijackson ijackson deleted the slice-strip-stab branch August 24, 2021 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.