diff --git a/src/librustc_metadata/index.rs b/src/librustc_metadata/index.rs index 53e6988c756c..5b52b268849d 100644 --- a/src/librustc_metadata/index.rs +++ b/src/librustc_metadata/index.rs @@ -70,7 +70,7 @@ pub fn lookup(&self, bytes: &[u8], def_index: DefIndex) -> Option(&self, bytes: &'a [u8]) -> impl Iterator>)> + 'a { let words = &bytes_to_words(&bytes[self.position..])[..self.len]; - words.iter().enumerate().filter_map(|(index, &position)| { + words.iter().map(|word| word.get()).enumerate().filter_map(|(index, position)| { if position == u32::MAX { None } else { @@ -95,8 +95,16 @@ pub fn iter_enumerated<'a>(&self, } } -fn bytes_to_words(b: &[u8]) -> &[u32] { - unsafe { slice::from_raw_parts(b.as_ptr() as *const u32, b.len() / 4) } +#[repr(packed)] +#[derive(Copy, Clone)] +struct Unaligned(T); + +impl Unaligned { + fn get(self) -> T { self.0 } +} + +fn bytes_to_words(b: &[u8]) -> &[Unaligned] { + unsafe { slice::from_raw_parts(b.as_ptr() as *const Unaligned, b.len() / 4) } } fn words_to_bytes(w: &[u32]) -> &[u8] {