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 AsRef, as_slice, as_str to more things #65901

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ __pycache__/
/src/libcore/unicode/SpecialCasing.txt
/src/libcore/unicode/UnicodeData.txt
/src/libcore/unicode/downloaded
/target/
target/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; I should have put it in a separate commit. RLS puts the target/ directory in src/libcore for me.

# Generated by compiletest for incremental:
/tmp/
tags
Expand Down
71 changes: 70 additions & 1 deletion src/libcore/iter/adapters/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -234,6 +235,29 @@ unsafe impl<'a, I, T: 'a> TrustedLen for Copied<I>
T: Copy
{}

#[stable(feature = "slice_iter_adapter_as_ref", since = "1.41.0")]
impl<'a, T> AsRef<[T]> for Copied<slice::Iter<'a, T>> {
fn as_ref(&self) -> &[T] {
self.it.as_ref()
}
}
impl<'a, T> Copied<slice::Iter<'a, T>> {
/// 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<slice::IterMut<'a, T>> {
/// 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
Expand Down Expand Up @@ -357,6 +381,29 @@ unsafe impl<'a, I, T: 'a> TrustedLen for Cloned<I>
T: Clone
{}

#[stable(feature = "slice_iter_adapter_as_ref", since = "1.41.0")]
impl<'a, T> AsRef<[T]> for Cloned<slice::Iter<'a, T>> {
fn as_ref(&self) -> &[T] {
self.it.as_ref()
}
}
impl<'a, T> Cloned<slice::Iter<'a, T>> {
/// 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<slice::IterMut<'a, T>> {
/// 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
Expand Down Expand Up @@ -1222,6 +1269,28 @@ unsafe impl<I> TrustedLen for Enumerate<I>
where I: TrustedLen,
{}

#[stable(feature = "slice_iter_adapter_as_ref", since = "1.41.0")]
impl<'a, T> AsRef<[T]> for Enumerate<slice::Iter<'a, T>> {
fn as_ref(&self) -> &[T] {
self.iter.as_ref()
}
}
impl<'a, T> Enumerate<slice::Iter<'a, T>> {
/// 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<slice::IterMut<'a, T>> {
/// 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.
Expand Down
45 changes: 45 additions & 0 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,19 @@ impl<'a> Chars<'a> {
}
}

#[stable(feature = "chars_as_ref", since = "1.41.0")]
impl<'a> AsRef<str> 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
Expand Down Expand Up @@ -728,6 +741,19 @@ impl<'a> CharIndices<'a> {
}
}

#[stable(feature = "chars_as_ref", since = "1.41.0")]
impl<'a> AsRef<str> 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`].
Expand All @@ -739,6 +765,18 @@ impl<'a> CharIndices<'a> {
#[derive(Clone, Debug)]
pub struct Bytes<'a>(Cloned<slice::Iter<'a, u8>>);

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;
Expand Down Expand Up @@ -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 {
Expand Down