+// ignore-tidy-filelength
+
//! Slice management and manipulation.
//!
//! For more details see [`std::slice`].
/// The caller must ensure that the slice outlives the pointer this
/// function returns, or else it will end up pointing to garbage.
///
+ /// The caller must also ensure that the memory the pointer (non-transitively) points to
+ /// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
+ /// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
+ ///
/// Modifying the container referenced by this slice may cause its buffer
/// to be reallocated, which would also make any pointers to it invalid.
///
/// }
/// }
/// ```
+ ///
+ /// [`as_mut_ptr`]: #method.as_mut_ptr
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub const fn as_ptr(&self) -> *const T {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn iter(&self) -> Iter<T> {
+ pub fn iter(&self) -> Iter<'_, T> {
unsafe {
let ptr = self.as_ptr();
assume(!ptr.is_null());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn iter_mut(&mut self) -> IterMut<T> {
+ pub fn iter_mut(&mut self) -> IterMut<'_, T> {
unsafe {
let ptr = self.as_mut_ptr();
assume(!ptr.is_null());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn windows(&self, size: usize) -> Windows<T> {
+ pub fn windows(&self, size: usize) -> Windows<'_, T> {
assert!(size != 0);
Windows { v: self, size }
}
/// [`rchunks`]: #method.rchunks
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn chunks(&self, chunk_size: usize) -> Chunks<T> {
+ pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
assert!(chunk_size != 0);
Chunks { v: self, chunk_size }
}
/// [`rchunks_mut`]: #method.rchunks_mut
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T> {
+ pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
assert!(chunk_size != 0);
ChunksMut { v: self, chunk_size }
}
/// [`rchunks_exact`]: #method.rchunks_exact
#[stable(feature = "chunks_exact", since = "1.31.0")]
#[inline]
- pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<T> {
+ pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
assert!(chunk_size != 0);
let rem = self.len() % chunk_size;
let len = self.len() - rem;
/// [`rchunks_exact_mut`]: #method.rchunks_exact_mut
#[stable(feature = "chunks_exact", since = "1.31.0")]
#[inline]
- pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<T> {
+ pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
assert!(chunk_size != 0);
let rem = self.len() % chunk_size;
let len = self.len() - rem;
/// [`chunks`]: #method.chunks
#[stable(feature = "rchunks", since = "1.31.0")]
#[inline]
- pub fn rchunks(&self, chunk_size: usize) -> RChunks<T> {
+ pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
assert!(chunk_size != 0);
RChunks { v: self, chunk_size }
}
/// [`chunks_mut`]: #method.chunks_mut
#[stable(feature = "rchunks", since = "1.31.0")]
#[inline]
- pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<T> {
+ pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
assert!(chunk_size != 0);
RChunksMut { v: self, chunk_size }
}
/// [`chunks_exact`]: #method.chunks_exact
#[stable(feature = "rchunks", since = "1.31.0")]
#[inline]
- pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<T> {
+ pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
assert!(chunk_size != 0);
let rem = self.len() % chunk_size;
let (fst, snd) = self.split_at(rem);
/// [`chunks_exact_mut`]: #method.chunks_exact_mut
#[stable(feature = "rchunks", since = "1.31.0")]
#[inline]
- pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<T> {
+ pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
assert!(chunk_size != 0);
let rem = self.len() % chunk_size;
let (fst, snd) = self.split_at_mut(rem);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn split<F>(&self, pred: F) -> Split<T, F>
+ pub fn split<F>(&self, pred: F) -> Split<'_, T, F>
where F: FnMut(&T) -> bool
{
Split {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn split_mut<F>(&mut self, pred: F) -> SplitMut<T, F>
+ pub fn split_mut<F>(&mut self, pred: F) -> SplitMut<'_, T, F>
where F: FnMut(&T) -> bool
{
SplitMut { v: self, pred, finished: false }
/// ```
#[stable(feature = "slice_rsplit", since = "1.27.0")]
#[inline]
- pub fn rsplit<F>(&self, pred: F) -> RSplit<T, F>
+ pub fn rsplit<F>(&self, pred: F) -> RSplit<'_, T, F>
where F: FnMut(&T) -> bool
{
RSplit { inner: self.split(pred) }
///
#[stable(feature = "slice_rsplit", since = "1.27.0")]
#[inline]
- pub fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<T, F>
+ pub fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<'_, T, F>
where F: FnMut(&T) -> bool
{
RSplitMut { inner: self.split_mut(pred) }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<T, F>
+ pub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<'_, T, F>
where F: FnMut(&T) -> bool
{
SplitN {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<T, F>
+ pub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<'_, T, F>
where F: FnMut(&T) -> bool
{
SplitNMut {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<T, F>
+ pub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<'_, T, F>
where F: FnMut(&T) -> bool
{
RSplitN {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<T, F>
+ pub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<'_, T, F>
where F: FnMut(&T) -> bool
{
RSplitNMut {
#[stable(feature = "core_impl_debug", since = "1.9.0")]
impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Iter")
.field(&self.as_slice())
.finish()
#[stable(feature = "core_impl_debug", since = "1.9.0")]
impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("IterMut")
.field(&self.make_slice())
.finish()
#[stable(feature = "core_impl_debug", since = "1.9.0")]
impl<T: fmt::Debug, P> fmt::Debug for Split<'_, T, P> where P: FnMut(&T) -> bool {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Split")
.field("v", &self.v)
.field("finished", &self.finished)
(1, Some(self.v.len() + 1))
}
}
+
+ #[inline]
+ fn last(mut self) -> Option<Self::Item> {
+ self.next_back()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "core_impl_debug", since = "1.9.0")]
impl<T: fmt::Debug, P> fmt::Debug for SplitMut<'_, T, P> where P: FnMut(&T) -> bool {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SplitMut")
.field("v", &self.v)
.field("finished", &self.finished)
(1, Some(self.v.len() + 1))
}
}
+
+ #[inline]
+ fn last(mut self) -> Option<Self::Item> {
+ self.next_back()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "slice_rsplit", since = "1.27.0")]
impl<T: fmt::Debug, P> fmt::Debug for RSplit<'_, T, P> where P: FnMut(&T) -> bool {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RSplit")
.field("v", &self.inner.v)
.field("finished", &self.inner.finished)
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
+
+ #[inline]
+ fn last(mut self) -> Option<Self::Item> {
+ self.next_back()
+ }
}
#[stable(feature = "slice_rsplit", since = "1.27.0")]
#[stable(feature = "slice_rsplit", since = "1.27.0")]
impl<T: fmt::Debug, P> fmt::Debug for RSplitMut<'_, T, P> where P: FnMut(&T) -> bool {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RSplitMut")
.field("v", &self.inner.v)
.field("finished", &self.inner.finished)
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
+
+ #[inline]
+ fn last(mut self) -> Option<Self::Item> {
+ self.next_back()
+ }
}
#[stable(feature = "slice_rsplit", since = "1.27.0")]
#[stable(feature = "core_impl_debug", since = "1.9.0")]
impl<T: fmt::Debug, P> fmt::Debug for SplitN<'_, T, P> where P: FnMut(&T) -> bool {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SplitN")
.field("inner", &self.inner)
.finish()
#[stable(feature = "core_impl_debug", since = "1.9.0")]
impl<T: fmt::Debug, P> fmt::Debug for RSplitN<'_, T, P> where P: FnMut(&T) -> bool {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RSplitN")
.field("inner", &self.inner)
.finish()
#[stable(feature = "core_impl_debug", since = "1.9.0")]
impl<T: fmt::Debug, P> fmt::Debug for SplitNMut<'_, T, P> where P: FnMut(&T) -> bool {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SplitNMut")
.field("inner", &self.inner)
.finish()
#[stable(feature = "core_impl_debug", since = "1.9.0")]
impl<T: fmt::Debug, P> fmt::Debug for RSplitNMut<'_, T, P> where P: FnMut(&T) -> bool {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RSplitNMut")
.field("inner", &self.inner)
.finish()
Some(snd)
}
}
+
+ #[inline]
+ fn nth_back(&mut self, n: usize) {
+ let (end, overflow) = self.v.len().overflowing_sub(n);
+ if end < self.v.len() || overflow {
+ self.v = &[];
+ None
+ } else {
+ let start = match end.checked_sub(self.chunk_size) {
+ Some(sum) => cmp::min(self.v.len(), sum),
+ None => self.v.len(),
+ };
+ let nth = &self.v[start..end];
+ self.v = &self.v[end..];
+ }
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]