Skip to content

Commit feb775c

Browse files
committed
Drain only needs a shared reference
1 parent b0c4a35 commit feb775c

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/liballoc/collections/vec_deque.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1019,19 +1019,17 @@ impl<T> VecDeque<T> {
10191019
// the drain is complete and the Drain destructor is run.
10201020
self.head = drain_tail;
10211021

1022-
// `deque` and `ring` overlap in what they point to, so we must make sure
1023-
// that `ring` is "derived-from" `deque`, or else even just creating ring
1024-
// from `self` already invalidates `deque`.
1025-
let deque = NonNull::from(&mut *self);
1026-
10271022
Drain {
1028-
deque,
1023+
deque: NonNull::from(&mut *self),
10291024
after_tail: drain_head,
10301025
after_head: head,
10311026
iter: Iter {
10321027
tail: drain_tail,
10331028
head: drain_head,
1034-
ring: unsafe { (&mut *deque.as_ptr()).buffer_as_mut_slice() },
1029+
// Crucially, we only create shared references from `self` here and read from
1030+
// it. We do not write to `self` nor reborrow to a mutable reference.
1031+
// Hence the raw pointer we created above, for `deque`, remains valid.
1032+
ring: unsafe { self.buffer_as_slice() },
10351033
},
10361034
}
10371035
}

0 commit comments

Comments
 (0)