From 67b15b928a47b9e660e2a79cd10f03466db88eb2 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Thu, 13 Jan 2022 19:37:02 -0800 Subject: [PATCH] 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) };