mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
Rollup merge of #120291 - pitaj:string-sliceindex, r=Amanieu
Have `String` use `SliceIndex` impls from `str` This PR simplifies the implementation of `Index` and `IndexMut` on `String`, and in the process enables indexing `String` by any user types that implement `SliceIndex<str>`. Similar to #47832 r? libs Not sure if this warrants a crater run.
This commit is contained in:
commit
6d865038a5
@ -152,6 +152,7 @@
|
||||
#![feature(set_ptr_value)]
|
||||
#![feature(sized_type_properties)]
|
||||
#![feature(slice_from_ptr_range)]
|
||||
#![feature(slice_index_methods)]
|
||||
#![feature(slice_ptr_get)]
|
||||
#![feature(slice_ptr_len)]
|
||||
#![feature(slice_range)]
|
||||
|
@ -54,7 +54,7 @@ use core::ops::Add;
|
||||
use core::ops::AddAssign;
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
use core::ops::Bound::{Excluded, Included, Unbounded};
|
||||
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
|
||||
use core::ops::{self, Range, RangeBounds};
|
||||
use core::ptr;
|
||||
use core::slice;
|
||||
use core::str::pattern::Pattern;
|
||||
@ -2433,100 +2433,26 @@ impl AddAssign<&str> for String {
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl ops::Index<ops::Range<usize>> for String {
|
||||
type Output = str;
|
||||
impl<I> ops::Index<I> for String
|
||||
where
|
||||
I: slice::SliceIndex<str>,
|
||||
{
|
||||
type Output = I::Output;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: ops::Range<usize>) -> &str {
|
||||
&self[..][index]
|
||||
fn index(&self, index: I) -> &I::Output {
|
||||
index.index(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl ops::Index<ops::RangeTo<usize>> for String {
|
||||
type Output = str;
|
||||
|
||||
impl<I> ops::IndexMut<I> for String
|
||||
where
|
||||
I: slice::SliceIndex<str>,
|
||||
{
|
||||
#[inline]
|
||||
fn index(&self, index: ops::RangeTo<usize>) -> &str {
|
||||
&self[..][index]
|
||||
}
|
||||
}
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl ops::Index<ops::RangeFrom<usize>> for String {
|
||||
type Output = str;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
|
||||
&self[..][index]
|
||||
}
|
||||
}
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl ops::Index<ops::RangeFull> for String {
|
||||
type Output = str;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, _index: ops::RangeFull) -> &str {
|
||||
unsafe { str::from_utf8_unchecked(&self.vec) }
|
||||
}
|
||||
}
|
||||
#[stable(feature = "inclusive_range", since = "1.26.0")]
|
||||
impl ops::Index<ops::RangeInclusive<usize>> for String {
|
||||
type Output = str;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: ops::RangeInclusive<usize>) -> &str {
|
||||
Index::index(&**self, index)
|
||||
}
|
||||
}
|
||||
#[stable(feature = "inclusive_range", since = "1.26.0")]
|
||||
impl ops::Index<ops::RangeToInclusive<usize>> for String {
|
||||
type Output = str;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
|
||||
Index::index(&**self, index)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
|
||||
impl ops::IndexMut<ops::Range<usize>> for String {
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
|
||||
&mut self[..][index]
|
||||
}
|
||||
}
|
||||
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
|
||||
impl ops::IndexMut<ops::RangeTo<usize>> for String {
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
|
||||
&mut self[..][index]
|
||||
}
|
||||
}
|
||||
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
|
||||
impl ops::IndexMut<ops::RangeFrom<usize>> for String {
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
|
||||
&mut self[..][index]
|
||||
}
|
||||
}
|
||||
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
|
||||
impl ops::IndexMut<ops::RangeFull> for String {
|
||||
#[inline]
|
||||
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
|
||||
unsafe { str::from_utf8_unchecked_mut(&mut *self.vec) }
|
||||
}
|
||||
}
|
||||
#[stable(feature = "inclusive_range", since = "1.26.0")]
|
||||
impl ops::IndexMut<ops::RangeInclusive<usize>> for String {
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str {
|
||||
IndexMut::index_mut(&mut **self, index)
|
||||
}
|
||||
}
|
||||
#[stable(feature = "inclusive_range", since = "1.26.0")]
|
||||
impl ops::IndexMut<ops::RangeToInclusive<usize>> for String {
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
|
||||
IndexMut::index_mut(&mut **self, index)
|
||||
fn index_mut(&mut self, index: I) -> &mut I::Output {
|
||||
index.index_mut(self.as_mut_str())
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user