Skip to content

Commit f30328c

Browse files
committed
use vec::IntoIter instead
Signed-off-by: tison <[email protected]>
1 parent 4f37c6f commit f30328c

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

library/alloc/src/string.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use crate::collections::TryReserveError;
6565
use crate::str::{self, CharIndices, Chars, Utf8Error, from_utf8_unchecked_mut};
6666
#[cfg(not(no_global_oom_handling))]
6767
use crate::str::{FromStr, from_boxed_utf8_unchecked};
68+
use crate::vec;
6869
use crate::vec::Vec;
6970

7071
/// A UTF-8–encoded, growable string.
@@ -3103,7 +3104,7 @@ impl fmt::Write for String {
31033104
/// Placeholder docs.
31043105
#[unstable(feature = "into_chars", reason = "new API", issue = "none")]
31053106
pub struct IntoChars {
3106-
bytes: Vec<u8>,
3107+
bytes: vec::IntoIter<u8>,
31073108
}
31083109

31093110
#[unstable(feature = "into_chars", reason = "new API", issue = "none")]
@@ -3151,7 +3152,8 @@ impl Iterator for IntoChars {
31513152
None => None,
31523153
Some((_, ch)) => {
31533154
let offset = iter.offset();
3154-
drop(self.bytes.drain(..offset));
3155+
// SAFETY: `offset` is a valid index.
3156+
let _ = self.bytes.advance_by(offset);
31553157
Some(ch)
31563158
}
31573159
}
@@ -3171,11 +3173,13 @@ impl Iterator for IntoChars {
31713173
impl DoubleEndedIterator for IntoChars {
31723174
#[inline]
31733175
fn next_back(&mut self) -> Option<char> {
3176+
let len = self.as_str().len();
31743177
let mut iter = self.iter();
31753178
match iter.next_back() {
31763179
None => None,
31773180
Some((idx, ch)) => {
3178-
self.bytes.truncate(idx);
3181+
// SAFETY: `idx` is a valid index.
3182+
let _ = self.bytes.advance_back_by(len - idx);
31793183
Some(ch)
31803184
}
31813185
}

0 commit comments

Comments
 (0)