Tighten internal visibility and naming
This commit is contained in:
+3
-3
@@ -37,11 +37,11 @@ impl<'vec, T> ContentGuard<'vec, T> {
|
||||
///
|
||||
/// The guard remains usable with an empty, zero-capacity vector.
|
||||
pub fn take(&mut self) -> Vec<T> {
|
||||
let erased = std::mem::replace(self.erased, (self.reerase)(Vec::new()));
|
||||
let erased = ManuallyDrop::new(erased);
|
||||
let old_erased = std::mem::replace(self.erased, (self.reerase)(Vec::new()));
|
||||
let old_erased = ManuallyDrop::new(old_erased);
|
||||
// SAFETY: The erased pointer, length, and capacity are valid for a Vec<T>.
|
||||
// ManuallyDrop prevents the old TypeErasedVec from double-freeing the allocation.
|
||||
unsafe { erased.parts.into_vec() }
|
||||
unsafe { old_erased.parts.into_vec() }
|
||||
}
|
||||
|
||||
/// Calls `function` with the underlying vector and then type-erases it again.
|
||||
|
||||
+10
-10
@@ -1,34 +1,34 @@
|
||||
use std::ptr::NonNull;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct VecParts {
|
||||
pub ptr: NonNull<u8>,
|
||||
pub len: usize,
|
||||
pub cap: usize,
|
||||
pub(super) struct VecParts {
|
||||
pub(super) ptr: NonNull<u8>,
|
||||
pub(super) len: usize,
|
||||
pub(super) cap: usize,
|
||||
}
|
||||
|
||||
impl VecParts {
|
||||
pub(crate) fn from_vec<T>(vec: Vec<T>) -> Self {
|
||||
let (ptr, len, cap) = vec.into_raw_parts();
|
||||
pub(super) fn from_vec<T>(vec: Vec<T>) -> Self {
|
||||
let (raw_ptr, len, cap) = vec.into_raw_parts();
|
||||
|
||||
// SAFETY: Vec guarantees its underlying pointer is non-null.
|
||||
let ptr = unsafe { NonNull::new_unchecked(ptr.cast::<u8>()) };
|
||||
let ptr = unsafe { NonNull::new_unchecked(raw_ptr.cast::<u8>()) };
|
||||
|
||||
Self { ptr, len, cap }
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn into_vec<T>(self) -> Vec<T> {
|
||||
pub(super) unsafe fn into_vec<T>(self) -> Vec<T> {
|
||||
unsafe { Vec::from_raw_parts(self.ptr.as_ptr().cast::<T>(), self.len, self.cap) }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub(crate) unsafe fn as_slice<T>(&self) -> &[T] {
|
||||
pub(super) unsafe fn as_slice<T>(&self) -> &[T] {
|
||||
// SAFETY: Ensured by the caller
|
||||
unsafe { std::slice::from_raw_parts(self.ptr.as_ptr().cast(), self.len) }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub(crate) unsafe fn as_slice_mut<T>(&mut self) -> &mut [T] {
|
||||
pub(super) unsafe fn as_slice_mut<T>(&mut self) -> &mut [T] {
|
||||
// SAFETY: Ensured by the caller
|
||||
unsafe { std::slice::from_raw_parts_mut(self.ptr.as_ptr().cast(), self.len) }
|
||||
}
|
||||
|
||||
+5
-5
@@ -1,16 +1,16 @@
|
||||
use crate::VecParts;
|
||||
|
||||
pub(crate) struct TypeErasedVecVtable {
|
||||
pub(super) struct TypeErasedVecVtable {
|
||||
/// Function pointer to reserve additional capacity in the underlying Vec
|
||||
pub reserve: unsafe fn(parts: &mut VecParts, additional: usize),
|
||||
pub(super) reserve: unsafe fn(parts: &mut VecParts, additional: usize),
|
||||
/// Function pointer to clear the underlying capacity of its old elements
|
||||
pub clear: unsafe fn(parts: &mut VecParts),
|
||||
pub(super) clear: unsafe fn(parts: &mut VecParts),
|
||||
/// Function pointer to the original type's drop logic.
|
||||
pub drop: unsafe fn(parts: VecParts),
|
||||
pub(super) drop: unsafe fn(parts: VecParts),
|
||||
}
|
||||
|
||||
impl TypeErasedVecVtable {
|
||||
pub fn new<T>() -> Self {
|
||||
pub(super) fn new<T>() -> Self {
|
||||
unsafe fn drop_vec<T>(parts: VecParts) {
|
||||
// SAFETY: We reconstruct the Vec to let its Drop impl handle deallocation.
|
||||
_ = unsafe { parts.into_vec::<T>() };
|
||||
|
||||
Reference in New Issue
Block a user