finish refactor
This commit is contained in:
+2
-2
@@ -45,13 +45,13 @@ impl TypeErasedVec {
|
||||
/// Clear any remaining elements in the vector, dropping them.
|
||||
pub fn clear(&mut self) {
|
||||
// SAFETY: The clear function correctly targets the currently stored type elements.
|
||||
self.parts = unsafe { (self.vtable.clear)(self.parts) };
|
||||
unsafe { (self.vtable.clear)(&mut self.parts) };
|
||||
}
|
||||
|
||||
/// Reserve additional elements in the vector.
|
||||
pub fn reserve(&mut self, additional: usize) {
|
||||
// SAFETY: The reserve function correctly targets the currently stored type elements.
|
||||
self.parts = unsafe { (self.vtable.reserve)(self.parts, additional) };
|
||||
unsafe { (self.vtable.reserve)(&mut self.parts, additional) };
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
||||
@@ -323,6 +323,7 @@ fn test_guard_push() {
|
||||
let mut guard = erased.as_type::<i32>();
|
||||
|
||||
guard.push(42);
|
||||
assert_eq!(guard.with(|v| v[0]), 42);
|
||||
assert_eq!(guard.length(), 1);
|
||||
assert_eq!(guard.as_slice(), &[42]);
|
||||
}
|
||||
|
||||
+6
-6
@@ -2,9 +2,9 @@ use crate::VecParts;
|
||||
|
||||
pub(crate) struct TypeErasedVecVtable {
|
||||
/// Function pointer to reserve additional capacity in the underlying Vec
|
||||
pub reserve: unsafe fn(parts: VecParts, additional: usize) -> VecParts,
|
||||
pub reserve: unsafe fn(parts: &mut VecParts, additional: usize),
|
||||
/// Function pointer to clear the underlying capacity of its old elements
|
||||
pub clear: unsafe fn(parts: VecParts) -> VecParts,
|
||||
pub clear: unsafe fn(parts: &mut VecParts),
|
||||
/// Function pointer to the original type's drop logic.
|
||||
pub drop: unsafe fn(parts: VecParts),
|
||||
}
|
||||
@@ -16,7 +16,7 @@ impl TypeErasedVecVtable {
|
||||
_ = unsafe { parts.into_vec::<T>() };
|
||||
}
|
||||
|
||||
unsafe fn clear_vec<T>(parts: VecParts) -> VecParts {
|
||||
unsafe fn clear_vec<T>(parts: &mut VecParts) {
|
||||
// SAFETY: We reconstruct the Vec to call clear, dropping only the elements.
|
||||
// Using into_raw_parts ensures we don't accidentally drop the allocation itself.
|
||||
unsafe {
|
||||
@@ -26,11 +26,11 @@ impl TypeErasedVecVtable {
|
||||
debug_assert!(std::ptr::eq(new_parts.ptr.as_ptr(), parts.ptr.as_ptr()));
|
||||
debug_assert_eq!(new_parts.cap, parts.cap);
|
||||
debug_assert_eq!(new_parts.len, 0);
|
||||
new_parts
|
||||
*parts = new_parts;
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn reserve_vec<T>(parts: VecParts, additional: usize) -> VecParts {
|
||||
unsafe fn reserve_vec<T>(parts: &mut VecParts, additional: usize) {
|
||||
// SAFETY: We reconstruct the Vec to trigger a reserve.
|
||||
// Capacity might change upon reallocation.
|
||||
unsafe {
|
||||
@@ -39,7 +39,7 @@ impl TypeErasedVecVtable {
|
||||
let new_parts = VecParts::from_vec(vec);
|
||||
debug_assert_eq!(new_parts.len, parts.len);
|
||||
|
||||
new_parts
|
||||
*parts = new_parts;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user