]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/vec/in_place_drop.rs
Merge commit '63734fcdd718cca089f84c42f3a42c0096cfd431' into sync_cg_clif-2022-05-15
[rust.git] / library / alloc / src / vec / in_place_drop.rs
1 use core::ptr::{self};
2 use core::slice::{self};
3
4 // A helper struct for in-place iteration that drops the destination slice of iteration,
5 // i.e. the head. The source slice (the tail) is dropped by IntoIter.
6 pub(super) struct InPlaceDrop<T> {
7     pub(super) inner: *mut T,
8     pub(super) dst: *mut T,
9 }
10
11 impl<T> InPlaceDrop<T> {
12     fn len(&self) -> usize {
13         unsafe { self.dst.sub_ptr(self.inner) }
14     }
15 }
16
17 impl<T> Drop for InPlaceDrop<T> {
18     #[inline]
19     fn drop(&mut self) {
20         unsafe {
21             ptr::drop_in_place(slice::from_raw_parts_mut(self.inner, self.len()));
22         }
23     }
24 }