Skip to content

Commit

Permalink
Cleanup unsafe for vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
james7132 committed Mar 28, 2024
1 parent 71a0cbd commit b31fe1b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 82 deletions.
8 changes: 2 additions & 6 deletions src/types/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,13 @@ macro_rules! impl_matrix_inner {
#[cfg(target_endian = "little")]
{
// Const branch, should be eliminated at compile time.
if <Self as $crate::private::ShaderType>::METADATA.has_internal_padding() {
if <Self as $crate::private::ShaderType>::METADATA.col_padding() != 0 {
for col in columns {
$crate::private::WriteInto::write_into(col, writer);
writer.advance(<Self as $crate::private::ShaderType>::METADATA.col_padding() as ::core::primitive::usize);
}
} else {
let ptr = (self as *const Self) as *const ::core::primitive::u8;
let byte_slice: &[::core::primitive::u8] = unsafe {
::core::slice::from_raw_parts(ptr, ::core::mem::size_of::<Self>())
};
writer.write_slice(byte_slice);
$crate::private::WriteInto::write_into(columns, writer);
}
}
#[cfg(not(target_endian = "little"))]
Expand Down
82 changes: 6 additions & 76 deletions src/types/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,8 @@ macro_rules! impl_vector_inner {
{
#[inline]
fn write_into<B: $crate::private::BufferMut>(&self, writer: &mut $crate::private::Writer<B>) {
#[cfg(target_endian = "little")]
{
// Const branch, should be eliminated at compile time.
if <Self as $crate::private::ShaderType>::METADATA.has_internal_padding() {
let elements = $crate::private::AsRefVectorParts::<$el_ty, $n>::as_ref_parts(self);
for el in elements {
$crate::private::WriteInto::write_into(el, writer);
}
} else {
let ptr: *const Self = self;
let ptr = ptr.cast::<::core::primitive::u8>();
let byte_slice: &[::core::primitive::u8] = unsafe {
::core::slice::from_raw_parts(ptr, ::core::mem::size_of::<Self>())
};
writer.write_slice(byte_slice);
}
}
#[cfg(not(target_endian = "little"))]
{
let elements = $crate::private::AsRefVectorParts::<$el_ty, $n>::as_ref_parts(self);
for el in elements {
$crate::private::WriteInto::write_into(el, writer);
}
}
let elements = $crate::private::AsRefVectorParts::<$el_ty, $n>::as_ref_parts(self);
$crate::private::WriteInto::write_into(elements, writer);
}
}

Expand All @@ -175,30 +153,8 @@ macro_rules! impl_vector_inner {
{
#[inline]
fn read_from<B: $crate::private::BufferRef>(&mut self, reader: &mut $crate::private::Reader<B>) {
#[cfg(target_endian = "little")]
{
// Const branch, should be eliminated at compile time.
if <Self as $crate::private::ShaderType>::METADATA.has_internal_padding() {
let elements = $crate::private::AsMutVectorParts::<$el_ty, $n>::as_mut_parts(self);
for el in elements {
$crate::private::ReadFrom::read_from(el, reader);
}
} else {
let ptr: *mut Self = self;
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);
}
}
#[cfg(not(target_endian = "little"))]
{
let elements = $crate::private::AsRefVectorParts::<$el_ty, $n>::as_ref_parts(self);
for el in elements {
$crate::private::ReadFrom::read_from(el, reader);
}
}
let elements = $crate::private::AsMutVectorParts::<$el_ty, $n>::as_mut_parts(self);
$crate::private::ReadFrom::read_from(elements, reader);
}
}

Expand All @@ -210,34 +166,8 @@ macro_rules! impl_vector_inner {
#[inline]
#[allow(trivial_casts)]
fn create_from<B: $crate::private::BufferRef>(reader: &mut $crate::private::Reader<B>) -> Self {
#[cfg(target_endian = "little")]
{
// Const branch, should be eliminated at compile time.
if <Self as $crate::private::ShaderType>::METADATA.has_internal_padding() {
let elements = ::core::array::from_fn(|_| {
$crate::private::CreateFrom::create_from(reader)
});

$crate::private::FromVectorParts::<$el_ty, $n>::from_parts(elements)
} else {
let mut me = ::core::mem::MaybeUninit::zeroed();
let ptr = (&mut me as *mut ::core::mem::MaybeUninit<Self>).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() }
}
}
#[cfg(not(target_endian = "little"))]
{
let elements = ::core::array::from_fn(|_| {
$crate::private::CreateFrom::create_from(reader)
});

$crate::private::FromVectorParts::<$el_ty, $n>::from_parts(elements)
}
let elements = $crate::private::CreateFrom::create_from(reader);
$crate::private::FromVectorParts::<$el_ty, $n>::from_parts(elements)
}
}
};
Expand Down

0 comments on commit b31fe1b

Please sign in to comment.