SOLVED: more alignment fun

This commit is contained in:
nora 2021-12-21 13:22:51 +01:00
parent e9965ca40f
commit 338b7cc1ef
2 changed files with 3 additions and 2 deletions

View file

@ -117,7 +117,7 @@ impl<T: ?Sized> Vechonk<T> {
pub fn push(&mut self, element: Box<T>) { pub fn push(&mut self, element: Box<T>) {
let elem_size = mem::size_of_val(element.as_ref()); let elem_size = mem::size_of_val(element.as_ref());
let elem_align = mem::align_of_val(&element); let elem_align = mem::align_of_val(element.as_ref());
let elem_ptr = Box::into_raw(element); let elem_ptr = Box::into_raw(element);
let meta = ptr::metadata(elem_ptr); let meta = ptr::metadata(elem_ptr);
@ -155,6 +155,7 @@ impl<T: ?Sized> Vechonk<T> {
// SAFETY: `elem_ptr` comes from `Box`, and is therefore valid to read from for the size // SAFETY: `elem_ptr` comes from `Box`, and is therefore valid to read from for the size
// We have made sure above that we have more than `elem_size` bytes free // We have made sure above that we have more than `elem_size` bytes free
// The two allocations cannot overlap, since the `Box` owned its contents, and so do we // The two allocations cannot overlap, since the `Box` owned its contents, and so do we
// `dest_ptr` has been aligned above
unsafe { unsafe {
ptr::copy_nonoverlapping::<u8>(elem_ptr as _, dest_ptr, elem_size); ptr::copy_nonoverlapping::<u8>(elem_ptr as _, dest_ptr, elem_size);
} }

View file

@ -133,8 +133,8 @@ fn push_alignment() {
let mut chonk = Vechonk::<dyn Any>::with_capacity(4096); let mut chonk = Vechonk::<dyn Any>::with_capacity(4096);
chonk.push(Box::new(0_u8));
chonk.push(Box::new(BigAlign(5))); chonk.push(Box::new(BigAlign(5)));
chonk.push(Box::new(0_u8));
chonk.push(Box::new(1_u64)); chonk.push(Box::new(1_u64));
let _ = chonk[0]; let _ = chonk[0];