Browse Source

fix: uneeded usages of transmute

pull/43/head
Natasha England-Elbro 11 months ago
parent
commit
12d62b3a41
No known key found for this signature in database
GPG Key ID: 46F323AE9236FD6A
  1. 5
      citro3d/src/math/fvec.rs
  2. 15
      citro3d/src/math/matrix.rs

5
citro3d/src/math/fvec.rs

@ -55,6 +55,11 @@ impl FVec4 {
unsafe { self.0.__bindgen_anon_1.w } unsafe { self.0.__bindgen_anon_1.w }
} }
/// Wrap a raw [`citro3d_sys::C3D_FVec`]
pub fn from_raw(raw: citro3d_sys::C3D_FVec) -> Self {
Self(raw)
}
/// Create a new [`FVec4`] from its components. /// Create a new [`FVec4`] from its components.
/// ///
/// # Example /// # Example

15
citro3d/src/math/matrix.rs

@ -26,8 +26,7 @@ impl Matrix4 {
/// Construct a Matrix4 from its rows /// Construct a Matrix4 from its rows
pub fn from_rows(rows: [FVec4; 4]) -> Self { pub fn from_rows(rows: [FVec4; 4]) -> Self {
Self(citro3d_sys::C3D_Mtx { Self(citro3d_sys::C3D_Mtx {
// Safety: FVec is repr(transparent) r: rows.map(|r| r.0),
r: unsafe { core::mem::transmute::<_, [citro3d_sys::C3D_FVec; 4]>(rows) },
}) })
} }
/// Create a new matrix from a raw citro3d_sys one /// Create a new matrix from a raw citro3d_sys one
@ -49,20 +48,12 @@ impl Matrix4 {
/// Get the rows in raw (WZYX) form /// Get the rows in raw (WZYX) form
pub fn rows_wzyx(self) -> [FVec4; 4] { pub fn rows_wzyx(self) -> [FVec4; 4] {
// Safety: FVec4 is repr(C) to allow transmute from C3D_Vec unsafe { self.0.r }.map(FVec4::from_raw)
unsafe { core::mem::transmute::<[citro3d_sys::C3D_FVec; 4], [FVec4; 4]>(self.0.r) }
} }
/// Get the rows in XYZW form /// Get the rows in XYZW form
pub fn rows_xyzw(self) -> [[f32; 4]; 4] { pub fn rows_xyzw(self) -> [[f32; 4]; 4] {
let mut rows = self.rows_wzyx(); self.rows_wzyx().map(|r| [r.x(), r.y(), r.z(), r.w()])
for r in &mut rows {
unsafe {
r.0.c.reverse();
}
}
// Safety: FVec has same layout as citro3d_sys::C3D_FVec which is a union with [f32; 4] as one variant
unsafe { std::mem::transmute::<_, [[f32; 4]; 4]>(rows) }
} }
/// Construct the zero matrix. /// Construct the zero matrix.
#[doc(alias = "Mtx_Zeros")] #[doc(alias = "Mtx_Zeros")]

Loading…
Cancel
Save