]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/vec/in_place_drop.rs
Auto merge of #104990 - matthiaskrgr:rollup-oskk8v3, r=matthiaskrgr
[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 }
25
26 // A helper struct for in-place collection that drops the destination allocation and elements,
27 // to avoid leaking them if some other destructor panics.
28 pub(super) struct InPlaceDstBufDrop<T> {
29     pub(super) ptr: *mut T,
30     pub(super) len: usize,
31     pub(super) cap: usize,
32 }
33
34 impl<T> Drop for InPlaceDstBufDrop<T> {
35     #[inline]
36     fn drop(&mut self) {
37         unsafe { super::Vec::from_raw_parts(self.ptr, self.len, self.cap) };
38     }
39 }