Skip to content

Commit

Permalink
Cleanup unsafe in Matrix impls
Browse files Browse the repository at this point in the history
  • Loading branch information
james7132 committed Mar 28, 2024
1 parent b31fe1b commit 4ee8377
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions src/types/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,7 @@ macro_rules! impl_matrix_inner {
reader.advance(<Self as $crate::private::ShaderType>::METADATA.col_padding() as ::core::primitive::usize);
}
} else {
let ptr = (self as *mut Self) as *mut ::core::primitive::u8;
let byte_slice: &mut [::core::primitive::u8] =
unsafe { ::core::slice::from_raw_parts_mut(ptr, ::core::mem::size_of::<Self>()) };
reader.read_slice(byte_slice);
$crate::private::ReadFrom::read_from(columns, reader);
}
}
#[cfg(not(target_endian = "little"))]
Expand Down Expand Up @@ -239,25 +236,13 @@ macro_rules! impl_matrix_inner {

$crate::private::FromMatrixParts::<$el_ty, $c, $r>::from_parts(columns)
} else {
let mut me = ::core::mem::MaybeUninit::zeroed();
let ptr: *mut ::core::mem::MaybeUninit<Self> = &mut me;
let ptr = ptr.cast::<::core::primitive::u8>();
let byte_slice: &mut [::core::primitive::u8] = unsafe {
::core::slice::from_raw_parts_mut(ptr, ::core::mem::size_of::<Self>())
};
reader.read_slice(byte_slice);
// SAFETY: All values were properly initialized by reading the bytes.
unsafe { me.assume_init() }
let columns = $crate::private::CreateFrom::create_from(reader);
$crate::private::FromMatrixParts::<$el_ty, $c, $r>::from_parts(columns)
}
}
#[cfg(not(target_endian = "little"))]
{
let columns = ::core::array::from_fn(|_| {
let col = $crate::private::CreateFrom::create_from(reader);
reader.advance(<Self as $crate::private::ShaderType>::METADATA.col_padding() as ::core::primitive::usize);
col
});

let columns = $crate::private::CreateFrom::create_from(reader);
$crate::private::FromMatrixParts::<$el_ty, $c, $r>::from_parts(columns)
}
}
Expand Down

0 comments on commit 4ee8377

Please sign in to comment.