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

Deprecation of str::slice_unchecked(_mut) #51807

Merged
merged 4 commits into from
Jul 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ impl str {
let mut result = String::new();
let mut last_end = 0;
for (start, part) in self.match_indices(from) {
result.push_str(unsafe { self.slice_unchecked(last_end, start) });
result.push_str(unsafe { self.get_unchecked(last_end..start) });
result.push_str(to);
last_end = start + part.len();
}
result.push_str(unsafe { self.slice_unchecked(last_end, self.len()) });
result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
result
}

Expand Down Expand Up @@ -307,11 +307,11 @@ impl str {
let mut result = String::with_capacity(32);
let mut last_end = 0;
for (start, part) in self.match_indices(pat).take(count) {
result.push_str(unsafe { self.slice_unchecked(last_end, start) });
result.push_str(unsafe { self.get_unchecked(last_end..start) });
result.push_str(to);
last_end = start + part.len();
}
result.push_str(unsafe { self.slice_unchecked(last_end, self.len()) });
result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
result
}

Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ impl String {

while idx < len {
let ch = unsafe {
self.slice_unchecked(idx, len).chars().next().unwrap()
self.get_unchecked(idx..len).chars().next().unwrap()
};
let ch_len = ch.len_utf8();

Expand Down
8 changes: 4 additions & 4 deletions src/liballoc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ fn test_join_for_different_lengths_with_long_separator() {

#[test]
fn test_unsafe_slice() {
assert_eq!("ab", unsafe {"abc".slice_unchecked(0, 2)});
assert_eq!("bc", unsafe {"abc".slice_unchecked(1, 3)});
assert_eq!("", unsafe {"abc".slice_unchecked(1, 1)});
assert_eq!("ab", unsafe {"abc".get_unchecked(0..2)});
assert_eq!("bc", unsafe {"abc".get_unchecked(1..3)});
assert_eq!("", unsafe {"abc".get_unchecked(1..1)});
fn a_million_letter_a() -> String {
let mut i = 0;
let mut rs = String::new();
Expand All @@ -200,7 +200,7 @@ fn test_unsafe_slice() {
}
let letters = a_million_letter_a();
assert_eq!(half_a_million_letter_a(),
unsafe { letters.slice_unchecked(0, 500000)});
unsafe { letters.get_unchecked(0..500000)});
}

#[test]
Expand Down
28 changes: 15 additions & 13 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
if !self.finished && (self.allow_trailing_empty || self.end - self.start > 0) {
self.finished = true;
unsafe {
let string = self.matcher.haystack().slice_unchecked(self.start, self.end);
let string = self.matcher.haystack().get_unchecked(self.start..self.end);
Some(string)
}
} else {
Expand All @@ -1070,7 +1070,7 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
let haystack = self.matcher.haystack();
match self.matcher.next_match() {
Some((a, b)) => unsafe {
let elt = haystack.slice_unchecked(self.start, a);
let elt = haystack.get_unchecked(self.start..a);
self.start = b;
Some(elt)
},
Expand All @@ -1095,13 +1095,13 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
let haystack = self.matcher.haystack();
match self.matcher.next_match_back() {
Some((a, b)) => unsafe {
let elt = haystack.slice_unchecked(b, self.end);
let elt = haystack.get_unchecked(b..self.end);
self.end = a;
Some(elt)
},
None => unsafe {
self.finished = true;
Some(haystack.slice_unchecked(self.start, self.end))
Some(haystack.get_unchecked(self.start..self.end))
},
}
}
Expand Down Expand Up @@ -1222,7 +1222,7 @@ impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
#[inline]
fn next(&mut self) -> Option<(usize, &'a str)> {
self.0.next_match().map(|(start, end)| unsafe {
(start, self.0.haystack().slice_unchecked(start, end))
(start, self.0.haystack().get_unchecked(start..end))
})
}

Expand All @@ -1231,7 +1231,7 @@ impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
where P::Searcher: ReverseSearcher<'a>
{
self.0.next_match_back().map(|(start, end)| unsafe {
(start, self.0.haystack().slice_unchecked(start, end))
(start, self.0.haystack().get_unchecked(start..end))
})
}
}
Expand Down Expand Up @@ -1274,7 +1274,7 @@ impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
fn next(&mut self) -> Option<&'a str> {
self.0.next_match().map(|(a, b)| unsafe {
// Indices are known to be on utf8 boundaries
self.0.haystack().slice_unchecked(a, b)
self.0.haystack().get_unchecked(a..b)
})
}

Expand All @@ -1284,7 +1284,7 @@ impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
{
self.0.next_match_back().map(|(a, b)| unsafe {
// Indices are known to be on utf8 boundaries
self.0.haystack().slice_unchecked(a, b)
self.0.haystack().get_unchecked(a..b)
})
}
}
Expand Down Expand Up @@ -2453,6 +2453,7 @@ impl str {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_deprecated(since = "1.28.0", reason = "duplicates `get_unchecked`")]
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if 1.28.0 or 1.29.0 should be used for deprecation version

It should be 1.29.0 (or higher).

#[inline]
pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
(begin..end).get_unchecked(self)
Expand Down Expand Up @@ -2483,6 +2484,7 @@ impl str {
/// * `begin` and `end` must be byte positions within the string slice.
/// * `begin` and `end` must lie on UTF-8 sequence boundaries.
#[stable(feature = "str_slice_mut", since = "1.5.0")]
#[rustc_deprecated(since = "1.28.0", reason = "duplicates `get_unchecked`")]
Copy link
Member

Choose a reason for hiding this comment

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

get_unchecked_mut.

#[inline]
pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
(begin..end).get_unchecked_mut(self)
Expand Down Expand Up @@ -2524,8 +2526,8 @@ impl str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(mid) {
unsafe {
(self.slice_unchecked(0, mid),
self.slice_unchecked(mid, self.len()))
(self.get_unchecked(0..mid),
self.get_unchecked(mid..self.len()))
}
} else {
slice_error_fail(self, 0, mid)
Expand Down Expand Up @@ -3652,7 +3654,7 @@ impl str {
}
unsafe {
// Searcher is known to return valid indices
self.slice_unchecked(i, j)
self.get_unchecked(i..j)
}
}

Expand Down Expand Up @@ -3691,7 +3693,7 @@ impl str {
}
unsafe {
// Searcher is known to return valid indices
self.slice_unchecked(i, self.len())
self.get_unchecked(i..self.len())
}
}

Expand Down Expand Up @@ -3738,7 +3740,7 @@ impl str {
}
unsafe {
// Searcher is known to return valid indices
self.slice_unchecked(0, j)
self.get_unchecked(0..j)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
#[inline]
fn next_back(&mut self) -> SearchStep {
let old_finger = self.finger_back;
let slice = unsafe { self.haystack.slice_unchecked(self.finger, old_finger) };
let slice = unsafe { self.haystack.get_unchecked(self.finger..old_finger) };
let mut iter = slice.chars();
let old_len = iter.iter.len();
if let Some(ch) = iter.next_back() {
Expand Down