Browse Source

Remove Console S generic by making ConsoleScreen trait alias

pull/86/head
AzureMarker 1 year ago
parent
commit
c6d8d8823f
  1. 3
      ctru-rs/examples/file-explorer.rs
  2. 11
      ctru-rs/examples/ir-user-circle-pad-pro.rs
  3. 18
      ctru-rs/src/console.rs
  4. 4
      ctru-rs/src/services/gfx.rs

3
ctru-rs/examples/file-explorer.rs

@ -6,7 +6,6 @@
use ctru::applets::swkbd::{Button, SoftwareKeyboard}; use ctru::applets::swkbd::{Button, SoftwareKeyboard};
use ctru::prelude::*; use ctru::prelude::*;
use ctru::services::gfx::TopScreen;
use std::fs::DirEntry; use std::fs::DirEntry;
use std::os::horizon::fs::MetadataExt; use std::os::horizon::fs::MetadataExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -27,7 +26,7 @@ struct FileExplorer<'a> {
apt: &'a Apt, apt: &'a Apt,
hid: &'a mut Hid, hid: &'a mut Hid,
gfx: &'a Gfx, gfx: &'a Gfx,
console: Console<'a, TopScreen>, console: Console<'a>,
path: PathBuf, path: PathBuf,
entries: Vec<DirEntry>, entries: Vec<DirEntry>,
running: bool, running: bool,

11
ctru-rs/examples/ir-user-circle-pad-pro.rs

@ -1,7 +1,7 @@
//! A demo of using the ir:USER service to connect to the Circle Pad Pro. //! A demo of using the ir:USER service to connect to the Circle Pad Pro.
use ctru::prelude::*; use ctru::prelude::*;
use ctru::services::gfx::{BottomScreen, Flush, Swap, TopScreen}; use ctru::services::gfx::{Flush, Swap};
use ctru::services::ir_user::{CirclePadProInputResponse, ConnectionStatus, IrDeviceId, IrUser}; use ctru::services::ir_user::{CirclePadProInputResponse, ConnectionStatus, IrDeviceId, IrUser};
use ctru::services::srv::HandleExt; use ctru::services::srv::HandleExt;
use ctru_sys::Handle; use ctru_sys::Handle;
@ -62,8 +62,8 @@ fn main() {
} }
struct CirclePadProDemo<'screen> { struct CirclePadProDemo<'screen> {
top_console: Console<'screen, TopScreen>, top_console: Console<'screen>,
bottom_console: Console<'screen, BottomScreen>, bottom_console: Console<'screen>,
ir_user: IrUser, ir_user: IrUser,
connection_status_event: Handle, connection_status_event: Handle,
receive_packet_event: Handle, receive_packet_event: Handle,
@ -75,10 +75,7 @@ enum ConnectionResult {
} }
impl<'screen> CirclePadProDemo<'screen> { impl<'screen> CirclePadProDemo<'screen> {
fn new( fn new(mut top_console: Console<'screen>, bottom_console: Console<'screen>) -> Self {
mut top_console: Console<'screen, TopScreen>,
bottom_console: Console<'screen, BottomScreen>,
) -> Self {
// Set up double buffering on top screen // Set up double buffering on top screen
top_console.set_double_buffering(true); top_console.set_double_buffering(true);
top_console.swap_buffers(); top_console.swap_buffers();

18
ctru-rs/src/console.rs

@ -39,6 +39,10 @@ pub enum Dimension {
Height, Height,
} }
/// A [`Screen`] that can be used as a target for [`Console`].
pub trait ConsoleScreen: Screen + Swap + Flush {}
impl<S: Screen + Swap + Flush> ConsoleScreen for S {}
/// Virtual text console. /// Virtual text console.
/// ///
/// [`Console`] lets the application redirect `stdout` and `stderr` to a simple text displayer on the 3DS screen. /// [`Console`] lets the application redirect `stdout` and `stderr` to a simple text displayer on the 3DS screen.
@ -58,12 +62,12 @@ pub enum Dimension {
/// you can try using [`Soc::redirect_to_3dslink`](crate::services::soc::Soc::redirect_to_3dslink) while activating the `--server` flag for `3dslink` (also supported by `cargo-3ds`). /// you can try using [`Soc::redirect_to_3dslink`](crate::services::soc::Soc::redirect_to_3dslink) while activating the `--server` flag for `3dslink` (also supported by `cargo-3ds`).
/// More info in the [`cargo-3ds` docs](https://github.com/rust3ds/cargo-3ds#running-executables). /// More info in the [`cargo-3ds` docs](https://github.com/rust3ds/cargo-3ds#running-executables).
#[doc(alias = "PrintConsole")] #[doc(alias = "PrintConsole")]
pub struct Console<'screen, S: Screen> { pub struct Console<'screen> {
context: Box<PrintConsole>, context: Box<PrintConsole>,
screen: RefMut<'screen, S>, screen: RefMut<'screen, dyn ConsoleScreen>,
} }
impl<'screen, S: Screen> Console<'screen, S> { impl<'screen> Console<'screen> {
/// Initialize a console on the chosen screen. /// Initialize a console on the chosen screen.
/// ///
/// # Notes /// # Notes
@ -102,7 +106,7 @@ impl<'screen, S: Screen> Console<'screen, S> {
/// # } /// # }
/// ``` /// ```
#[doc(alias = "consoleInit")] #[doc(alias = "consoleInit")]
pub fn new(screen: RefMut<'screen, S>) -> Self { pub fn new<S: ConsoleScreen>(screen: RefMut<'screen, S>) -> Self {
let mut context = Box::<PrintConsole>::default(); let mut context = Box::<PrintConsole>::default();
unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; unsafe { consoleInit(screen.as_raw(), context.as_mut()) };
@ -324,7 +328,7 @@ impl<'screen, S: Screen> Console<'screen, S> {
} }
} }
impl<S: Screen + Swap> Swap for Console<'_, S> { impl Swap for Console<'_> {
/// Swaps the video buffers. Note: The console's cursor position is not reset, only the framebuffer is changed. /// Swaps the video buffers. Note: The console's cursor position is not reset, only the framebuffer is changed.
/// ///
/// Even if double buffering is disabled, "swapping" the buffers has the side effect /// Even if double buffering is disabled, "swapping" the buffers has the side effect
@ -342,13 +346,13 @@ impl<S: Screen + Swap> Swap for Console<'_, S> {
} }
} }
impl<S: Screen + Flush> Flush for Console<'_, S> { impl Flush for Console<'_> {
fn flush_buffers(&mut self) { fn flush_buffers(&mut self) {
self.screen.flush_buffers(); self.screen.flush_buffers();
} }
} }
impl<S: Screen> Drop for Console<'_, S> { impl Drop for Console<'_> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
// Safety: We are about to deallocate the PrintConsole data pointed // Safety: We are about to deallocate the PrintConsole data pointed

4
ctru-rs/src/services/gfx.rs

@ -13,7 +13,7 @@ use crate::services::gspgpu::{self, FramebufferFormat};
use crate::services::ServiceReference; use crate::services::ServiceReference;
mod private { mod private {
use super::{BottomScreen, Screen, TopScreen, TopScreen3D, TopScreenLeft, TopScreenRight}; use super::{BottomScreen, TopScreen, TopScreen3D, TopScreenLeft, TopScreenRight};
use crate::console::Console; use crate::console::Console;
pub trait Sealed {} pub trait Sealed {}
@ -23,7 +23,7 @@ mod private {
impl Sealed for TopScreenLeft {} impl Sealed for TopScreenLeft {}
impl Sealed for TopScreenRight {} impl Sealed for TopScreenRight {}
impl Sealed for BottomScreen {} impl Sealed for BottomScreen {}
impl<S: Screen> Sealed for Console<'_, S> {} impl Sealed for Console<'_> {}
} }
/// Trait to handle common functionality for all screens. /// Trait to handle common functionality for all screens.

Loading…
Cancel
Save