use crate::fmt;
use crate::intrinsics::{assume, exact_div, unchecked_sub};
use crate::iter::{FusedIterator, TrustedLen, TrustedRandomAccess};
-use crate::marker::{self, Send, Sized, Sync};
+use crate::marker::{PhantomData, Send, Sized, Sync};
use crate::mem;
use crate::ptr::NonNull;
/// [slices]: ../../std/primitive.slice.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, T: 'a> {
- pub(super) ptr: NonNull<T>,
- pub(super) end: *const T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
+ ptr: NonNull<T>,
+ end: *const T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
// ptr == end is a quick test for the Iterator being empty, that works
// for both ZST and non-ZST.
- pub(super) _marker: marker::PhantomData<&'a T>,
+ _marker: PhantomData<&'a T>,
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]
unsafe impl<T: Sync> Send for Iter<'_, T> {}
impl<'a, T> Iter<'a, T> {
+ pub(super) fn new(ptr: NonNull<T>, end: *const T) -> Self {
+ Self { ptr, end, _marker: PhantomData }
+ }
+
/// Views the underlying data as a subslice of the original data.
///
/// This has the same lifetime as the original slice, and so the
/// [slices]: ../../std/primitive.slice.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IterMut<'a, T: 'a> {
- pub(super) ptr: NonNull<T>,
- pub(super) end: *mut T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
+ ptr: NonNull<T>,
+ end: *mut T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
// ptr == end is a quick test for the Iterator being empty, that works
// for both ZST and non-ZST.
- pub(super) _marker: marker::PhantomData<&'a mut T>,
+ _marker: PhantomData<&'a mut T>,
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]
unsafe impl<T: Send> Send for IterMut<'_, T> {}
impl<'a, T> IterMut<'a, T> {
+ pub(super) fn new(ptr: NonNull<T>, end: *mut T) -> Self {
+ Self { ptr, end, _marker: PhantomData }
+ }
+
/// Views the underlying data as a subslice of the original data.
///
/// To avoid creating `&mut` references that alias, this is forced
where
P: FnMut(&T) -> bool,
{
- pub(super) v: &'a [T],
- pub(super) pred: P,
- pub(super) finished: bool,
+ v: &'a [T],
+ pred: P,
+ finished: bool,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> Split<'a, T, P> {
+ pub(super) fn new(slice: &'a [T], pred: P, finished: bool) -> Self {
+ Self { v: slice, pred, finished }
+ }
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]
where
P: FnMut(&T) -> bool,
{
- pub(super) v: &'a [T],
- pub(super) pred: P,
- pub(super) finished: bool,
+ v: &'a [T],
+ pred: P,
+ finished: bool,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitInclusive<'a, T, P> {
+ pub(super) fn new(slice: &'a [T], pred: P, finished: bool) -> Self {
+ Self { v: slice, pred, finished }
+ }
}
#[unstable(feature = "split_inclusive", issue = "72360")]
where
P: FnMut(&T) -> bool,
{
- pub(super) v: &'a mut [T],
- pub(super) pred: P,
- pub(super) finished: bool,
+ v: &'a mut [T],
+ pred: P,
+ finished: bool,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitMut<'a, T, P> {
+ pub(super) fn new(slice: &'a mut [T], pred: P, finished: bool) -> Self {
+ Self { v: slice, pred, finished }
+ }
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]
where
P: FnMut(&T) -> bool,
{
- pub(super) v: &'a mut [T],
- pub(super) pred: P,
- pub(super) finished: bool,
+ v: &'a mut [T],
+ pred: P,
+ finished: bool,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitInclusiveMut<'a, T, P> {
+ pub(super) fn new(slice: &'a mut [T], pred: P, finished: bool) -> Self {
+ Self { v: slice, pred, finished }
+ }
}
#[unstable(feature = "split_inclusive", issue = "72360")]
where
P: FnMut(&T) -> bool,
{
- pub(super) inner: Split<'a, T, P>,
+ inner: Split<'a, T, P>,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> RSplit<'a, T, P> {
+ pub(super) fn new(slice: &'a [T], pred: P, finished: bool) -> Self {
+ Self { inner: Split::new(slice, pred, finished) }
+ }
}
#[stable(feature = "slice_rsplit", since = "1.27.0")]
where
P: FnMut(&T) -> bool,
{
- pub(super) inner: SplitMut<'a, T, P>,
+ inner: SplitMut<'a, T, P>,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> RSplitMut<'a, T, P> {
+ pub(super) fn new(slice: &'a mut [T], pred: P, finished: bool) -> Self {
+ Self { inner: SplitMut::new(slice, pred, finished) }
+ }
}
#[stable(feature = "slice_rsplit", since = "1.27.0")]
/// match a predicate function, splitting at most a fixed number of
/// times.
#[derive(Debug)]
-pub(super) struct GenericSplitN<I> {
- pub(super) iter: I,
- pub(super) count: usize,
+struct GenericSplitN<I> {
+ iter: I,
+ count: usize,
}
impl<T, I: SplitIter<Item = T>> Iterator for GenericSplitN<I> {
where
P: FnMut(&T) -> bool,
{
- pub(super) inner: GenericSplitN<Split<'a, T, P>>,
+ inner: GenericSplitN<Split<'a, T, P>>,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitN<'a, T, P> {
+ pub(super) fn new(s: Split<'a, T, P>, n: usize) -> Self {
+ Self { inner: GenericSplitN { iter: s, count: n } }
+ }
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]
where
P: FnMut(&T) -> bool,
{
- pub(super) inner: GenericSplitN<RSplit<'a, T, P>>,
+ inner: GenericSplitN<RSplit<'a, T, P>>,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> RSplitN<'a, T, P> {
+ pub(super) fn new(s: RSplit<'a, T, P>, n: usize) -> Self {
+ Self { inner: GenericSplitN { iter: s, count: n } }
+ }
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]
where
P: FnMut(&T) -> bool,
{
- pub(super) inner: GenericSplitN<SplitMut<'a, T, P>>,
+ inner: GenericSplitN<SplitMut<'a, T, P>>,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitNMut<'a, T, P> {
+ pub(super) fn new(s: SplitMut<'a, T, P>, n: usize) -> Self {
+ Self { inner: GenericSplitN { iter: s, count: n } }
+ }
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]
where
P: FnMut(&T) -> bool,
{
- pub(super) inner: GenericSplitN<RSplitMut<'a, T, P>>,
+ inner: GenericSplitN<RSplitMut<'a, T, P>>,
+}
+
+impl<'a, T: 'a, P: FnMut(&T) -> bool> RSplitNMut<'a, T, P> {
+ pub(super) fn new(s: RSplitMut<'a, T, P>, n: usize) -> Self {
+ Self { inner: GenericSplitN { iter: s, count: n } }
+ }
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]
#[derive(Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Windows<'a, T: 'a> {
- pub(super) v: &'a [T],
- pub(super) size: usize,
+ v: &'a [T],
+ size: usize,
+}
+
+impl<'a, T: 'a> Windows<'a, T> {
+ pub(super) fn new(slice: &'a [T], size: usize) -> Self {
+ Self { v: slice, size }
+ }
}
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
#[derive(Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Chunks<'a, T: 'a> {
- pub(super) v: &'a [T],
- pub(super) chunk_size: usize,
+ v: &'a [T],
+ chunk_size: usize,
+}
+
+impl<'a, T: 'a> Chunks<'a, T> {
+ pub(super) fn new(slice: &'a [T], size: usize) -> Self {
+ Self { v: slice, chunk_size: size }
+ }
}
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
#[derive(Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct ChunksMut<'a, T: 'a> {
- pub(super) v: &'a mut [T],
- pub(super) chunk_size: usize,
+ v: &'a mut [T],
+ chunk_size: usize,
+}
+
+impl<'a, T: 'a> ChunksMut<'a, T> {
+ pub(super) fn new(slice: &'a mut [T], size: usize) -> Self {
+ Self { v: slice, chunk_size: size }
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Debug)]
#[stable(feature = "chunks_exact", since = "1.31.0")]
pub struct ChunksExact<'a, T: 'a> {
- pub(super) v: &'a [T],
- pub(super) rem: &'a [T],
- pub(super) chunk_size: usize,
+ v: &'a [T],
+ rem: &'a [T],
+ chunk_size: usize,
+}
+
+impl<'a, T: 'a> ChunksExact<'a, T> {
+ pub(super) fn new(slice: &'a [T], rem: &'a [T], size: usize) -> Self {
+ Self { v: slice, rem, chunk_size: size }
+ }
}
impl<'a, T> ChunksExact<'a, T> {
#[derive(Debug)]
#[stable(feature = "chunks_exact", since = "1.31.0")]
pub struct ChunksExactMut<'a, T: 'a> {
- pub(super) v: &'a mut [T],
- pub(super) rem: &'a mut [T],
- pub(super) chunk_size: usize,
+ v: &'a mut [T],
+ rem: &'a mut [T],
+ chunk_size: usize,
+}
+
+impl<'a, T: 'a> ChunksExactMut<'a, T> {
+ pub(super) fn new(slice: &'a mut [T], rem: &'a mut [T], size: usize) -> Self {
+ Self { v: slice, rem, chunk_size: size }
+ }
}
impl<'a, T> ChunksExactMut<'a, T> {
#[derive(Debug, Clone, Copy)]
#[unstable(feature = "array_windows", issue = "75027")]
pub struct ArrayWindows<'a, T: 'a, const N: usize> {
- pub(crate) slice_head: *const T,
- pub(crate) num: usize,
- pub(crate) marker: marker::PhantomData<&'a [T; N]>,
+ slice_head: *const T,
+ num: usize,
+ marker: PhantomData<&'a [T; N]>,
+}
+
+impl<'a, T: 'a, const N: usize> ArrayWindows<'a, T, N> {
+ pub(super) fn new(head: *const T, num: usize) -> Self {
+ Self { slice_head: head, num, marker: PhantomData }
+ }
}
#[unstable(feature = "array_windows", issue = "75027")]
#[derive(Debug)]
#[unstable(feature = "array_chunks", issue = "74985")]
pub struct ArrayChunks<'a, T: 'a, const N: usize> {
- pub(super) iter: Iter<'a, [T; N]>,
- pub(super) rem: &'a [T],
+ iter: Iter<'a, [T; N]>,
+ rem: &'a [T],
+}
+
+impl<'a, T: 'a, const N: usize> ArrayChunks<'a, T, N> {
+ pub(super) fn new(iter: Iter<'a, [T; N]>, rem: &'a [T]) -> Self {
+ Self { iter, rem }
+ }
}
impl<'a, T, const N: usize> ArrayChunks<'a, T, N> {
#[derive(Debug)]
#[unstable(feature = "array_chunks", issue = "74985")]
pub struct ArrayChunksMut<'a, T: 'a, const N: usize> {
- pub(super) iter: IterMut<'a, [T; N]>,
- pub(super) rem: &'a mut [T],
+ iter: IterMut<'a, [T; N]>,
+ rem: &'a mut [T],
+}
+
+impl<'a, T: 'a, const N: usize> ArrayChunksMut<'a, T, N> {
+ pub(super) fn new(iter: IterMut<'a, [T; N]>, rem: &'a mut [T]) -> Self {
+ Self { iter, rem }
+ }
}
impl<'a, T, const N: usize> ArrayChunksMut<'a, T, N> {
#[derive(Debug)]
#[stable(feature = "rchunks", since = "1.31.0")]
pub struct RChunks<'a, T: 'a> {
- pub(super) v: &'a [T],
- pub(super) chunk_size: usize,
+ v: &'a [T],
+ chunk_size: usize,
+}
+
+impl<'a, T: 'a> RChunks<'a, T> {
+ pub(super) fn new(slice: &'a [T], size: usize) -> Self {
+ Self { v: slice, chunk_size: size }
+ }
}
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
#[derive(Debug)]
#[stable(feature = "rchunks", since = "1.31.0")]
pub struct RChunksMut<'a, T: 'a> {
- pub(super) v: &'a mut [T],
- pub(super) chunk_size: usize,
+ v: &'a mut [T],
+ chunk_size: usize,
+}
+
+impl<'a, T: 'a> RChunksMut<'a, T> {
+ pub(super) fn new(slice: &'a mut [T], size: usize) -> Self {
+ Self { v: slice, chunk_size: size }
+ }
}
#[stable(feature = "rchunks", since = "1.31.0")]
#[derive(Debug)]
#[stable(feature = "rchunks", since = "1.31.0")]
pub struct RChunksExact<'a, T: 'a> {
- pub(super) v: &'a [T],
- pub(super) rem: &'a [T],
- pub(super) chunk_size: usize,
+ v: &'a [T],
+ rem: &'a [T],
+ chunk_size: usize,
}
impl<'a, T> RChunksExact<'a, T> {
+ pub(super) fn new(slice: &'a [T], rem: &'a [T], size: usize) -> Self {
+ Self { v: slice, rem, chunk_size: size }
+ }
+
/// Returns the remainder of the original slice that is not going to be
/// returned by the iterator. The returned slice has at most `chunk_size-1`
/// elements.
#[derive(Debug)]
#[stable(feature = "rchunks", since = "1.31.0")]
pub struct RChunksExactMut<'a, T: 'a> {
- pub(super) v: &'a mut [T],
- pub(super) rem: &'a mut [T],
- pub(super) chunk_size: usize,
+ v: &'a mut [T],
+ rem: &'a mut [T],
+ chunk_size: usize,
}
impl<'a, T> RChunksExactMut<'a, T> {
+ pub(super) fn new(slice: &'a mut [T], rem: &'a mut [T], size: usize) -> Self {
+ Self { v: slice, rem, chunk_size: size }
+ }
+
/// Returns the remainder of the original slice that is not going to be
/// returned by the iterator. The returned slice has at most `chunk_size-1`
/// elements.
use crate::cmp::Ordering::{self, Equal, Greater, Less};
use crate::intrinsics::assume;
-use crate::marker::{self, Copy};
+use crate::marker::Copy;
use crate::mem;
use crate::ops::{FnMut, Range, RangeBounds};
use crate::option::Option;
mod rotate;
mod sort;
-use iter::GenericSplitN;
-
#[stable(feature = "rust1", since = "1.0.0")]
pub use iter::{Chunks, ChunksMut, Windows};
#[stable(feature = "rust1", since = "1.0.0")]
ptr.add(self.len())
};
- Iter { ptr: NonNull::new_unchecked(ptr as *mut T), end, _marker: marker::PhantomData }
+ Iter::new(NonNull::new_unchecked(ptr as *mut T), end)
}
}
ptr.add(self.len())
};
- IterMut { ptr: NonNull::new_unchecked(ptr), end, _marker: marker::PhantomData }
+ IterMut::new(NonNull::new_unchecked(ptr), end)
}
}
#[inline]
pub fn windows(&self, size: usize) -> Windows<'_, T> {
assert_ne!(size, 0);
- Windows { v: self, size }
+ Windows::new(self, size)
}
/// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
#[inline]
pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
assert_ne!(chunk_size, 0);
- Chunks { v: self, chunk_size }
+ Chunks::new(self, chunk_size)
}
/// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
#[inline]
pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
assert_ne!(chunk_size, 0);
- ChunksMut { v: self, chunk_size }
+ ChunksMut::new(self, chunk_size)
}
/// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
let fst_len = self.len() - rem;
// SAFETY: 0 <= fst_len <= self.len() by construction above
let (fst, snd) = unsafe { self.split_at_unchecked(fst_len) };
- ChunksExact { v: fst, rem: snd, chunk_size }
+ ChunksExact::new(fst, snd, chunk_size)
}
/// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
let fst_len = self.len() - rem;
// SAFETY: 0 <= fst_len <= self.len() by construction above
let (fst, snd) = unsafe { self.split_at_mut_unchecked(fst_len) };
- ChunksExactMut { v: fst, rem: snd, chunk_size }
+ ChunksExactMut::new(fst, snd, chunk_size)
}
/// Returns an iterator over `N` elements of the slice at a time, starting at the
// SAFETY: We cast a slice of `len * N` elements into
// a slice of `len` many `N` elements chunks.
let array_slice: &[[T; N]] = unsafe { from_raw_parts(fst.as_ptr().cast(), len) };
- ArrayChunks { iter: array_slice.iter(), rem: snd }
+ ArrayChunks::new(array_slice.iter(), snd)
}
/// Returns an iterator over `N` elements of the slice at a time, starting at the
// a slice of `len` many `N` elements chunks.
unsafe {
let array_slice: &mut [[T; N]] = from_raw_parts_mut(fst.as_mut_ptr().cast(), len);
- ArrayChunksMut { iter: array_slice.iter_mut(), rem: snd }
+ ArrayChunksMut::new(array_slice.iter_mut(), snd)
}
}
assert_ne!(N, 0);
let num_windows = self.len().saturating_sub(N - 1);
- ArrayWindows { slice_head: self.as_ptr(), num: num_windows, marker: marker::PhantomData }
+ ArrayWindows::new(self.as_ptr(), num_windows)
}
/// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
#[inline]
pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
assert!(chunk_size != 0);
- RChunks { v: self, chunk_size }
+ RChunks::new(self, chunk_size)
}
/// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
#[inline]
pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
assert!(chunk_size != 0);
- RChunksMut { v: self, chunk_size }
+ RChunksMut::new(self, chunk_size)
}
/// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
let rem = self.len() % chunk_size;
// SAFETY: 0 <= rem <= self.len() by construction above
let (fst, snd) = unsafe { self.split_at_unchecked(rem) };
- RChunksExact { v: snd, rem: fst, chunk_size }
+ RChunksExact::new(snd, fst, chunk_size)
}
/// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
let rem = self.len() % chunk_size;
// SAFETY: 0 <= rem <= self.len() by construction above
let (fst, snd) = unsafe { self.split_at_mut_unchecked(rem) };
- RChunksExactMut { v: snd, rem: fst, chunk_size }
+ RChunksExactMut::new(snd, fst, chunk_size)
}
/// Divides one slice into two at an index.
where
F: FnMut(&T) -> bool,
{
- Split { v: self, pred, finished: false }
+ Split::new(self, pred, false)
}
/// Returns an iterator over mutable subslices separated by elements that
where
F: FnMut(&T) -> bool,
{
- SplitMut { v: self, pred, finished: false }
+ SplitMut::new(self, pred, false)
}
/// Returns an iterator over subslices separated by elements that match
where
F: FnMut(&T) -> bool,
{
- SplitInclusive { v: self, pred, finished: false }
+ SplitInclusive::new(self, pred, false)
}
/// Returns an iterator over mutable subslices separated by elements that
where
F: FnMut(&T) -> bool,
{
- SplitInclusiveMut { v: self, pred, finished: false }
+ SplitInclusiveMut::new(self, pred, false)
}
/// Returns an iterator over subslices separated by elements that match
where
F: FnMut(&T) -> bool,
{
- RSplit { inner: self.split(pred) }
+ RSplit::new(self, pred, false)
}
/// Returns an iterator over mutable subslices separated by elements that
where
F: FnMut(&T) -> bool,
{
- RSplitMut { inner: self.split_mut(pred) }
+ RSplitMut::new(self, pred, false)
}
/// Returns an iterator over subslices separated by elements that match
where
F: FnMut(&T) -> bool,
{
- SplitN { inner: GenericSplitN { iter: self.split(pred), count: n } }
+ SplitN::new(self.split(pred), n)
}
/// Returns an iterator over subslices separated by elements that match
where
F: FnMut(&T) -> bool,
{
- SplitNMut { inner: GenericSplitN { iter: self.split_mut(pred), count: n } }
+ SplitNMut::new(self.split_mut(pred), n)
}
/// Returns an iterator over subslices separated by elements that match
where
F: FnMut(&T) -> bool,
{
- RSplitN { inner: GenericSplitN { iter: self.rsplit(pred), count: n } }
+ RSplitN::new(self.rsplit(pred), n)
}
/// Returns an iterator over subslices separated by elements that match
where
F: FnMut(&T) -> bool,
{
- RSplitNMut { inner: GenericSplitN { iter: self.rsplit_mut(pred), count: n } }
+ RSplitNMut::new(self.rsplit_mut(pred), n)
}
/// Returns `true` if the slice contains an element with the given value.