-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Changes from all commits
274e299
f51b681
beb293d
03b4ea4
8b2e79d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1778,19 +1778,24 @@ impl<T> [T] { | |
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(slice_strip)] | ||
/// let v = &[10, 40, 30]; | ||
/// assert_eq!(v.strip_prefix(&[10]), Some(&[40, 30][..])); | ||
/// assert_eq!(v.strip_prefix(&[10, 40]), Some(&[30][..])); | ||
/// assert_eq!(v.strip_prefix(&[50]), None); | ||
/// assert_eq!(v.strip_prefix(&[10, 50]), None); | ||
/// | ||
/// let prefix : &str = "he"; | ||
/// assert_eq!(b"hello".strip_prefix(prefix.as_bytes()), | ||
/// Some(b"llo".as_ref())); | ||
/// ``` | ||
#[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")] | ||
pub fn strip_prefix<P: SlicePattern<Item = T> + ?Sized>(&self, prefix: &P) -> Option<&[T]> | ||
where | ||
T: PartialEq, | ||
{ | ||
// This function will need rewriting if and when SlicePattern becomes more sophisticated. | ||
let prefix = prefix.as_slice(); | ||
let n = prefix.len(); | ||
if n <= self.len() { | ||
let (head, tail) = self.split_at(n); | ||
|
@@ -1811,19 +1816,20 @@ impl<T> [T] { | |
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(slice_strip)] | ||
/// let v = &[10, 40, 30]; | ||
/// assert_eq!(v.strip_suffix(&[30]), Some(&[10, 40][..])); | ||
/// assert_eq!(v.strip_suffix(&[40, 30]), Some(&[10][..])); | ||
/// assert_eq!(v.strip_suffix(&[50]), None); | ||
/// assert_eq!(v.strip_suffix(&[50, 30]), None); | ||
/// ``` | ||
#[must_use = "returns the subslice without modifying the original"] | ||
#[unstable(feature = "slice_strip", issue = "73413")] | ||
pub fn strip_suffix(&self, suffix: &[T]) -> Option<&[T]> | ||
#[stable(feature = "slice_strip", since = "1.50.0")] | ||
pub fn strip_suffix<P: SlicePattern<Item = T> + ?Sized>(&self, suffix: &P) -> Option<&[T]> | ||
where | ||
T: PartialEq, | ||
{ | ||
// This function will need rewriting if and when SlicePattern becomes more sophisticated. | ||
let suffix = suffix.as_slice(); | ||
let (len, n) = (self.len(), suffix.len()); | ||
if n <= len { | ||
let (head, tail) = self.split_at(len - n); | ||
|
@@ -3216,3 +3222,35 @@ impl<T> Default for &mut [T] { | |
&mut [] | ||
} | ||
} | ||
|
||
#[unstable(feature = "slice_pattern", reason = "stopgap trait for slice patterns", issue = "56345")] | ||
/// 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 { | ||
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. Should we seal this trait? 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. I don't think it is necessary right now since the trait is still unstable. |
||
/// The element type of the slice being matched on. | ||
type Item; | ||
|
||
/// Currently, the consumers of `SlicePattern` need a slice. | ||
fn as_slice(&self) -> &[Self::Item]; | ||
} | ||
|
||
#[stable(feature = "slice_strip", since = "1.50.0")] | ||
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. Stability attributes don't work on trait impls, I think you can just remove them. 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. They are being rendered by rustdoc, e.g. https://doc.rust-lang.org/core/num/struct.NonZeroU128.html#impl-TryFrom%3CNonZeroU128%3E-1 |
||
impl<T> SlicePattern for [T] { | ||
type Item = T; | ||
|
||
#[inline] | ||
fn as_slice(&self) -> &[Self::Item] { | ||
self | ||
} | ||
} | ||
|
||
#[stable(feature = "slice_strip", since = "1.50.0")] | ||
impl<T, const N: usize> SlicePattern for [T; N] { | ||
type Item = T; | ||
|
||
#[inline] | ||
fn as_slice(&self) -> &[Self::Item] { | ||
self | ||
} | ||
} |
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.
This should be 1.51 right ?
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.
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 ?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.
This PR is on 1.51.0 milestone according to rustbot.
Yes, someone could send another PR to update the stabilization version.