1 // SPDX-License-Identifier: Apache-2.0 OR MIT 2 3 use crate::alloc::{Allocator, Global}; 4 use core::ptr; 5 use core::slice; 6 7 use super::Vec; 8 9 /// An iterator which uses a closure to determine if an element should be removed. 10 /// 11 /// This struct is created by [`Vec::extract_if`]. 12 /// See its documentation for more. 13 /// 14 /// # Example 15 /// 16 /// ``` 17 /// #![feature(extract_if)] 18 /// 19 /// let mut v = vec![0, 1, 2]; 20 /// let iter: std::vec::ExtractIf<'_, _, _> = v.extract_if(|x| *x % 2 == 0); 21 /// ``` 22 #[unstable(feature = "extract_if", reason = "recently added", issue = "43244")] 23 #[derive(Debug)] 24 #[must_use = "iterators are lazy and do nothing unless consumed"] 25 pub struct ExtractIf< 26 'a, 27 T, 28 F, 29 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, 30 > where 31 F: FnMut(&mut T) -> bool, 32 { 33 pub(super) vec: &'a mut Vec<T, A>, 34 /// The index of the item that will be inspected by the next call to `next`. 35 pub(super) idx: usize, 36 /// The number of items that have been drained (removed) thus far. 37 pub(super) del: usize, 38 /// The original length of `vec` prior to draining. 39 pub(super) old_len: usize, 40 /// The filter test predicate. 41 pub(super) pred: F, 42 } 43 44 impl<T, F, A: Allocator> ExtractIf<'_, T, F, A> 45 where 46 F: FnMut(&mut T) -> bool, 47 { 48 /// Returns a reference to the underlying allocator. 49 #[unstable(feature = "allocator_api", issue = "32838")] 50 #[inline] 51 pub fn allocator(&self) -> &A { 52 self.vec.allocator() 53 } 54 } 55 56 #[unstable(feature = "extract_if", reason = "recently added", issue = "43244")] 57 impl<T, F, A: Allocator> Iterator for ExtractIf<'_, T, F, A> 58 where 59 F: FnMut(&mut T) -> bool, 60 { 61 type Item = T; 62 63 fn next(&mut self) -> Option<T> { 64 unsafe { 65 while self.idx < self.old_len { 66 let i = self.idx; 67 let v = slice::from_raw_parts_mut(self.vec.as_mut_ptr(), self.old_len); 68 let drained = (self.pred)(&mut v[i]); 69 // Update the index *after* the predicate is called. If the index 70 // is updated prior and the predicate panics, the element at this 71 // index would be leaked. 72 self.idx += 1; 73 if drained { 74 self.del += 1; 75 return Some(ptr::read(&v[i])); 76 } else if self.del > 0 { 77 let del = self.del; 78 let src: *const T = &v[i]; 79 let dst: *mut T = &mut v[i - del]; 80 ptr::copy_nonoverlapping(src, dst, 1); 81 } 82 } 83 None 84 } 85 } 86 87 fn size_hint(&self) -> (usize, Option<usize>) { 88 (0, Some(self.old_len - self.idx)) 89 } 90 } 91 92 #[unstable(feature = "extract_if", reason = "recently added", issue = "43244")] 93 impl<T, F, A: Allocator> Drop for ExtractIf<'_, T, F, A> 94 where 95 F: FnMut(&mut T) -> bool, 96 { 97 fn drop(&mut self) { 98 unsafe { 99 if self.idx < self.old_len && self.del > 0 { 100 // This is a pretty messed up state, and there isn't really an 101 // obviously right thing to do. We don't want to keep trying 102 // to execute `pred`, so we just backshift all the unprocessed 103 // elements and tell the vec that they still exist. The backshift 104 // is required to prevent a double-drop of the last successfully 105 // drained item prior to a panic in the predicate. 106 let ptr = self.vec.as_mut_ptr(); 107 let src = ptr.add(self.idx); 108 let dst = src.sub(self.del); 109 let tail_len = self.old_len - self.idx; 110 src.copy_to(dst, tail_len); 111 } 112 self.vec.set_len(self.old_len - self.del); 113 } 114 } 115 } 116