-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from reitermarkus/drain
Add `Vec::drain` and `String::drain`.
- Loading branch information
Showing
7 changed files
with
576 additions
and
2 deletions.
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
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,38 @@ | ||
use core::ops; | ||
|
||
// FIXME: Remove when `slice_range` feature is stable. | ||
#[track_caller] | ||
#[must_use] | ||
pub fn range<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize> | ||
where | ||
R: ops::RangeBounds<usize>, | ||
{ | ||
let len = bounds.end; | ||
|
||
let start: ops::Bound<&usize> = range.start_bound(); | ||
let start = match start { | ||
ops::Bound::Included(&start) => start, | ||
ops::Bound::Excluded(start) => start | ||
.checked_add(1) | ||
.unwrap_or_else(|| panic!("attempted to index slice from after maximum usize")), | ||
ops::Bound::Unbounded => 0, | ||
}; | ||
|
||
let end: ops::Bound<&usize> = range.end_bound(); | ||
let end = match end { | ||
ops::Bound::Included(end) => end | ||
.checked_add(1) | ||
.unwrap_or_else(|| panic!("attempted to index slice up to maximum usize")), | ||
ops::Bound::Excluded(&end) => end, | ||
ops::Bound::Unbounded => len, | ||
}; | ||
|
||
if start > end { | ||
panic!("slice index starts at {start} but ends at {end}"); | ||
} | ||
if end > len { | ||
panic!("range end index {end} out of range for slice of length {len}"); | ||
} | ||
|
||
ops::Range { start, end } | ||
} |
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,134 @@ | ||
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> {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::String; | ||
|
||
#[test] | ||
fn drain_front() { | ||
let mut s = String::<8>::try_from("abcd").unwrap(); | ||
let mut it = s.drain(..1); | ||
assert_eq!(it.next(), Some('a')); | ||
drop(it); | ||
assert_eq!(s, "bcd"); | ||
} | ||
|
||
#[test] | ||
fn drain_middle() { | ||
let mut s = String::<8>::try_from("abcd").unwrap(); | ||
let mut it = s.drain(1..3); | ||
assert_eq!(it.next(), Some('b')); | ||
assert_eq!(it.next(), Some('c')); | ||
drop(it); | ||
assert_eq!(s, "ad"); | ||
} | ||
|
||
#[test] | ||
fn drain_end() { | ||
let mut s = String::<8>::try_from("abcd").unwrap(); | ||
let mut it = s.drain(3..); | ||
assert_eq!(it.next(), Some('d')); | ||
drop(it); | ||
assert_eq!(s, "abc"); | ||
} | ||
} |
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
Oops, something went wrong.