Skip to content

Commit aee62d2

Browse files
committed
have String use SliceIndex impls from str
1 parent 5d3d347 commit aee62d2

File tree

2 files changed

+15
-88
lines changed

2 files changed

+15
-88
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
#![feature(sized_type_properties)]
151151
#![feature(slice_from_ptr_range)]
152152
#![feature(slice_group_by)]
153+
#![feature(slice_index_methods)]
153154
#![feature(slice_ptr_get)]
154155
#![feature(slice_ptr_len)]
155156
#![feature(slice_range)]

library/alloc/src/string.rs

+14-88
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use core::ops::Add;
5454
use core::ops::AddAssign;
5555
#[cfg(not(no_global_oom_handling))]
5656
use core::ops::Bound::{Excluded, Included, Unbounded};
57-
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
57+
use core::ops::{self, Range, RangeBounds};
5858
use core::ptr;
5959
use core::slice;
6060
use core::str::pattern::Pattern;
@@ -2429,100 +2429,26 @@ impl AddAssign<&str> for String {
24292429
}
24302430

24312431
#[stable(feature = "rust1", since = "1.0.0")]
2432-
impl ops::Index<ops::Range<usize>> for String {
2433-
type Output = str;
2432+
impl<I> ops::Index<I> for String
2433+
where
2434+
I: slice::SliceIndex<str>,
2435+
{
2436+
type Output = I::Output;
24342437

24352438
#[inline]
2436-
fn index(&self, index: ops::Range<usize>) -> &str {
2437-
&self[..][index]
2439+
fn index(&self, index: I) -> &I::Output {
2440+
index.index(self.as_str())
24382441
}
24392442
}
2440-
#[stable(feature = "rust1", since = "1.0.0")]
2441-
impl ops::Index<ops::RangeTo<usize>> for String {
2442-
type Output = str;
24432443

2444-
#[inline]
2445-
fn index(&self, index: ops::RangeTo<usize>) -> &str {
2446-
&self[..][index]
2447-
}
2448-
}
24492444
#[stable(feature = "rust1", since = "1.0.0")]
2450-
impl ops::Index<ops::RangeFrom<usize>> for String {
2451-
type Output = str;
2452-
2453-
#[inline]
2454-
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
2455-
&self[..][index]
2456-
}
2457-
}
2458-
#[stable(feature = "rust1", since = "1.0.0")]
2459-
impl ops::Index<ops::RangeFull> for String {
2460-
type Output = str;
2461-
2462-
#[inline]
2463-
fn index(&self, _index: ops::RangeFull) -> &str {
2464-
unsafe { str::from_utf8_unchecked(&self.vec) }
2465-
}
2466-
}
2467-
#[stable(feature = "inclusive_range", since = "1.26.0")]
2468-
impl ops::Index<ops::RangeInclusive<usize>> for String {
2469-
type Output = str;
2470-
2471-
#[inline]
2472-
fn index(&self, index: ops::RangeInclusive<usize>) -> &str {
2473-
Index::index(&**self, index)
2474-
}
2475-
}
2476-
#[stable(feature = "inclusive_range", since = "1.26.0")]
2477-
impl ops::Index<ops::RangeToInclusive<usize>> for String {
2478-
type Output = str;
2479-
2480-
#[inline]
2481-
fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
2482-
Index::index(&**self, index)
2483-
}
2484-
}
2485-
2486-
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
2487-
impl ops::IndexMut<ops::Range<usize>> for String {
2488-
#[inline]
2489-
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
2490-
&mut self[..][index]
2491-
}
2492-
}
2493-
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
2494-
impl ops::IndexMut<ops::RangeTo<usize>> for String {
2495-
#[inline]
2496-
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
2497-
&mut self[..][index]
2498-
}
2499-
}
2500-
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
2501-
impl ops::IndexMut<ops::RangeFrom<usize>> for String {
2502-
#[inline]
2503-
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
2504-
&mut self[..][index]
2505-
}
2506-
}
2507-
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
2508-
impl ops::IndexMut<ops::RangeFull> for String {
2509-
#[inline]
2510-
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
2511-
unsafe { str::from_utf8_unchecked_mut(&mut *self.vec) }
2512-
}
2513-
}
2514-
#[stable(feature = "inclusive_range", since = "1.26.0")]
2515-
impl ops::IndexMut<ops::RangeInclusive<usize>> for String {
2516-
#[inline]
2517-
fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str {
2518-
IndexMut::index_mut(&mut **self, index)
2519-
}
2520-
}
2521-
#[stable(feature = "inclusive_range", since = "1.26.0")]
2522-
impl ops::IndexMut<ops::RangeToInclusive<usize>> for String {
2445+
impl<I> ops::IndexMut<I> for String
2446+
where
2447+
I: slice::SliceIndex<str>,
2448+
{
25232449
#[inline]
2524-
fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
2525-
IndexMut::index_mut(&mut **self, index)
2450+
fn index_mut(&mut self, index: I) -> &mut I::Output {
2451+
index.index_mut(self.as_mut_str())
25262452
}
25272453
}
25282454

0 commit comments

Comments
 (0)