From 92c37b00b7808413cf0470303c092d915a1cfa77 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Mon, 31 Jan 2022 22:46:56 -0500 Subject: [PATCH 1/7] Add translations of result macros + debug impl --- ctru-rs/src/error.rs | 16 ++++++++++++++-- ctru-sys/src/lib.rs | 11 +++++++---- ctru-sys/src/result.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 ctru-sys/src/result.rs diff --git a/ctru-rs/src/error.rs b/ctru-rs/src/error.rs index b1d968b..bd9c222 100644 --- a/ctru-rs/src/error.rs +++ b/ctru-rs/src/error.rs @@ -1,6 +1,8 @@ use std::error; use std::fmt; +use ctru_sys::result::{R_DESCRIPTION, R_LEVEL, R_MODULE, R_SUMMARY}; + pub type Result = ::std::result::Result; /// The error type returned by all libctru functions. @@ -17,12 +19,22 @@ impl From for Error { impl fmt::Debug for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Error::Os(err) => write!(f, "libctru result code: {:08X}", err), + Error::Os(err) => { + write!(f, "libctru result code: {:08X}:", err)?; + f.debug_struct("") + .field("description", &R_DESCRIPTION(err)) + .field("module", &R_MODULE(err)) + .field("summary", &R_SUMMARY(err)) + .field("level", &R_LEVEL(err)) + .finish() + } } } } -// TODO: Expand libctru result code into human-readable error message +// TODO: Expand libctru result code into human-readable error message. These should be useful: +// https://www.3dbrew.org/wiki/Error_codes +// https://github.com/devkitPro/libctru/blob/master/libctru/include/3ds/result.h impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { diff --git a/ctru-sys/src/lib.rs b/ctru-sys/src/lib.rs index d91d887..454a380 100644 --- a/ctru-sys/src/lib.rs +++ b/ctru-sys/src/lib.rs @@ -1,9 +1,12 @@ #![no_std] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(clippy::all)] + +pub mod result; -#[allow(non_upper_case_globals)] -#[allow(non_camel_case_types)] -#[allow(non_snake_case)] -#[allow(clippy::all)] mod bindings; pub use bindings::*; +pub use result::*; diff --git a/ctru-sys/src/result.rs b/ctru-sys/src/result.rs new file mode 100644 index 0000000..5f54694 --- /dev/null +++ b/ctru-sys/src/result.rs @@ -0,0 +1,42 @@ +//! Ports of macros in +//! + +use crate::Result; + +/// Checks whether a result code indicates success. +pub fn R_SUCCEEDED(res: Result) -> bool { + res >= 0 +} + +/// Checks whether a result code indicates failure. +pub fn R_FAILED(res: Result) -> bool { + res < 0 +} + +/// Returns the level of a result code. +pub fn R_LEVEL(res: Result) -> Result { + (res >> 27) & 0x1F +} + +/// Returns the summary of a result code. +pub fn R_SUMMARY(res: Result) -> Result { + (res >> 21) & 0x3F +} + +/// Returns the module ID of a result code. +pub fn R_MODULE(res: Result) -> Result { + (res >> 10) & 0xFF +} + +/// Returns the description of a result code. +pub fn R_DESCRIPTION(res: Result) -> Result { + res & 0x3FF +} + +/// Builds a result code from its constituent components. +pub fn MAKERESULT(level: Result, summary: Result, module: Result, description: Result) -> Result { + ((level & 0x1F) << 27) + | ((summary & 0x3F) << 21) + | ((module & 0xFF) << 10) + | (description & 0x3FF) +} From aebf347a001835bb486434073e43216d21619d43 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Mon, 31 Jan 2022 20:38:13 -0800 Subject: [PATCH 2/7] Fix clippy warning about unused screen field --- ctru-rs/src/console.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index ec03a3d..ddd4fe8 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -9,7 +9,7 @@ static mut EMPTY_CONSOLE: PrintConsole = unsafe { const_zero::const_zero!(PrintC pub struct Console<'screen> { context: Box, - screen: RefMut<'screen, dyn Screen>, + _screen: RefMut<'screen, dyn Screen>, } impl<'screen> Console<'screen> { @@ -21,7 +21,10 @@ impl<'screen> Console<'screen> { unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; - Console { context, screen } + Console { + context, + _screen: screen, + } } /// Returns true if a valid Console to print on is selected From 0326e9ea9d1b7906e7025d03d6d5297ff3089461 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Tue, 1 Feb 2022 08:24:07 -0500 Subject: [PATCH 3/7] Address review comments for Debug impl --- ctru-rs/src/error.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ctru-rs/src/error.rs b/ctru-rs/src/error.rs index bd9c222..53c3fbe 100644 --- a/ctru-rs/src/error.rs +++ b/ctru-rs/src/error.rs @@ -6,12 +6,13 @@ use ctru_sys::result::{R_DESCRIPTION, R_LEVEL, R_MODULE, R_SUMMARY}; pub type Result = ::std::result::Result; /// The error type returned by all libctru functions. +#[non_exhaustive] pub enum Error { - Os(i32), + Os(ctru_sys::Result), } -impl From for Error { - fn from(err: i32) -> Self { +impl From for Error { + fn from(err: ctru_sys::Result) -> Self { Error::Os(err) } } @@ -19,15 +20,14 @@ impl From for Error { impl fmt::Debug for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Error::Os(err) => { - write!(f, "libctru result code: {:08X}:", err)?; - f.debug_struct("") - .field("description", &R_DESCRIPTION(err)) - .field("module", &R_MODULE(err)) - .field("summary", &R_SUMMARY(err)) - .field("level", &R_LEVEL(err)) - .finish() - } + Error::Os(err) => f + .debug_struct("Error") + .field("raw", &format_args!("{:#08X}", err)) + .field("description", &R_DESCRIPTION(err)) + .field("module", &R_MODULE(err)) + .field("summary", &R_SUMMARY(err)) + .field("level", &R_LEVEL(err)) + .finish(), } } } From c59de27f853dc69bb8087cad074f06bba591d444 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Tue, 1 Feb 2022 18:37:17 +0100 Subject: [PATCH 4/7] Removed link field in ctru-sys --- ctru-rs/examples/hello-world.rs | 22 ++++++++++++++-------- ctru-sys/Cargo.toml | 1 - 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/ctru-rs/examples/hello-world.rs b/ctru-rs/examples/hello-world.rs index 930549f..d617223 100644 --- a/ctru-rs/examples/hello-world.rs +++ b/ctru-rs/examples/hello-world.rs @@ -12,16 +12,22 @@ fn main() { let apt = Apt::init().expect("Couldn't obtain APT controller"); let _console = Console::init(gfx.top_screen.borrow_mut()); - let out = b"Hello fellow Rustaceans, I'm on the Nintendo 3DS!"; - let width = 24; - - let mut writer = BufWriter::new(Vec::new()); - ferris_says::say(out, width, &mut writer).unwrap(); + struct Timespec { + t: libc::timespec, + } + //let inst = std::time::Instant::now(); + let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } }; + let res = unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut t.t) }; + println!("{{tv_sec: {}, tv_nsec: {} }} {} clock:{}", t.t.tv_sec, t.t.tv_nsec, res, libc::CLOCK_MONOTONIC); + ctru::thread::sleep(std::time::Duration::from_secs(2)); + /*let ela = inst.elapsed(); println!( - "\x1b[0;0H{}", - String::from_utf8_lossy(&writer.into_inner().unwrap()) - ); + "\x1b[0;0HElapsed: {:#?}", + ela + );*/ + + // Main loop while apt.main_loop() { diff --git a/ctru-sys/Cargo.toml b/ctru-sys/Cargo.toml index 8c059bb..46bed0b 100644 --- a/ctru-sys/Cargo.toml +++ b/ctru-sys/Cargo.toml @@ -3,7 +3,6 @@ name = "ctru-sys" version = "0.4.0" authors = ["Ronald Kinard "] license = "https://en.wikipedia.org/wiki/Zlib_License" -links = "ctru" edition = "2021" [dependencies] From 8835951f7d15468579026255d86c26a45c553f6e Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Mon, 31 Jan 2022 20:38:13 -0800 Subject: [PATCH 5/7] Fix clippy warning about unused screen field --- ctru-rs/src/console.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index ec03a3d..ddd4fe8 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -9,7 +9,7 @@ static mut EMPTY_CONSOLE: PrintConsole = unsafe { const_zero::const_zero!(PrintC pub struct Console<'screen> { context: Box, - screen: RefMut<'screen, dyn Screen>, + _screen: RefMut<'screen, dyn Screen>, } impl<'screen> Console<'screen> { @@ -21,7 +21,10 @@ impl<'screen> Console<'screen> { unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; - Console { context, screen } + Console { + context, + _screen: screen, + } } /// Returns true if a valid Console to print on is selected From fbd873432c8512236eb0936ea8952795e1b63a5c Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Tue, 1 Feb 2022 19:28:27 +0100 Subject: [PATCH 6/7] Revert "Removed link field in ctru-sys" This reverts commit c59de27f853dc69bb8087cad074f06bba591d444. --- ctru-rs/examples/hello-world.rs | 22 ++++++++-------------- ctru-sys/Cargo.toml | 1 + 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/ctru-rs/examples/hello-world.rs b/ctru-rs/examples/hello-world.rs index d617223..930549f 100644 --- a/ctru-rs/examples/hello-world.rs +++ b/ctru-rs/examples/hello-world.rs @@ -12,22 +12,16 @@ fn main() { let apt = Apt::init().expect("Couldn't obtain APT controller"); let _console = Console::init(gfx.top_screen.borrow_mut()); - struct Timespec { - t: libc::timespec, - } - - //let inst = std::time::Instant::now(); - let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } }; - let res = unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut t.t) }; - println!("{{tv_sec: {}, tv_nsec: {} }} {} clock:{}", t.t.tv_sec, t.t.tv_nsec, res, libc::CLOCK_MONOTONIC); - ctru::thread::sleep(std::time::Duration::from_secs(2)); - /*let ela = inst.elapsed(); - println!( - "\x1b[0;0HElapsed: {:#?}", - ela - );*/ + let out = b"Hello fellow Rustaceans, I'm on the Nintendo 3DS!"; + let width = 24; + let mut writer = BufWriter::new(Vec::new()); + ferris_says::say(out, width, &mut writer).unwrap(); + println!( + "\x1b[0;0H{}", + String::from_utf8_lossy(&writer.into_inner().unwrap()) + ); // Main loop while apt.main_loop() { diff --git a/ctru-sys/Cargo.toml b/ctru-sys/Cargo.toml index 46bed0b..8c059bb 100644 --- a/ctru-sys/Cargo.toml +++ b/ctru-sys/Cargo.toml @@ -3,6 +3,7 @@ name = "ctru-sys" version = "0.4.0" authors = ["Ronald Kinard "] license = "https://en.wikipedia.org/wiki/Zlib_License" +links = "ctru" edition = "2021" [dependencies] From 77f673e9d995c0d29d9034337163ac0ef7a33484 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Tue, 1 Feb 2022 21:08:49 +0100 Subject: [PATCH 7/7] Removed link field in ctru-sys --- ctru-sys/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/ctru-sys/Cargo.toml b/ctru-sys/Cargo.toml index 8c059bb..46bed0b 100644 --- a/ctru-sys/Cargo.toml +++ b/ctru-sys/Cargo.toml @@ -3,7 +3,6 @@ name = "ctru-sys" version = "0.4.0" authors = ["Ronald Kinard "] license = "https://en.wikipedia.org/wiki/Zlib_License" -links = "ctru" edition = "2021" [dependencies]