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]