@@ -12,6 +12,8 @@ use crate::alloc::{Allocator, Global};
1212/// # Example
1313///
1414/// ```
15+ /// #![feature(vec_deque_extract_if)]
16+ ///
1517/// use std::collections::vec_deque::ExtractIf;
1618/// use std::collections::vec_deque::VecDeque;
1719///
@@ -40,14 +42,16 @@ pub struct ExtractIf<
4042}
4143
4244impl < ' a , T , F , A : Allocator > ExtractIf < ' a , T , F , A > {
43- pub ( super ) fn new < R : RangeBounds < usize > > ( vec : & ' a mut Vec < T , A > , pred : F , range : R ) -> Self {
45+ pub ( super ) fn new < R : RangeBounds < usize > > (
46+ vec : & ' a mut VecDeque < T , A > ,
47+ pred : F ,
48+ range : R ,
49+ ) -> Self {
4450 let old_len = vec. len ( ) ;
4551 let Range { start, end } = slice:: range ( range, ..old_len) ;
4652
4753 // Guard against the deque getting leaked (leak amplification)
48- unsafe {
49- vec. len = len;
50- }
54+ vec. len = 0 ;
5155 ExtractIf { vec, idx : start, del : 0 , end, old_len, pred }
5256 }
5357
7882 //
7983 // Note: we can't use `vec.get_mut(i).unwrap()` here since the precondition for that
8084 // function is that i < vec.len, but we've set vec's length to zero.
81- let idx = self . to_physical_idx ( index ) ;
82- let cur = unsafe { & mut * self . ptr ( ) . add ( idx) } ;
85+ let idx = self . vec . to_physical_idx ( i ) ;
86+ let cur = unsafe { & mut * self . vec . ptr ( ) . add ( idx) } ;
8387 let drained = ( self . pred ) ( cur) ;
8488 // Update the index *after* the predicate is called. If the index
8589 // is updated prior and the predicate panics, the element at this
@@ -110,7 +114,7 @@ where
110114impl < T , F , A : Allocator > Drop for ExtractIf < ' _ , T , F , A > {
111115 fn drop ( & mut self ) {
112116 if self . del > 0 {
113- let idx = self . to_physical_idx ( self . idx ) ;
117+ let idx = self . vec . to_physical_idx ( self . idx ) ;
114118 // SAFETY: Trailing unchecked items must be valid since we never touch them.
115119 unsafe {
116120 ptr:: copy (
@@ -121,9 +125,7 @@ impl<T, F, A: Allocator> Drop for ExtractIf<'_, T, F, A> {
121125 }
122126 }
123127 // SAFETY: After filling holes, all items are in contiguous memory.
124- unsafe {
125- self . vec . set_len ( self . old_len - self . del ) ;
126- }
128+ self . vec . len = self . old_len - self . del ;
127129 }
128130}
129131
0 commit comments