Skip to content

Commit

Permalink
Remove the use of mem::zeroed() on generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor committed May 11, 2022
1 parent e625dfb commit e8b215c
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,13 +1020,10 @@ impl TestGenerator {
macro_rules! offset_of {
($ty:ident, $field:ident) => ({
let zeroed_ty = std::mem::zeroed::<$ty>();
let ty_ptr = &zeroed_ty as *const $ty;
let field_ptr = std::ptr::addr_of!(zeroed_ty.$field);
let ty_address = ty_ptr as u64;
let field_address = field_ptr as u64;
std::mem::forget(zeroed_ty);
field_address.checked_sub(ty_address).unwrap()
let value = std::mem::MaybeUninit::<$ty>::uninit();
let base_pointer = value.as_ptr();
let offset_pointer = std::ptr::addr_of!((*base_pointer).$field);
(offset_pointer as u64) - (base_pointer as u64)
})
}
Expand Down Expand Up @@ -1277,8 +1274,9 @@ impl<'a> Generator<'a> {
fn __test_fsize_{ty}_{field}() -> u64;
}}
unsafe {{
let zeroed_ty = std::mem::zeroed::<{ty}>();
let ty_ptr = std::ptr::addr_of!((zeroed_ty).{field});
let uninit_ty = std::mem::MaybeUninit::<{ty}>::uninit();
let uninit_ty = uninit_ty.as_ptr();
let ty_ptr = std::ptr::addr_of!((*uninit_ty).{field});
let val = ty_ptr.read_unaligned();
same(offset_of!({ty}, {field}),
__test_offset_{ty}_{field}(),
Expand Down Expand Up @@ -1328,13 +1326,14 @@ impl<'a> Generator<'a> {
-> *mut u8;
}}
unsafe {{
let mut zeroed_ty = mem::zeroed::<{ty}>();
let ty_ptr_mut = std::ptr::addr_of_mut!(zeroed_ty);
let field_ptr = std::ptr::addr_of!(zeroed_ty.{field});
let mut uninit_ty = std::mem::MaybeUninit::<{ty}>::uninit();
let uninit_ty = uninit_ty.as_mut_ptr();
let ty_ptr_mut = std::ptr::addr_of_mut!(*uninit_ty);
let field_ptr = std::ptr::addr_of!((*uninit_ty).{field});
same(field_ptr as *mut _,
__test_field_type_{ty}_{field}(ty_ptr_mut),
"field type {field} of {ty}");
mem::forget(zeroed_ty);
mem::forget(uninit_ty);
}}
"#,
ty = ty,
Expand Down

0 comments on commit e8b215c

Please sign in to comment.