From 1383e753eeebe5c0378bc5443c7dd44d96bf5d8e Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 31 Mar 2023 23:46:20 +0200 Subject: [PATCH] Inequivocable From impls --- ctru-rs/src/applets/swkbd.rs | 8 +++---- ctru-rs/src/lib.rs | 38 ++++---------------------------- ctru-rs/src/services/cam.rs | 20 ++++++++--------- ctru-rs/src/services/cfgu.rs | 6 ++--- ctru-rs/src/services/fs.rs | 6 ++--- ctru-rs/src/services/gfx.rs | 7 +++--- ctru-rs/src/services/gspgpu.rs | 4 ++-- ctru-rs/src/services/ndsp/mod.rs | 6 ++--- ctru-rs/src/services/ps.rs | 4 ++-- 9 files changed, 35 insertions(+), 64 deletions(-) diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 5153c25..b5aaaf1 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -213,7 +213,7 @@ impl Default for Swkbd { } } -from_type_to_u32!(Kind); -from_type_to_u32!(Button); -from_type_to_u32!(Error); -from_type_to_i32!(ValidInput); +from_impl!(Kind, ctru_sys::SwkbdType); +from_impl!(Button, ctru_sys::SwkbdButton); +from_impl!(Error, ctru_sys::SwkbdResult); +from_impl!(ValidInput, i32); diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index a841e78..e733d5a 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -15,41 +15,11 @@ extern crate pthread_3ds; #[cfg(feature = "big-stack")] static __stacksize__: usize = 2 * 1024 * 1024; // 2MB -macro_rules! from_type_to_u8 { - ($from_type:ty) => { - impl From<$from_type> for u8 { +macro_rules! from_impl { + ($from_type:ty, $into_type:ty) => { + impl From<$from_type> for $into_type { fn from(v: $from_type) -> Self { - v as u8 - } - } - }; -} - -macro_rules! from_type_to_u16 { - ($from_type:ty) => { - impl From<$from_type> for u16 { - fn from(v: $from_type) -> Self { - v as u16 - } - } - }; -} - -macro_rules! from_type_to_u32 { - ($from_type:ty) => { - impl From<$from_type> for u32 { - fn from(v: $from_type) -> Self { - v as u32 - } - } - }; -} - -macro_rules! from_type_to_i32 { - ($from_type:ty) => { - impl From<$from_type> for i32 { - fn from(v: $from_type) -> Self { - v as i32 + v as $into_type } } }; diff --git a/ctru-rs/src/services/cam.rs b/ctru-rs/src/services/cam.rs index bca6c65..5046dec 100644 --- a/ctru-rs/src/services/cam.rs +++ b/ctru-rs/src/services/cam.rs @@ -794,13 +794,13 @@ impl Drop for Cam { } } -from_type_to_u32!(FlipMode); -from_type_to_u32!(ViewSize); -from_type_to_u32!(FrameRate); -from_type_to_u32!(WhiteBalance); -from_type_to_u32!(PhotoMode); -from_type_to_u32!(Effect); -from_type_to_u32!(Contrast); -from_type_to_u32!(LensCorrection); -from_type_to_u32!(OutputFormat); -from_type_to_u32!(ShutterSound); +from_impl!(FlipMode, ctru_sys::CAMU_Flip); +from_impl!(ViewSize, ctru_sys::CAMU_Size); +from_impl!(FrameRate, ctru_sys::CAMU_FrameRate); +from_impl!(WhiteBalance, ctru_sys::CAMU_WhiteBalance); +from_impl!(PhotoMode, ctru_sys::CAMU_PhotoMode); +from_impl!(Effect, ctru_sys::CAMU_Effect); +from_impl!(Contrast, ctru_sys::CAMU_Contrast); +from_impl!(LensCorrection, ctru_sys::CAMU_LensCorrection); +from_impl!(OutputFormat, ctru_sys::CAMU_OutputFormat); +from_impl!(ShutterSound, ctru_sys::CAMU_ShutterSoundType); diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 12857c6..3f268c7 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -115,9 +115,9 @@ impl Drop for Cfgu { } } -from_type_to_u8!(Region); -from_type_to_u8!(Language); -from_type_to_u8!(SystemModel); +from_impl!(Region, u8); +from_impl!(Language, u8); +from_impl!(SystemModel, u8); impl TryFrom for Region { type Error = (); diff --git a/ctru-rs/src/services/fs.rs b/ctru-rs/src/services/fs.rs index 512debb..45f9efb 100644 --- a/ctru-rs/src/services/fs.rs +++ b/ctru-rs/src/services/fs.rs @@ -1018,6 +1018,6 @@ impl Drop for Dir { } } -from_type_to_u32!(FsMediaType); -from_type_to_u32!(PathType); -from_type_to_u32!(ArchiveID); +from_impl!(FsMediaType, ctru_sys::FS_MediaType); +from_impl!(PathType, ctru_sys::FS_PathType); +from_impl!(ArchiveID, ctru_sys::FS_ArchiveID); diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index f4a580a..40d4740 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -104,14 +104,15 @@ pub struct RawFrameBuffer<'screen> { } #[derive(Copy, Clone, Debug)] +#[repr(u32)] /// Side of top screen framebuffer /// /// The top screen of the 3DS can have two separate sets of framebuffers to support its 3D functionality pub enum Side { /// The left framebuffer. This framebuffer is also the one used when 3D is disabled - Left, + Left = ctru_sys::GFX_LEFT, /// The right framebuffer - Right, + Right = ctru_sys::GFX_RIGHT, } /// A handle to libctru's gfx module. This module is a wrapper around the GSPGPU service that @@ -283,7 +284,7 @@ impl Screen for BottomScreen { } } -from_type_to_u32!(Side); +from_impl!(Side, ctru_sys::gfx3dSide_t); #[cfg(test)] mod tests { diff --git a/ctru-rs/src/services/gspgpu.rs b/ctru-rs/src/services/gspgpu.rs index 29ca102..234c088 100644 --- a/ctru-rs/src/services/gspgpu.rs +++ b/ctru-rs/src/services/gspgpu.rs @@ -65,5 +65,5 @@ impl From for FramebufferFormat { } } -from_type_to_u32!(FramebufferFormat); -from_type_to_u32!(Event); +from_impl!(FramebufferFormat, ctru_sys::GSPGPU_FramebufferFormat); +from_impl!(Event, ctru_sys::GSPGPU_Event); diff --git a/ctru-rs/src/services/ndsp/mod.rs b/ctru-rs/src/services/ndsp/mod.rs index 832e586..5ab9e20 100644 --- a/ctru-rs/src/services/ndsp/mod.rs +++ b/ctru-rs/src/services/ndsp/mod.rs @@ -341,6 +341,6 @@ impl Drop for Ndsp { } } -from_type_to_u32!(InterpolationType); -from_type_to_u32!(OutputMode); -from_type_to_u16!(AudioFormat); +from_impl!(InterpolationType, ctru_sys::ndspInterpType); +from_impl!(OutputMode, ctru_sys::ndspOutputMode); +from_impl!(AudioFormat, u16); diff --git a/ctru-rs/src/services/ps.rs b/ctru-rs/src/services/ps.rs index 552a34e..a14f2c4 100644 --- a/ctru-rs/src/services/ps.rs +++ b/ctru-rs/src/services/ps.rs @@ -70,8 +70,8 @@ impl Drop for Ps { } } -from_type_to_u32!(AESAlgorithm); -from_type_to_u32!(AESKeyType); +from_impl!(AESAlgorithm, ctru_sys::PS_AESAlgorithm); +from_impl!(AESKeyType, ctru_sys::PS_AESKeyType); #[cfg(test)] mod tests {