diff --git a/.gitignore b/.gitignore index 487867c375d45..20a5879a9c4dc 100644 --- a/.gitignore +++ b/.gitignore @@ -42,7 +42,7 @@ __pycache__/ /src/libcore/unicode/SpecialCasing.txt /src/libcore/unicode/UnicodeData.txt /src/libcore/unicode/downloaded -/target/ +target/ # Generated by compiletest for incremental: /tmp/ tags diff --git a/src/libcore/iter/adapters/mod.rs b/src/libcore/iter/adapters/mod.rs index 3b8edc2ad6177..6fb440b6e254b 100644 --- a/src/libcore/iter/adapters/mod.rs +++ b/src/libcore/iter/adapters/mod.rs @@ -1,8 +1,9 @@ use crate::cmp; use crate::fmt; +use crate::intrinsics; use crate::ops::{Add, AddAssign, Try}; +use crate::slice; use crate::usize; -use crate::intrinsics; use super::{Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, TrustedLen}; use super::{LoopState, from_fn}; @@ -234,6 +235,29 @@ unsafe impl<'a, I, T: 'a> TrustedLen for Copied T: Copy {} +#[stable(feature = "slice_iter_adapter_as_ref", since = "1.41.0")] +impl<'a, T> AsRef<[T]> for Copied> { + fn as_ref(&self) -> &[T] { + self.it.as_ref() + } +} +impl<'a, T> Copied> { + /// This simply calls the parent method. For more information, see + /// [`Iter::as_slice`][slice::Iter::as_slice]. + #[unstable(feature = "slice_iter_adapter_as_slice", issue = "0")] + pub fn as_slice(&self) -> &'a [T] { + self.it.as_slice() + } +} +impl<'a, T> Copied> { + /// This simply calls the parent method. For more information, see + /// [`IterMut::as_slice`][slice::IterMut::as_slice]. + #[unstable(feature = "slice_iter_adapter_as_slice", issue = "0")] + pub fn as_slice(&self) -> &[T] { + self.it.as_slice() + } +} + /// An iterator that clones the elements of an underlying iterator. /// /// This `struct` is created by the [`cloned`] method on [`Iterator`]. See its @@ -357,6 +381,29 @@ unsafe impl<'a, I, T: 'a> TrustedLen for Cloned T: Clone {} +#[stable(feature = "slice_iter_adapter_as_ref", since = "1.41.0")] +impl<'a, T> AsRef<[T]> for Cloned> { + fn as_ref(&self) -> &[T] { + self.it.as_ref() + } +} +impl<'a, T> Cloned> { + /// This simply calls the parent method. For more information, see + /// [`Iter::as_slice`][slice::Iter::as_slice]. + #[unstable(feature = "slice_iter_adapter_as_slice", issue = "0")] + pub fn as_slice(&self) -> &'a [T] { + self.it.as_slice() + } +} +impl<'a, T> Cloned> { + /// This simply calls the parent method. For more information, see + /// [`IterMut::as_slice`][slice::IterMut::as_slice]. + #[unstable(feature = "slice_iter_adapter_as_slice", issue = "0")] + pub fn as_slice(&self) -> &[T] { + self.it.as_slice() + } +} + /// An iterator that repeats endlessly. /// /// This `struct` is created by the [`cycle`] method on [`Iterator`]. See its @@ -1222,6 +1269,28 @@ unsafe impl TrustedLen for Enumerate where I: TrustedLen, {} +#[stable(feature = "slice_iter_adapter_as_ref", since = "1.41.0")] +impl<'a, T> AsRef<[T]> for Enumerate> { + fn as_ref(&self) -> &[T] { + self.iter.as_ref() + } +} +impl<'a, T> Enumerate> { + /// This simply calls the parent method. For more information, see + /// [`Iter::as_slice`][slice::Iter::as_slice]. + #[unstable(feature = "slice_iter_adapter_as_slice", issue = "0")] + pub fn as_slice(&self) -> &'a [T] { + self.iter.as_slice() + } +} +impl<'a, T> Enumerate> { + /// This simply calls the parent method. For more information, see + /// [`IterMut::as_slice`][slice::IterMut::as_slice]. + #[unstable(feature = "slice_iter_adapter_as_slice", issue = "0")] + pub fn as_slice(&self) -> &[T] { + self.iter.as_slice() + } +} /// An iterator with a `peek()` that returns an optional reference to the next /// element. diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index f67012d8f2fce..c2e338cb55a6f 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -651,6 +651,19 @@ impl<'a> Chars<'a> { } } +#[stable(feature = "chars_as_ref", since = "1.41.0")] +impl<'a> AsRef for Chars<'a> { + fn as_ref(&self) -> &str { + self.as_str() + } +} +#[stable(feature = "chars_as_ref", since = "1.41.0")] +impl<'a> AsRef<[u8]> for Chars<'a> { + fn as_ref(&self) -> &[u8] { + self.as_str().as_bytes() + } +} + /// An iterator over the [`char`]s of a string slice, and their positions. /// /// [`char`]: ../../std/primitive.char.html @@ -728,6 +741,19 @@ impl<'a> CharIndices<'a> { } } +#[stable(feature = "chars_as_ref", since = "1.41.0")] +impl<'a> AsRef for CharIndices<'a> { + fn as_ref(&self) -> &str { + self.as_str() + } +} +#[stable(feature = "chars_as_ref", since = "1.41.0")] +impl<'a> AsRef<[u8]> for CharIndices<'a> { + fn as_ref(&self) -> &[u8] { + self.as_str().as_bytes() + } +} + /// An iterator over the bytes of a string slice. /// /// This struct is created by the [`bytes`] method on [`str`]. @@ -739,6 +765,18 @@ impl<'a> CharIndices<'a> { #[derive(Clone, Debug)] pub struct Bytes<'a>(Cloned>); +impl<'a> Bytes<'a> { + /// Views the underlying data as a subslice of the original data. + /// + /// This has the same lifetime as the original slice, and so the + /// iterator can continue to be used while this exists. + #[unstable(feature = "bytes_as_bytes", issue = "0")] + #[inline] + pub fn as_bytes(&self) -> &'a [u8] { + self.0.as_slice() + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Iterator for Bytes<'_> { type Item = u8; @@ -847,6 +885,13 @@ unsafe impl TrustedRandomAccess for Bytes<'_> { fn may_have_side_effect() -> bool { false } } +#[stable(feature = "bytes_as_ref", since = "1.41.0")] +impl<'a> AsRef<[u8]> for Bytes<'a> { + fn as_ref(&self) -> &[u8] { + self.0.as_ref() + } +} + /// This macro generates a Clone impl for string pattern API /// wrapper types of the form X<'a, P> macro_rules! derive_pattern_clone {