From 6ba85149d84aa3bcc879ec16ef79079dc411643f Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sun, 3 Jul 2022 17:02:44 -0400 Subject: [PATCH] Use bytemuck instead of incorrect align_to Std docs say that prorgams should not rely on `align_to` to return empty prefix/suffix for correctess, only performance. `bytemuck` on the other hand deals with alignment for correctness so let's use that. --- citro3d/Cargo.toml | 1 + citro3d/src/shader.rs | 16 +++++----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/citro3d/Cargo.toml b/citro3d/Cargo.toml index 8be434a..8bdaf3f 100644 --- a/citro3d/Cargo.toml +++ b/citro3d/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] bitflags = "1.3.2" +bytemuck = { version = "1.10.0", features = ["extern_crate_std"] } citro3d-sys = { git = "https://github.com/ian-h-chamberlain/citro3d-rs.git" } ctru-rs = { git = "https://github.com/Meziu/ctru-rs.git" } libc = "0.2.125" diff --git a/citro3d/src/shader.rs b/citro3d/src/shader.rs index 520eec1..d66a924 100644 --- a/citro3d/src/shader.rs +++ b/citro3d/src/shader.rs @@ -98,22 +98,16 @@ impl Library { /// An error is returned if the input data does not have an alignment of 4 /// (cannot be safely converted to `&[u32]`). pub fn from_bytes(bytes: &[u8]) -> Result> { - unsafe { - let (prefix, aligned, suffix) = bytes.align_to::(); - if !prefix.is_empty() || !suffix.is_empty() { - // Align is incorrect, we don't want to drop any data - // TODO fill in error details - return Err("uh oh".into()); - } - - Ok(Self(citro3d_sys::DVLB_ParseFile( + let aligned: &[u32] = bytemuck::try_cast_slice(bytes)?; + Ok(Self(unsafe { + citro3d_sys::DVLB_ParseFile( // SAFETY: we're trusting the parse implementation doesn't mutate // the contents of the data. From a quick read it looks like that's // correct and it should just take a const arg in the API. aligned.as_ptr() as *mut _, aligned.len().try_into()?, - ))) - } + ) + })) } #[must_use]