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

Add vec_deque::Iter::as_slices and friends #123947

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 34 additions & 0 deletions library/alloc/src/collections/vec_deque/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,40 @@ impl<'a, T> Iter<'a, T> {
pub(super) fn new(i1: slice::Iter<'a, T>, i2: slice::Iter<'a, T>) -> Self {
Self { i1, i2 }
}

/// Views the underlying data as a pair of subslices of the original data.
///
/// The slices contain, in order, the contents of the deque not yet yielded
/// by the iterator.
///
/// This has the same lifetime as the original `VecDeque`, and so the
/// iterator can continue to be used while this exists.
///
/// # Examples
///
/// ```
/// #![feature(vec_deque_iter_as_slices)]
///
/// use std::collections::VecDeque;
///
/// let mut deque = VecDeque::new();
/// deque.push_back(0);
/// deque.push_back(1);
/// deque.push_back(2);
/// deque.push_front(10);
/// deque.push_front(9);
/// deque.push_front(8);
///
/// let mut iter = deque.iter();
/// iter.next();
/// iter.next_back();
///
/// assert_eq!(iter.as_slices(), (&[9, 10][..], &[0, 1][..]));
/// ```
#[unstable(feature = "vec_deque_iter_as_slices", issue = "123947")]
pub fn as_slices(&self) -> (&'a [T], &'a [T]) {
(self.i1.as_slice(), self.i2.as_slice())
}
}

#[stable(feature = "collection_debug", since = "1.17.0")]
Expand Down
107 changes: 107 additions & 0 deletions library/alloc/src/collections/vec_deque/iter_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,113 @@ impl<'a, T> IterMut<'a, T> {
pub(super) fn new(i1: slice::IterMut<'a, T>, i2: slice::IterMut<'a, T>) -> Self {
Self { i1, i2 }
}

/// Views the underlying data as a pair of subslices of the original data.
///
/// The slices contain, in order, the contents of the deque not yet yielded
/// by the iterator.
///
/// To avoid creating `&mut` references that alias, this is forced to
/// consume the iterator.
///
/// # Examples
///
/// ```
/// #![feature(vec_deque_iter_as_slices)]
///
/// use std::collections::VecDeque;
///
/// let mut deque = VecDeque::new();
/// deque.push_back(0);
/// deque.push_back(1);
/// deque.push_back(2);
/// deque.push_front(10);
/// deque.push_front(9);
/// deque.push_front(8);
///
/// let mut iter = deque.iter_mut();
/// iter.next();
/// iter.next_back();
///
/// let slices = iter.into_slices();
/// slices.0[0] = 42;
/// slices.1[0] = 24;
/// assert_eq!(deque.as_slices(), (&[8, 42, 10][..], &[24, 1, 2][..]));
/// ```
#[unstable(feature = "vec_deque_iter_as_slices", issue = "123947")]
pub fn into_slices(self) -> (&'a mut [T], &'a mut [T]) {
(self.i1.into_slice(), self.i2.into_slice())
}

/// Views the underlying data as a pair of subslices of the original data.
///
/// The slices contain, in order, the contents of the deque not yet yielded
/// by the iterator.
///
/// To avoid creating `&mut [T]` references that alias, the returned slices
/// borrow their lifetimes from the iterator the method is applied on.
///
/// # Examples
///
/// ```
/// #![feature(vec_deque_iter_as_slices)]
///
/// use std::collections::VecDeque;
///
/// let mut deque = VecDeque::new();
/// deque.push_back(0);
/// deque.push_back(1);
/// deque.push_back(2);
/// deque.push_front(10);
/// deque.push_front(9);
/// deque.push_front(8);
///
/// let mut iter = deque.iter_mut();
/// iter.next();
/// iter.next_back();
///
/// assert_eq!(iter.as_slices(), (&[9, 10][..], &[0, 1][..]));
/// ```
#[unstable(feature = "vec_deque_iter_as_slices", issue = "123947")]
pub fn as_slices(&self) -> (&[T], &[T]) {
(self.i1.as_slice(), self.i2.as_slice())
}

/// Views the underlying data as a pair of subslices of the original data.
///
/// The slices contain, in order, the contents of the deque not yet yielded
/// by the iterator.
///
/// To avoid creating `&mut [T]` references that alias, the returned slices
/// borrow their lifetimes from the iterator the method is applied on.
///
/// # Examples
///
/// ```
/// #![feature(vec_deque_iter_as_slices)]
///
/// use std::collections::VecDeque;
///
/// let mut deque = VecDeque::new();
/// deque.push_back(0);
/// deque.push_back(1);
/// deque.push_back(2);
/// deque.push_front(10);
/// deque.push_front(9);
/// deque.push_front(8);
///
/// let mut iter = deque.iter_mut();
/// iter.next();
/// iter.next_back();
///
/// iter.as_mut_slices().0[0] = 42;
/// iter.as_mut_slices().1[0] = 24;
/// assert_eq!(deque.as_slices(), (&[8, 42, 10][..], &[24, 1, 2][..]));
/// ```
#[unstable(feature = "vec_deque_iter_as_slices", issue = "123947")]
pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) {
(self.i1.as_mut_slice(), self.i2.as_mut_slice())
}
}

#[stable(feature = "collection_debug", since = "1.17.0")]
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
#![feature(sized_type_properties)]
#![feature(slice_from_ptr_range)]
#![feature(slice_index_methods)]
#![feature(slice_iter_mut_as_mut_slice)]
#![feature(slice_ptr_get)]
#![feature(slice_range)]
#![feature(std_internals)]
Expand Down
Loading