Skip to content

Commit a9329d3

Browse files
committed
Auto merge of #40737 - nagisa:safe-slicing-strs, r=BurntSushi
Checked slicing for strings cc #39932
2 parents 474f7a9 + 53a3692 commit a9329d3

File tree

8 files changed

+448
-63
lines changed

8 files changed

+448
-63
lines changed

src/libcollections/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#![feature(unicode)]
6161
#![feature(unique)]
6262
#![feature(untagged_unions)]
63+
#![cfg_attr(not(test), feature(str_checked_slicing))]
6364
#![cfg_attr(test, feature(rand, test))]
6465

6566
#![no_std]

src/libcollections/slice.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl<T> [T] {
362362
#[stable(feature = "rust1", since = "1.0.0")]
363363
#[inline]
364364
pub fn get<I>(&self, index: I) -> Option<&I::Output>
365-
where I: SliceIndex<T>
365+
where I: SliceIndex<Self>
366366
{
367367
core_slice::SliceExt::get(self, index)
368368
}
@@ -385,7 +385,7 @@ impl<T> [T] {
385385
#[stable(feature = "rust1", since = "1.0.0")]
386386
#[inline]
387387
pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
388-
where I: SliceIndex<T>
388+
where I: SliceIndex<Self>
389389
{
390390
core_slice::SliceExt::get_mut(self, index)
391391
}
@@ -405,7 +405,7 @@ impl<T> [T] {
405405
#[stable(feature = "rust1", since = "1.0.0")]
406406
#[inline]
407407
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
408-
where I: SliceIndex<T>
408+
where I: SliceIndex<Self>
409409
{
410410
core_slice::SliceExt::get_unchecked(self, index)
411411
}
@@ -427,7 +427,7 @@ impl<T> [T] {
427427
#[stable(feature = "rust1", since = "1.0.0")]
428428
#[inline]
429429
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
430-
where I: SliceIndex<T>
430+
where I: SliceIndex<Self>
431431
{
432432
core_slice::SliceExt::get_unchecked_mut(self, index)
433433
}

src/libcollections/str.rs

+109-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use borrow::{Borrow, ToOwned};
5151
use string::String;
5252
use std_unicode;
5353
use vec::Vec;
54-
use slice::SliceConcatExt;
54+
use slice::{SliceConcatExt, SliceIndex};
5555
use boxed::Box;
5656

5757
#[stable(feature = "rust1", since = "1.0.0")]
@@ -310,6 +310,114 @@ impl str {
310310
core_str::StrExt::as_ptr(self)
311311
}
312312

313+
/// Returns a subslice of `str`.
314+
///
315+
/// This is the non-panicking alternative to indexing the `str`. Returns `None` whenever
316+
/// equivalent indexing operation would panic.
317+
///
318+
/// # Examples
319+
///
320+
/// ```
321+
/// # #![feature(str_checked_slicing)]
322+
/// let v = "🗻∈🌏";
323+
/// assert_eq!(Some("🗻"), v.get(0..4));
324+
/// assert!(v.get(1..).is_none());
325+
/// assert!(v.get(..8).is_none());
326+
/// assert!(v.get(..42).is_none());
327+
/// ```
328+
#[unstable(feature = "str_checked_slicing", issue = "39932")]
329+
#[inline]
330+
pub fn get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
331+
core_str::StrExt::get(self, i)
332+
}
333+
334+
/// Returns a mutable subslice of `str`.
335+
///
336+
/// This is the non-panicking alternative to indexing the `str`. Returns `None` whenever
337+
/// equivalent indexing operation would panic.
338+
///
339+
/// # Examples
340+
///
341+
/// ```
342+
/// # #![feature(str_checked_slicing)]
343+
/// let mut v = String::from("🗻∈🌏");
344+
/// assert_eq!(Some("🗻"), v.get_mut(0..4).map(|v| &*v));
345+
/// assert!(v.get_mut(1..).is_none());
346+
/// assert!(v.get_mut(..8).is_none());
347+
/// assert!(v.get_mut(..42).is_none());
348+
/// ```
349+
#[unstable(feature = "str_checked_slicing", issue = "39932")]
350+
#[inline]
351+
pub fn get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
352+
core_str::StrExt::get_mut(self, i)
353+
}
354+
355+
/// Returns a unchecked subslice of `str`.
356+
///
357+
/// This is the unchecked alternative to indexing the `str`.
358+
///
359+
/// # Safety
360+
///
361+
/// Callers of this function are responsible that these preconditions are
362+
/// satisfied:
363+
///
364+
/// * The starting index must come before the ending index;
365+
/// * Indexes must be within bounds of the original slice;
366+
/// * Indexes must lie on UTF-8 sequence boundaries.
367+
///
368+
/// Failing that, the returned string slice may reference invalid memory or
369+
/// violate the invariants communicated by the `str` type.
370+
///
371+
/// # Examples
372+
///
373+
/// ```
374+
/// # #![feature(str_checked_slicing)]
375+
/// let v = "🗻∈🌏";
376+
/// unsafe {
377+
/// assert_eq!("🗻", v.get_unchecked(0..4));
378+
/// assert_eq!("∈", v.get_unchecked(4..7));
379+
/// assert_eq!("🌏", v.get_unchecked(7..11));
380+
/// }
381+
/// ```
382+
#[unstable(feature = "str_checked_slicing", issue = "39932")]
383+
#[inline]
384+
pub unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output {
385+
core_str::StrExt::get_unchecked(self, i)
386+
}
387+
388+
/// Returns a mutable, unchecked subslice of `str`.
389+
///
390+
/// This is the unchecked alternative to indexing the `str`.
391+
///
392+
/// # Safety
393+
///
394+
/// Callers of this function are responsible that these preconditions are
395+
/// satisfied:
396+
///
397+
/// * The starting index must come before the ending index;
398+
/// * Indexes must be within bounds of the original slice;
399+
/// * Indexes must lie on UTF-8 sequence boundaries.
400+
///
401+
/// Failing that, the returned string slice may reference invalid memory or
402+
/// violate the invariants communicated by the `str` type.
403+
///
404+
/// # Examples
405+
///
406+
/// ```
407+
/// # #![feature(str_checked_slicing)]
408+
/// let mut v = String::from("🗻∈🌏");
409+
/// unsafe {
410+
/// assert_eq!("🗻", v.get_unchecked_mut(0..4));
411+
/// assert_eq!("∈", v.get_unchecked_mut(4..7));
412+
/// assert_eq!("🌏", v.get_unchecked_mut(7..11));
413+
/// }
414+
/// ```
415+
#[unstable(feature = "str_checked_slicing", issue = "39932")]
416+
#[inline]
417+
pub unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output {
418+
core_str::StrExt::get_unchecked_mut(self, i)
419+
}
420+
313421
/// Creates a string slice from another string slice, bypassing safety
314422
/// checks.
315423
///

src/libcore/slice/mod.rs

+24-28
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ pub trait SliceExt {
9797

9898
#[stable(feature = "core", since = "1.6.0")]
9999
fn get<I>(&self, index: I) -> Option<&I::Output>
100-
where I: SliceIndex<Self::Item>;
101-
100+
where I: SliceIndex<Self>;
102101
#[stable(feature = "core", since = "1.6.0")]
103102
fn first(&self) -> Option<&Self::Item>;
104103

@@ -113,8 +112,7 @@ pub trait SliceExt {
113112

114113
#[stable(feature = "core", since = "1.6.0")]
115114
unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
116-
where I: SliceIndex<Self::Item>;
117-
115+
where I: SliceIndex<Self>;
118116
#[stable(feature = "core", since = "1.6.0")]
119117
fn as_ptr(&self) -> *const Self::Item;
120118

@@ -141,8 +139,7 @@ pub trait SliceExt {
141139

142140
#[stable(feature = "core", since = "1.6.0")]
143141
fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
144-
where I: SliceIndex<Self::Item>;
145-
142+
where I: SliceIndex<Self>;
146143
#[stable(feature = "core", since = "1.6.0")]
147144
fn iter_mut(&mut self) -> IterMut<Self::Item>;
148145

@@ -184,8 +181,7 @@ pub trait SliceExt {
184181

185182
#[stable(feature = "core", since = "1.6.0")]
186183
unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
187-
where I: SliceIndex<Self::Item>;
188-
184+
where I: SliceIndex<Self>;
189185
#[stable(feature = "core", since = "1.6.0")]
190186
fn as_mut_ptr(&mut self) -> *mut Self::Item;
191187

@@ -337,7 +333,7 @@ impl<T> SliceExt for [T] {
337333

338334
#[inline]
339335
fn get<I>(&self, index: I) -> Option<&I::Output>
340-
where I: SliceIndex<T>
336+
where I: SliceIndex<[T]>
341337
{
342338
index.get(self)
343339
}
@@ -365,7 +361,7 @@ impl<T> SliceExt for [T] {
365361

366362
#[inline]
367363
unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
368-
where I: SliceIndex<T>
364+
where I: SliceIndex<[T]>
369365
{
370366
index.get_unchecked(self)
371367
}
@@ -406,7 +402,7 @@ impl<T> SliceExt for [T] {
406402

407403
#[inline]
408404
fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
409-
where I: SliceIndex<T>
405+
where I: SliceIndex<[T]>
410406
{
411407
index.get_mut(self)
412408
}
@@ -538,7 +534,7 @@ impl<T> SliceExt for [T] {
538534

539535
#[inline]
540536
unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
541-
where I: SliceIndex<T>
537+
where I: SliceIndex<[T]>
542538
{
543539
index.get_unchecked_mut(self)
544540
}
@@ -631,7 +627,7 @@ impl<T> SliceExt for [T] {
631627
#[stable(feature = "rust1", since = "1.0.0")]
632628
#[rustc_on_unimplemented = "slice indices are of type `usize` or ranges of `usize`"]
633629
impl<T, I> ops::Index<I> for [T]
634-
where I: SliceIndex<T>
630+
where I: SliceIndex<[T]>
635631
{
636632
type Output = I::Output;
637633

@@ -644,7 +640,7 @@ impl<T, I> ops::Index<I> for [T]
644640
#[stable(feature = "rust1", since = "1.0.0")]
645641
#[rustc_on_unimplemented = "slice indices are of type `usize` or ranges of `usize`"]
646642
impl<T, I> ops::IndexMut<I> for [T]
647-
where I: SliceIndex<T>
643+
where I: SliceIndex<[T]>
648644
{
649645
#[inline]
650646
fn index_mut(&mut self, index: I) -> &mut I::Output {
@@ -667,37 +663,37 @@ fn slice_index_order_fail(index: usize, end: usize) -> ! {
667663
/// A helper trait used for indexing operations.
668664
#[unstable(feature = "slice_get_slice", issue = "35729")]
669665
#[rustc_on_unimplemented = "slice indices are of type `usize` or ranges of `usize`"]
670-
pub trait SliceIndex<T> {
666+
pub trait SliceIndex<T: ?Sized> {
671667
/// The output type returned by methods.
672668
type Output: ?Sized;
673669

674670
/// Returns a shared reference to the output at this location, if in
675671
/// bounds.
676-
fn get(self, slice: &[T]) -> Option<&Self::Output>;
672+
fn get(self, slice: &T) -> Option<&Self::Output>;
677673

678674
/// Returns a mutable reference to the output at this location, if in
679675
/// bounds.
680-
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>;
676+
fn get_mut(self, slice: &mut T) -> Option<&mut Self::Output>;
681677

682678
/// Returns a shared reference to the output at this location, without
683679
/// performing any bounds checking.
684-
unsafe fn get_unchecked(self, slice: &[T]) -> &Self::Output;
680+
unsafe fn get_unchecked(self, slice: &T) -> &Self::Output;
685681

686682
/// Returns a mutable reference to the output at this location, without
687683
/// performing any bounds checking.
688-
unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut Self::Output;
684+
unsafe fn get_unchecked_mut(self, slice: &mut T) -> &mut Self::Output;
689685

690686
/// Returns a shared reference to the output at this location, panicking
691687
/// if out of bounds.
692-
fn index(self, slice: &[T]) -> &Self::Output;
688+
fn index(self, slice: &T) -> &Self::Output;
693689

694690
/// Returns a mutable reference to the output at this location, panicking
695691
/// if out of bounds.
696-
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output;
692+
fn index_mut(self, slice: &mut T) -> &mut Self::Output;
697693
}
698694

699695
#[stable(feature = "slice-get-slice-impls", since = "1.15.0")]
700-
impl<T> SliceIndex<T> for usize {
696+
impl<T> SliceIndex<[T]> for usize {
701697
type Output = T;
702698

703699
#[inline]
@@ -746,7 +742,7 @@ impl<T> SliceIndex<T> for usize {
746742
}
747743

748744
#[stable(feature = "slice-get-slice-impls", since = "1.15.0")]
749-
impl<T> SliceIndex<T> for ops::Range<usize> {
745+
impl<T> SliceIndex<[T]> for ops::Range<usize> {
750746
type Output = [T];
751747

752748
#[inline]
@@ -807,7 +803,7 @@ impl<T> SliceIndex<T> for ops::Range<usize> {
807803
}
808804

809805
#[stable(feature = "slice-get-slice-impls", since = "1.15.0")]
810-
impl<T> SliceIndex<T> for ops::RangeTo<usize> {
806+
impl<T> SliceIndex<[T]> for ops::RangeTo<usize> {
811807
type Output = [T];
812808

813809
#[inline]
@@ -842,7 +838,7 @@ impl<T> SliceIndex<T> for ops::RangeTo<usize> {
842838
}
843839

844840
#[stable(feature = "slice-get-slice-impls", since = "1.15.0")]
845-
impl<T> SliceIndex<T> for ops::RangeFrom<usize> {
841+
impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
846842
type Output = [T];
847843

848844
#[inline]
@@ -877,7 +873,7 @@ impl<T> SliceIndex<T> for ops::RangeFrom<usize> {
877873
}
878874

879875
#[stable(feature = "slice-get-slice-impls", since = "1.15.0")]
880-
impl<T> SliceIndex<T> for ops::RangeFull {
876+
impl<T> SliceIndex<[T]> for ops::RangeFull {
881877
type Output = [T];
882878

883879
#[inline]
@@ -913,7 +909,7 @@ impl<T> SliceIndex<T> for ops::RangeFull {
913909

914910

915911
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
916-
impl<T> SliceIndex<T> for ops::RangeInclusive<usize> {
912+
impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> {
917913
type Output = [T];
918914

919915
#[inline]
@@ -976,7 +972,7 @@ impl<T> SliceIndex<T> for ops::RangeInclusive<usize> {
976972
}
977973

978974
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
979-
impl<T> SliceIndex<T> for ops::RangeToInclusive<usize> {
975+
impl<T> SliceIndex<[T]> for ops::RangeToInclusive<usize> {
980976
type Output = [T];
981977

982978
#[inline]

0 commit comments

Comments
 (0)