|
| 1 | +use core::{fmt, iter::FusedIterator, str::Chars}; |
| 2 | + |
| 3 | +use super::String; |
| 4 | + |
| 5 | +/// A draining iterator for `String`. |
| 6 | +/// |
| 7 | +/// This struct is created by the [`drain`] method on [`String`]. See its |
| 8 | +/// documentation for more. |
| 9 | +/// |
| 10 | +/// [`drain`]: String::drain |
| 11 | +pub struct Drain<'a, const N: usize> { |
| 12 | + /// Will be used as &'a mut String in the destructor |
| 13 | + pub(super) string: *mut String<N>, |
| 14 | + /// Start of part to remove |
| 15 | + pub(super) start: usize, |
| 16 | + /// End of part to remove |
| 17 | + pub(super) end: usize, |
| 18 | + /// Current remaining range to remove |
| 19 | + pub(super) iter: Chars<'a>, |
| 20 | +} |
| 21 | + |
| 22 | +impl<const N: usize> fmt::Debug for Drain<'_, N> { |
| 23 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 24 | + f.debug_tuple("Drain").field(&self.as_str()).finish() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +unsafe impl<const N: usize> Sync for Drain<'_, N> {} |
| 29 | +unsafe impl<const N: usize> Send for Drain<'_, N> {} |
| 30 | + |
| 31 | +impl<const N: usize> Drop for Drain<'_, N> { |
| 32 | + fn drop(&mut self) { |
| 33 | + unsafe { |
| 34 | + // Use `Vec::drain`. “Reaffirm” the bounds checks to avoid |
| 35 | + // panic code being inserted again. |
| 36 | + let self_vec = (*self.string).as_mut_vec(); |
| 37 | + if self.start <= self.end && self.end <= self_vec.len() { |
| 38 | + self_vec.drain(self.start..self.end); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<'a, const N: usize> Drain<'a, N> { |
| 45 | + /// Returns the remaining (sub)string of this iterator as a slice. |
| 46 | + /// |
| 47 | + /// # Examples |
| 48 | + /// |
| 49 | + /// ``` |
| 50 | + /// use heapless::String; |
| 51 | + /// |
| 52 | + /// let mut s = String::<8>::try_from("abc").unwrap(); |
| 53 | + /// let mut drain = s.drain(..); |
| 54 | + /// assert_eq!(drain.as_str(), "abc"); |
| 55 | + /// let _ = drain.next().unwrap(); |
| 56 | + /// assert_eq!(drain.as_str(), "bc"); |
| 57 | + /// ``` |
| 58 | + #[must_use] |
| 59 | + pub fn as_str(&self) -> &str { |
| 60 | + self.iter.as_str() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<const N: usize> AsRef<str> for Drain<'_, N> { |
| 65 | + fn as_ref(&self) -> &str { |
| 66 | + self.as_str() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<const N: usize> AsRef<[u8]> for Drain<'_, N> { |
| 71 | + fn as_ref(&self) -> &[u8] { |
| 72 | + self.as_str().as_bytes() |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl<const N: usize> Iterator for Drain<'_, N> { |
| 77 | + type Item = char; |
| 78 | + |
| 79 | + #[inline] |
| 80 | + fn next(&mut self) -> Option<char> { |
| 81 | + self.iter.next() |
| 82 | + } |
| 83 | + |
| 84 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 85 | + self.iter.size_hint() |
| 86 | + } |
| 87 | + |
| 88 | + #[inline] |
| 89 | + fn last(mut self) -> Option<char> { |
| 90 | + self.next_back() |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<const N: usize> DoubleEndedIterator for Drain<'_, N> { |
| 95 | + #[inline] |
| 96 | + fn next_back(&mut self) -> Option<char> { |
| 97 | + self.iter.next_back() |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl<const N: usize> FusedIterator for Drain<'_, N> {} |
| 102 | + |
| 103 | +#[cfg(test)] |
| 104 | +mod tests { |
| 105 | + use super::String; |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn drain_front() { |
| 109 | + let mut s = String::<8>::try_from("abcd").unwrap(); |
| 110 | + let mut it = s.drain(..1); |
| 111 | + assert_eq!(it.next(), Some('a')); |
| 112 | + drop(it); |
| 113 | + assert_eq!(s, "bcd"); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn drain_middle() { |
| 118 | + let mut s = String::<8>::try_from("abcd").unwrap(); |
| 119 | + let mut it = s.drain(1..3); |
| 120 | + assert_eq!(it.next(), Some('b')); |
| 121 | + assert_eq!(it.next(), Some('c')); |
| 122 | + drop(it); |
| 123 | + assert_eq!(s, "ad"); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn drain_end() { |
| 128 | + let mut s = String::<8>::try_from("abcd").unwrap(); |
| 129 | + let mut it = s.drain(3..); |
| 130 | + assert_eq!(it.next(), Some('d')); |
| 131 | + drop(it); |
| 132 | + assert_eq!(s, "abc"); |
| 133 | + } |
| 134 | +} |
0 commit comments