-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f23ab72
commit 873bdf5
Showing
3 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use core::{fmt, iter::FusedIterator, str::Chars}; | ||
|
||
use super::String; | ||
|
||
/// A draining iterator for `String`. | ||
/// | ||
/// This struct is created by the [`drain`] method on [`String`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`drain`]: String::drain | ||
pub struct Drain<'a, const N: usize> { | ||
/// Will be used as &'a mut String in the destructor | ||
pub(super) string: *mut String<N>, | ||
/// Start of part to remove | ||
pub(super) start: usize, | ||
/// End of part to remove | ||
pub(super) end: usize, | ||
/// Current remaining range to remove | ||
pub(super) iter: Chars<'a>, | ||
} | ||
|
||
impl<const N: usize> fmt::Debug for Drain<'_, N> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple("Drain").field(&self.as_str()).finish() | ||
} | ||
} | ||
|
||
unsafe impl<const N: usize> Sync for Drain<'_, N> {} | ||
unsafe impl<const N: usize> Send for Drain<'_, N> {} | ||
|
||
impl<const N: usize> Drop for Drain<'_, N> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
// Use `Vec::drain`. “Reaffirm” the bounds checks to avoid | ||
// panic code being inserted again. | ||
let self_vec = (*self.string).as_mut_vec(); | ||
if self.start <= self.end && self.end <= self_vec.len() { | ||
self_vec.drain(self.start..self.end); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'a, const N: usize> Drain<'a, N> { | ||
/// Returns the remaining (sub)string of this iterator as a slice. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use heapless::String; | ||
/// | ||
/// let mut s = String::<8>::try_from("abc").unwrap(); | ||
/// let mut drain = s.drain(..); | ||
/// assert_eq!(drain.as_str(), "abc"); | ||
/// let _ = drain.next().unwrap(); | ||
/// assert_eq!(drain.as_str(), "bc"); | ||
/// ``` | ||
#[must_use] | ||
pub fn as_str(&self) -> &str { | ||
self.iter.as_str() | ||
} | ||
} | ||
|
||
impl<const N: usize> AsRef<str> for Drain<'_, N> { | ||
fn as_ref(&self) -> &str { | ||
self.as_str() | ||
} | ||
} | ||
|
||
impl<const N: usize> AsRef<[u8]> for Drain<'_, N> { | ||
fn as_ref(&self) -> &[u8] { | ||
self.as_str().as_bytes() | ||
} | ||
} | ||
|
||
impl<const N: usize> Iterator for Drain<'_, N> { | ||
type Item = char; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<char> { | ||
self.iter.next() | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.iter.size_hint() | ||
} | ||
|
||
#[inline] | ||
fn last(mut self) -> Option<char> { | ||
self.next_back() | ||
} | ||
} | ||
|
||
impl<const N: usize> DoubleEndedIterator for Drain<'_, N> { | ||
#[inline] | ||
fn next_back(&mut self) -> Option<char> { | ||
self.iter.next_back() | ||
} | ||
} | ||
|
||
impl<const N: usize> FusedIterator for Drain<'_, N> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters