| 1 | use super::*; | 
| 2 |  | 
|---|
| 3 | use core::{ | 
|---|
| 4 | ops::{Bound, RangeBounds}, | 
|---|
| 5 | slice, | 
|---|
| 6 | }; | 
|---|
| 7 |  | 
|---|
| 8 | /// Draining iterator for [`ArrayVec`] | 
|---|
| 9 | /// | 
|---|
| 10 | /// See [`ArrayVec::drain`](ArrayVec::drain) | 
|---|
| 11 | pub struct ArrayVecDrain<'a, T: 'a + Default> { | 
|---|
| 12 | iter: slice::IterMut<'a, T>, | 
|---|
| 13 | } | 
|---|
| 14 |  | 
|---|
| 15 | impl<'a, T: 'a + Default> ArrayVecDrain<'a, T> { | 
|---|
| 16 | pub(crate) fn new<A, R>(arr: &'a mut ArrayVec<A>, range: R) -> Self | 
|---|
| 17 | where | 
|---|
| 18 | A: Array<Item = T>, | 
|---|
| 19 | R: RangeBounds<usize>, | 
|---|
| 20 | { | 
|---|
| 21 | let start = match range.start_bound() { | 
|---|
| 22 | Bound::Unbounded => 0, | 
|---|
| 23 | Bound::Included(&n) => n, | 
|---|
| 24 | Bound::Excluded(&n) => n.saturating_add(1), | 
|---|
| 25 | }; | 
|---|
| 26 | let end = match range.end_bound() { | 
|---|
| 27 | Bound::Unbounded => arr.len(), | 
|---|
| 28 | Bound::Included(&n) => n.saturating_add(1), | 
|---|
| 29 | Bound::Excluded(&n) => n, | 
|---|
| 30 | }; | 
|---|
| 31 |  | 
|---|
| 32 | assert!( | 
|---|
| 33 | start <= end, | 
|---|
| 34 | "ArrayVec::drain> Illegal range, {}  to {} ", | 
|---|
| 35 | start, | 
|---|
| 36 | end | 
|---|
| 37 | ); | 
|---|
| 38 | assert!( | 
|---|
| 39 | end <= arr.len(), | 
|---|
| 40 | "ArrayVec::drain> Range ends at {} , but length is only {} ", | 
|---|
| 41 | end, | 
|---|
| 42 | arr.len() | 
|---|
| 43 | ); | 
|---|
| 44 |  | 
|---|
| 45 | let len = end - start; | 
|---|
| 46 | let to_rotate = &mut arr[start..]; | 
|---|
| 47 | to_rotate.rotate_left(len); | 
|---|
| 48 |  | 
|---|
| 49 | let oldlen = arr.len(); | 
|---|
| 50 | let newlen = oldlen - len; | 
|---|
| 51 | arr.set_len(newlen); | 
|---|
| 52 | let slice = &mut arr.data.as_slice_mut()[newlen..oldlen]; | 
|---|
| 53 | let iter = slice.iter_mut(); | 
|---|
| 54 | Self { iter } | 
|---|
| 55 | } | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | impl<'a, T: 'a + Default> DoubleEndedIterator for ArrayVecDrain<'a, T> { | 
|---|
| 59 | #[ inline] | 
|---|
| 60 | fn next_back(&mut self) -> Option<Self::Item> { | 
|---|
| 61 | self.iter.next_back().map(core::mem::take) | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | #[ inline] | 
|---|
| 65 | fn nth_back(&mut self, n: usize) -> Option<Self::Item> { | 
|---|
| 66 | self.iter.nth_back(n).map(core::mem::take) | 
|---|
| 67 | } | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | impl<'a, T: 'a + Default> Iterator for ArrayVecDrain<'a, T> { | 
|---|
| 71 | type Item = T; | 
|---|
| 72 | #[ inline] | 
|---|
| 73 | fn next(&mut self) -> Option<Self::Item> { | 
|---|
| 74 | self.iter.next().map(core::mem::take) | 
|---|
| 75 | } | 
|---|
| 76 | #[ inline] | 
|---|
| 77 | fn size_hint(&self) -> (usize, Option<usize>) { | 
|---|
| 78 | self.iter.size_hint() | 
|---|
| 79 | } | 
|---|
| 80 | #[ inline] | 
|---|
| 81 | fn nth(&mut self, n: usize) -> Option<Self::Item> { | 
|---|
| 82 | self.iter.nth(n).map(core::mem::take) | 
|---|
| 83 | } | 
|---|
| 84 | #[ inline] | 
|---|
| 85 | fn last(self) -> Option<Self::Item> { | 
|---|
| 86 | self.iter.last().map(core::mem::take) | 
|---|
| 87 | } | 
|---|
| 88 | #[ inline] | 
|---|
| 89 | fn for_each<F>(self, f: F) | 
|---|
| 90 | where | 
|---|
| 91 | F: FnMut(Self::Item), | 
|---|
| 92 | { | 
|---|
| 93 | self.iter.map(core::mem::take).for_each(f) | 
|---|
| 94 | } | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | impl<'a, T: 'a + Default> FusedIterator for ArrayVecDrain<'a, T> {} | 
|---|
| 98 | impl<'a, T: 'a + Default> ExactSizeIterator for ArrayVecDrain<'a, T> {} | 
|---|
| 99 | /* No need to impl Drop! */ | 
|---|
| 100 |  | 
|---|