@@ -9,6 +9,10 @@ use core::{
9
9
10
10
/// Workaround forbidden specialization of Drop
11
11
pub trait VecDrop {
12
+ // SAFETY: drop_with_len will be called to call drop in place the first `len` elements of the buffer.
13
+ // Only the Owned buffer (`[MaybeUninit<T>; N]`) must drop the items
14
+ // and the view (`[MaybeUninit<T>]`) drops nothing.
15
+ // `drop_with_len `assumes that the buffer can contain `len` elements.
12
16
unsafe fn drop_with_len ( & mut self , len : usize ) ;
13
17
}
14
18
@@ -21,12 +25,11 @@ impl<T> VecDrop for [MaybeUninit<T>] {
21
25
impl < T , const N : usize > VecDrop for [ MaybeUninit < T > ; N ] {
22
26
unsafe fn drop_with_len ( & mut self , len : usize ) {
23
27
// NOTE(unsafe) avoid bound checks in the slicing operation
24
- // &mut buffer[..self.len]
25
- let mut_slice = unsafe { slice:: from_raw_parts_mut ( self . as_mut_ptr ( ) as * mut T , len) } ;
28
+ // &mut buffer[..len]
29
+ // SAFETY: buffer[..len] must be valid to drop given the safety requirement of the trait definition.
30
+ let mut_slice = slice:: from_raw_parts_mut ( self . as_mut_ptr ( ) as * mut T , len) ;
26
31
// We drop each element used in the vector by turning into a `&mut [T]`.
27
- unsafe {
28
- ptr:: drop_in_place ( mut_slice) ;
29
- }
32
+ ptr:: drop_in_place ( mut_slice) ;
30
33
}
31
34
}
32
35
@@ -1540,6 +1543,7 @@ impl<T, const N: usize, const M: usize> From<[T; M]> for Vec<T, N> {
1540
1543
1541
1544
impl < T : ?Sized + VecDrop > Drop for VecInner < T > {
1542
1545
fn drop ( & mut self ) {
1546
+ // SAFETY: the buffer contains initialized data for the range 0..self.len
1543
1547
unsafe { self . buffer . drop_with_len ( self . len ) }
1544
1548
}
1545
1549
}
0 commit comments