Skip to content

Implement split_pattern on slices #131340

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
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,8 @@ declare_features! (
(unstable, simd_ffi, "1.0.0", Some(27731)),
/// Allows specialization of implementations (RFC 1210).
(incomplete, specialization, "1.7.0", Some(31844)),
/// Allows splitting a slice by another slice
(unstable, split_pattern, "CURRENT_RUSTC_VERSION", Some(49036)),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Allows splitting a slice by another slice
(unstable, split_pattern, "CURRENT_RUSTC_VERSION", Some(49036)),

Library features shouldn't (and don't need to) be declared inside the compiler.

/// Allows attributes on expressions and non-item statements.
(unstable, stmt_expr_attributes, "1.6.0", Some(15701)),
/// Allows lints part of the strict provenance effort.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,7 @@ symbols! {
soft,
specialization,
speed,
split_pattern,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
split_pattern,

(see above)

spotlight,
sqrtf128,
sqrtf16,
Expand Down
138 changes: 138 additions & 0 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,144 @@ forward_iterator! { RSplitN: T, &'a [T] }
forward_iterator! { SplitNMut: T, &'a mut [T] }
forward_iterator! { RSplitNMut: T, &'a mut [T] }

/// An iterator over subslices separated by a given slice
///
/// This struct is created by the [`split_pattern`] method on [slices].
///
/// # Example
///
/// ```
/// #![feature(split_pattern)]
/// let slice = [10, 10, 40, 33, 30, 10, 40, 20];
/// let pat = [10, 40];
/// let mut iter = slice.split_pattern(&pat);
/// assert_eq!(iter.next(), Some(&[10][..]));
/// assert_eq!(iter.next(), Some(&[33, 30][..]));
/// assert_eq!(iter.next(), Some(&[20][..]));
/// assert_eq!(iter.next(), None);
/// ```
///
/// [`split_pattern`]: slice::split_pattern
/// [slices]: slice
#[unstable(feature = "split_pattern", issue = "49036")]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct SplitPattern<'a, 'b, T>
where
T: cmp::PartialEq,
{
v: &'a [T],
pattern: &'b [T],
finished: bool,
}

#[unstable(feature = "split_pattern", issue = "49036")]
impl<'a, 'b, T: cmp::PartialEq> SplitPattern<'a, 'b, T> {
#![allow(unused)]
#[inline]
pub(super) fn new(slice: &'a [T], pattern: &'b [T]) -> Self {
Self { v: slice, pattern, finished: false }
}
}

#[unstable(feature = "split_pattern", issue = "49036")]
impl<T: fmt::Debug> fmt::Debug for SplitPattern<'_, '_, T>
where
T: cmp::PartialEq,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SplitPattern")
.field("v", &self.v)
.field("pattern", &self.pattern)
.field("finished", &self.finished)
.finish()
}
}

#[unstable(feature = "split_pattern", issue = "49036")]
impl<T> Clone for SplitPattern<'_, '_, T>
where
T: cmp::PartialEq,
{
fn clone(&self) -> Self {
SplitPattern { v: self.v, pattern: self.pattern, finished: self.finished }
}
}

#[unstable(feature = "split_pattern", issue = "49036")]
impl<'a, 'b, T> Iterator for SplitPattern<'a, 'b, T>
where
T: cmp::PartialEq,
{
type Item = &'a [T];

#[inline]
fn next(&mut self) -> Option<&'a [T]> {
if self.finished {
return None;
}

for i in 0..self.v.len() {
if self.v[i..].starts_with(&self.pattern) {
let (left, right) = (&self.v[0..i], &self.v[i + self.pattern.len()..]);
let ret = Some(left);
self.v = right;
return ret;
}
}
self.finish()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
if self.finished {
(0, Some(0))
} else {
// If the predicate doesn't match anything, we yield one slice.
// If it matches every element, we yield `len() + 1` empty slices.
(1, Some(self.v.len() + 1))
}
}
}

#[unstable(feature = "split_pattern", issue = "49036")]
impl<'a, 'b, T> DoubleEndedIterator for SplitPattern<'a, 'b, T>
where
T: cmp::PartialEq,
{
#[inline]
fn next_back(&mut self) -> Option<&'a [T]> {
if self.finished {
return None;
}

for i in (0..self.v.len()).rev() {
if self.v[..i].ends_with(&self.pattern) {
let (left, right) = (&self.v[i..], &self.v[..i - self.pattern.len()]);
let ret = Some(left);
self.v = right;
return ret;
}
}
self.finish()
}
}

#[unstable(feature = "split_pattern", issue = "49036")]
impl<'a, 'b, T> SplitIter for SplitPattern<'a, 'b, T>
where
T: cmp::PartialEq,
{
#[inline]
fn finish(&mut self) -> Option<&'a [T]> {
if self.finished {
None
} else {
self.finished = true;
Some(self.v)
}
}
}

/// An iterator over overlapping subslices of length `size`.
///
/// This struct is created by the [`windows`] method on [slices].
Expand Down
12 changes: 12 additions & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub use index::SliceIndex;
pub use index::{range, try_range};
#[unstable(feature = "array_windows", issue = "75027")]
pub use iter::ArrayWindows;
#[unstable(feature = "split_pattern", issue = "49036")]
pub use iter::SplitPattern;
#[unstable(feature = "array_chunks", issue = "74985")]
pub use iter::{ArrayChunks, ArrayChunksMut};
#[stable(feature = "slice_group_by", since = "1.77.0")]
Expand Down Expand Up @@ -4042,6 +4044,16 @@ impl<T> [T] {
unsafe { self.align_to() }
}

/// Splits a slice by a pattern
#[unstable(feature = "split_pattern", issue = "49036")]
#[inline]
pub fn split_pattern<'a, 'b>(&'a self, pattern: &'b [T]) -> SplitPattern<'a, 'b, T>
where
T: PartialEq,
{
SplitPattern::new(&self, pattern)
}

/// Splits a mutable slice into a mutable prefix, a middle of aligned SIMD types,
/// and a mutable suffix.
///
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/feature-gates/feature-gate-split-pattern.rs
Copy link
Member

Choose a reason for hiding this comment

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

This UI test should be removed. It's only required to exist right now by tidy because you declared a library feature as a compiler feature, too. The doctest should be sufficient.

Copy link
Author

Choose a reason for hiding this comment

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

Done! I was following the rustc-dev-guide and thought that all unstable features should be declared in the compiler, thanks for clarifying!

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn main() {
let a = &[1, 2, 3, 4, 5];
let b = &[2, 3];
let mut c = a.split_pattern(b); //~ ERROR use of unstable library feature 'split_pattern'
}
13 changes: 13 additions & 0 deletions tests/ui/feature-gates/feature-gate-split-pattern.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0658]: use of unstable library feature 'split_pattern'
--> $DIR/feature-gate-split-pattern.rs:4:19
|
LL | let mut c = a.split_pattern(b);
| ^^^^^^^^^^^^^
|
= note: see issue #49036 <https://github.com/rust-lang/rust/issues/49036> for more information
= help: add `#![feature(split_pattern)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0658`.
Loading