From 67b15b928a47b9e660e2a79cd10f03466db88eb2 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Thu, 13 Jan 2022 19:37:02 -0800 Subject: [PATCH 1/3] Tie the lifetime of Gfx to Console This prevents dropping the Gfx object before the console. We want to do this because printing after dropping gfx doesn't do anything (gfx isn't rendering the text anymore). Note that even though Rust doesn't understand the relationship between Console and println, even if you don't use the console variable Rust will prevent Gfx dropping because Console's Drop code _might_ use the Gfx object (it doesn't, but we get the error we want anyway). --- ctru-rs/src/console.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 986a7e2..0c86129 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -1,23 +1,28 @@ use std::default::Default; +use std::marker::PhantomData; use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole}; use crate::gfx::{Gfx, Screen}; -pub struct Console { +pub struct Console<'gfx> { context: Box, + _gfx: PhantomData<&'gfx ()>, } -impl Console { +impl<'gfx> Console<'gfx> { /// Initialize a console on the chosen screen, overwriting whatever was on the screen /// previously (including other consoles). The new console is automatically selected for /// printing. - pub fn init(_gfx: &Gfx, screen: Screen) -> Self { + pub fn init(_gfx: &'gfx Gfx, screen: Screen) -> Self { let mut context = Box::new(PrintConsole::default()); unsafe { consoleInit(screen.into(), context.as_mut()) }; - Console { context } + Console { + context, + _gfx: PhantomData, + } } /// Select this console as the current target for stdout @@ -45,7 +50,7 @@ impl Console { } } -impl Drop for Console { +impl Drop for Console<'_> { fn drop(&mut self) { static mut EMPTY_CONSOLE: PrintConsole = unsafe { const_zero::const_zero!(PrintConsole) }; From 1c5c72477cf28cbf163a730ab6289a2051230beb Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Thu, 13 Jan 2022 19:37:58 -0800 Subject: [PATCH 2/3] Import Screen in examples to improve consistency --- ctru-rs/examples/buttons.rs | 4 ++-- ctru-rs/examples/gfx_wide_mode.rs | 2 +- ctru-rs/examples/hello-both-screens.rs | 4 ++-- ctru-rs/examples/hello-world.rs | 4 ++-- ctru-rs/examples/software-keyboard.rs | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ctru-rs/examples/buttons.rs b/ctru-rs/examples/buttons.rs index 4c7fd60..36127cd 100644 --- a/ctru-rs/examples/buttons.rs +++ b/ctru-rs/examples/buttons.rs @@ -1,5 +1,5 @@ use ctru::console::Console; -use ctru::gfx::Gfx; +use ctru::gfx::{Gfx, Screen}; use ctru::services::apt::Apt; use ctru::services::hid::{Hid, KeyPad}; @@ -9,7 +9,7 @@ fn main() { let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); let gfx = Gfx::default(); - let console = Console::init(&gfx, ctru::gfx::Screen::Top); + let console = Console::init(&gfx, Screen::Top); println!("Hi there! Try pressing a button"); println!("\x1b[29;16HPress Start to exit"); diff --git a/ctru-rs/examples/gfx_wide_mode.rs b/ctru-rs/examples/gfx_wide_mode.rs index 8572f33..f4ba365 100644 --- a/ctru-rs/examples/gfx_wide_mode.rs +++ b/ctru-rs/examples/gfx_wide_mode.rs @@ -11,7 +11,7 @@ fn main() { let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); let gfx = Gfx::default(); - let _console = Console::init(&gfx, ctru::gfx::Screen::Top); + let _console = Console::init(&gfx, Screen::Top); println!("Press A to enable/disable wide screen mode."); diff --git a/ctru-rs/examples/hello-both-screens.rs b/ctru-rs/examples/hello-both-screens.rs index 22a1783..180b1b2 100644 --- a/ctru-rs/examples/hello-both-screens.rs +++ b/ctru-rs/examples/hello-both-screens.rs @@ -11,11 +11,11 @@ fn main() { let gfx = Gfx::default(); // Start a console on the top screen - let top_screen = Console::init(&gfx, ctru::gfx::Screen::Top); + let top_screen = Console::init(&gfx, Screen::Top); // Start a console on the bottom screen. // The most recently initialized console will be active by default - let bottom_screen = Console::init(&gfx, ctru::gfx::Screen::Bottom); + let bottom_screen = Console::init(&gfx, Screen::Bottom); // Let's print on the top screen first top_screen.select(); diff --git a/ctru-rs/examples/hello-world.rs b/ctru-rs/examples/hello-world.rs index 7f76266..ccebef7 100644 --- a/ctru-rs/examples/hello-world.rs +++ b/ctru-rs/examples/hello-world.rs @@ -1,5 +1,5 @@ use ctru::console::Console; -use ctru::gfx::Gfx; +use ctru::gfx::{Gfx, Screen}; use ctru::services::apt::Apt; use ctru::services::hid::{Hid, KeyPad}; @@ -21,7 +21,7 @@ fn main() { // Initialize a ctrulib console and direct standard output to it. // Consoles can be initialized on both the top and bottom screens. - let _console = Console::init(&gfx, ctru::gfx::Screen::Top); + let _console = Console::init(&gfx, Screen::Top); // Now we can print to stdout! println!("Hello, world!"); diff --git a/ctru-rs/examples/software-keyboard.rs b/ctru-rs/examples/software-keyboard.rs index 495f9c1..b3b9347 100644 --- a/ctru-rs/examples/software-keyboard.rs +++ b/ctru-rs/examples/software-keyboard.rs @@ -1,6 +1,6 @@ use ctru::applets::swkbd::{Button, Swkbd}; use ctru::console::Console; -use ctru::gfx::Gfx; +use ctru::gfx::{Gfx, Screen}; use ctru::services::apt::Apt; use ctru::services::hid::{Hid, KeyPad}; @@ -9,7 +9,7 @@ fn main() { let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); let gfx = Gfx::default(); - let _console = Console::init(&gfx, ctru::gfx::Screen::Top); + let _console = Console::init(&gfx, Screen::Top); println!("Press A to enter some text or press Start to quit"); From 6b5d420ed2e9db495ac4791a1547805b427ce4db Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Tue, 18 Jan 2022 09:37:01 -0500 Subject: [PATCH 3/3] Use a module instead of include! Modules are better understood by rust-analyzer compared to macros expansion, and we can still use the same bindgen script and lint attrs. --- ctru-sys/src/lib.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ctru-sys/src/lib.rs b/ctru-sys/src/lib.rs index dc32370..d91d887 100644 --- a/ctru-sys/src/lib.rs +++ b/ctru-sys/src/lib.rs @@ -1,7 +1,9 @@ -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] -#![allow(clippy::all)] #![no_std] -include!("bindings.rs"); +#[allow(non_upper_case_globals)] +#[allow(non_camel_case_types)] +#[allow(non_snake_case)] +#[allow(clippy::all)] +mod bindings; + +pub use bindings::*;