Skip to content

Commit

Permalink
use vec::IntoIter instead
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Nov 15, 2024
1 parent 4f37c6f commit f30328c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use crate::collections::TryReserveError;
use crate::str::{self, CharIndices, Chars, Utf8Error, from_utf8_unchecked_mut};
#[cfg(not(no_global_oom_handling))]
use crate::str::{FromStr, from_boxed_utf8_unchecked};
use crate::vec;
use crate::vec::Vec;

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

#[unstable(feature = "into_chars", reason = "new API", issue = "none")]
Expand Down Expand Up @@ -3151,7 +3152,8 @@ impl Iterator for IntoChars {
None => None,
Some((_, ch)) => {
let offset = iter.offset();
drop(self.bytes.drain(..offset));
// SAFETY: `offset` is a valid index.
let _ = self.bytes.advance_by(offset);
Some(ch)
}
}
Expand All @@ -3171,11 +3173,13 @@ impl Iterator for IntoChars {
impl DoubleEndedIterator for IntoChars {
#[inline]
fn next_back(&mut self) -> Option<char> {
let len = self.as_str().len();
let mut iter = self.iter();
match iter.next_back() {
None => None,
Some((idx, ch)) => {
self.bytes.truncate(idx);
// SAFETY: `idx` is a valid index.
let _ = self.bytes.advance_back_by(len - idx);
Some(ch)
}
}
Expand Down

0 comments on commit f30328c

Please sign in to comment.