Skip to content

Commit fc133a9

Browse files
Rename VecDrop to VecBuffer
This represent more clearly the role of the trait now that it's not used to work around drop specialization.
1 parent ec6700a commit fc133a9

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/vec.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,37 @@ use core::{
55
ops, ptr, slice,
66
};
77

8-
/// Workaround forbidden specialization of Drop
9-
pub trait VecDrop {
10-
// SAFETY: drop_with_len will be called to call drop in place the first `len` elements of the buffer.
11-
// Only the Owned buffer (`[MaybeUninit<T>; N]`) must drop the items
12-
// and the view (`[MaybeUninit<T>]`) drops nothing.
13-
// `drop_with_len `assumes that the buffer can contain `len` elements.
14-
unsafe fn drop_with_len(&mut self, len: usize);
8+
pub trait VecBuffer {
9+
type T;
10+
11+
fn as_vecview(vec: &VecInner<Self>) -> &VecView<Self::T>;
12+
fn as_mut_vecview(vec: &mut VecInner<Self>) -> &mut VecView<Self::T>;
1513
}
1614

17-
impl<T> VecDrop for [MaybeUninit<T>] {
18-
unsafe fn drop_with_len(&mut self, len: usize) {
19-
// NOTE(unsafe) avoid bound checks in the slicing operation
20-
// &mut buffer[..len]
21-
// SAFETY: buffer[..len] must be valid to drop given the safety requirement of the trait definition.
22-
let mut_slice = slice::from_raw_parts_mut(self.as_mut_ptr() as *mut T, len);
23-
// We drop each element used in the vector by turning into a `&mut [T]`.
24-
ptr::drop_in_place(mut_slice);
15+
impl<T, const N: usize> VecBuffer for [MaybeUninit<T>; N] {
16+
type T = T;
17+
18+
fn as_vecview(vec: &VecInner<Self>) -> &VecView<Self::T> {
19+
vec
20+
}
21+
fn as_mut_vecview(vec: &mut VecInner<Self>) -> &mut VecView<Self::T> {
22+
vec
2523
}
2624
}
2725

28-
impl<T, const N: usize> VecDrop for [MaybeUninit<T>; N] {
29-
unsafe fn drop_with_len(&mut self, len: usize) {
30-
VecDrop::drop_with_len(self.as_mut_slice(), len)
26+
impl<T> VecBuffer for [MaybeUninit<T>] {
27+
type T = T;
28+
29+
fn as_vecview(vec: &VecInner<Self>) -> &VecView<Self::T> {
30+
vec
31+
}
32+
fn as_mut_vecview(vec: &mut VecInner<Self>) -> &mut VecView<Self::T> {
33+
vec
3134
}
3235
}
3336

3437
/// <div class="warn">This is private API and should not be used</div>
35-
pub struct VecInner<B: ?Sized + VecDrop> {
38+
pub struct VecInner<B: ?Sized + VecBuffer> {
3639
len: usize,
3740
buffer: B,
3841
}
@@ -1572,10 +1575,12 @@ impl<T, const N: usize, const M: usize> From<[T; M]> for Vec<T, N> {
15721575
}
15731576
}
15741577

1575-
impl<T: ?Sized + VecDrop> Drop for VecInner<T> {
1578+
impl<T: ?Sized + VecBuffer> Drop for VecInner<T> {
15761579
fn drop(&mut self) {
1580+
let mut_slice = VecBuffer::as_mut_vecview(self).as_mut_slice();
1581+
// We drop each element used in the vector by turning into a `&mut [T]`.
15771582
// SAFETY: the buffer contains initialized data for the range 0..self.len
1578-
unsafe { self.buffer.drop_with_len(self.len) }
1583+
unsafe { ptr::drop_in_place(mut_slice) }
15791584
}
15801585
}
15811586

0 commit comments

Comments
 (0)