Skip to content

Commit 5e5bedb

Browse files
authored
Merge pull request #237 from rytheo/avoid-unaligned-ref
Avoid creating unaligned ref in H5Type derive macro
2 parents a6adcf1 + 90bd2ee commit 5e5bedb

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

hdf5-derive/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,16 @@ where
4444
F: ToTokens,
4545
{
4646
quote! {
47-
let origin: *const #ty #ty_generics = ::std::ptr::null();
47+
let origin = ::std::mem::MaybeUninit::<#ty #ty_generics>::uninit();
48+
let origin_ptr = origin.as_ptr();
4849
let mut fields = vec![#(
4950
_h5::types::CompoundField {
5051
name: #names.to_owned(),
5152
ty: <#types as _h5::types::H5Type>::type_descriptor(),
52-
offset: unsafe { &((*origin).#fields) as *const _ as _ },
53+
offset: unsafe {
54+
::std::ptr::addr_of!((*origin_ptr).#fields).cast::<u8>()
55+
.offset_from(origin_ptr.cast()) as usize
56+
},
5357
index: 0,
5458
}
5559
),*];

hdf5-derive/tests/test.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// due to compiler wrongfully complaining re: Copy impl missing for packed struct
2-
#![allow(unaligned_references)]
3-
41
#[macro_use]
52
extern crate hdf5_derive;
63

0 commit comments

Comments
 (0)