Skip to content

Commit

Permalink
Add vec_deque::Iter::as_slices and friends
Browse files Browse the repository at this point in the history
Add the following methods, that work similarly to VecDeque::as_slices:

 - alloc::collections::vec_deque::Iter::as_slices
 - alloc::collections::vec_deque::IterMut::into_slices
 - alloc::collections::vec_deque::IterMut::as_slices
 - alloc::collections::vec_deque::IterMut::as_mut_slices

Obtaining slices from a VecDeque iterator was not previously possible.
  • Loading branch information
chloekek committed Apr 14, 2024
1 parent 29b1207 commit 4a5bcbb
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
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

0 comments on commit 4a5bcbb

Please sign in to comment.