From a0aa6d5d9e87ef851ce2a07c2f6c6b9a2093c5a5 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Wed, 14 Feb 2024 16:53:16 -0700 Subject: [PATCH] Wrap PrintConsole in UnsafeCell --- ctru-rs/src/console.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 9fedd2a..bee9dc5 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -5,7 +5,7 @@ //! //! Have a look at [`Soc::redirect_to_3dslink()`](crate::services::soc::Soc::redirect_to_3dslink) for a better alternative when debugging applications. -use std::cell::RefMut; +use std::cell::{RefMut, UnsafeCell}; use std::default::Default; use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole}; @@ -63,7 +63,7 @@ impl ConsoleScreen for S {} /// More info in the [`cargo-3ds` docs](https://github.com/rust3ds/cargo-3ds#running-executables). #[doc(alias = "PrintConsole")] pub struct Console<'screen> { - context: Box, + context: Box>, screen: RefMut<'screen, dyn ConsoleScreen>, } @@ -107,9 +107,9 @@ impl<'screen> Console<'screen> { /// ``` #[doc(alias = "consoleInit")] pub fn new(screen: RefMut<'screen, S>) -> Self { - let mut context = Box::::default(); + let context = Box::>::default(); - unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; + unsafe { consoleInit(screen.as_raw(), context.get()) }; Console { context, screen } } @@ -190,7 +190,7 @@ impl<'screen> Console<'screen> { #[doc(alias = "consoleSelect")] pub fn select(&self) { unsafe { - consoleSelect(self.context.as_ref() as *const _ as *mut _); + consoleSelect(self.context.get()); } } @@ -248,7 +248,7 @@ impl<'screen> Console<'screen> { unsafe { consoleSetWindow( - self.context.as_mut(), + self.context.get(), x.into(), y.into(), width.into(), @@ -338,7 +338,10 @@ impl Swap for Console<'_> { /// This should be called once per frame at most. fn swap_buffers(&mut self) { self.screen.swap_buffers(); - self.context.frameBuffer = self.screen.raw_framebuffer().ptr as *mut u16; + + unsafe { + (*self.context.get()).frameBuffer = self.screen.raw_framebuffer().ptr as *mut u16 + }; } fn set_double_buffering(&mut self, enabled: bool) { @@ -366,7 +369,7 @@ impl Drop for Console<'_> { // Get the current console by replacing it with an empty one. let current_console = ctru_sys::consoleSelect(std::ptr::addr_of_mut!(EMPTY_CONSOLE)); - if std::ptr::eq(current_console, &*self.context) { + if std::ptr::eq(current_console, self.context.get()) { // Console dropped while selected. We just replaced it with the // empty console so nothing more to do. } else {