Skip to content

Commit 238229b

Browse files
committed
fixup compile
Signed-off-by: tison <[email protected]>
1 parent dbf8a13 commit 238229b

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

library/alloc/src/collections/vec_deque/extract_if.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4244
impl<'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

@@ -78,8 +82,8 @@ where
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
110114
impl<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

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
565565
/// Using this method is equivalent to the following code:
566566
///
567567
/// ```
568+
/// #![feature(vec_deque_extract_if)]
569+
/// # use std::collections::VecDeque;
568570
/// # let some_predicate = |x: &mut i32| { *x % 2 == 1 };
569-
/// # let mut deq = (0..10).collect();
571+
/// # let mut deq: VecDeque<_> = (0..10).collect();
570572
/// # let mut deq2 = deq.clone();
571573
/// # let range = 1..5;
572574
/// let mut i = range.start;
@@ -603,6 +605,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
603605
/// Splitting a deque into even and odd values, reusing the original deque:
604606
///
605607
/// ```
608+
/// #![feature(vec_deque_extract_if)]
609+
/// use std::collections::VecDeque;
610+
///
606611
/// let mut numbers = VecDeque::from([1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15]);
607612
///
608613
/// let evens = numbers.extract_if(.., |x| *x % 2 == 0).collect::<VecDeque<_>>();
@@ -615,6 +620,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
615620
/// Using the range argument to only process a part of the deque:
616621
///
617622
/// ```
623+
/// #![feature(vec_deque_extract_if)]
624+
/// use std::collections::VecDeque;
625+
///
618626
/// let mut items = VecDeque::from([0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 2, 1, 2]);
619627
/// let ones = items.extract_if(7.., |x| *x == 1).collect::<VecDeque<_>>();
620628
/// assert_eq!(items, VecDeque::from([0, 0, 0, 0, 0, 0, 0, 2, 2, 2]));

0 commit comments

Comments
 (0)