Browse Source

Fix unsoundness in Console's API

pull/10/head
Fenrir 7 years ago committed by FenrirWolf
parent
commit
16be4ef715
  1. 33
      ctru-rs/src/console.rs

33
ctru-rs/src/console.rs

@ -1,30 +1,45 @@
use std::default::Default; use std::default::Default;
use std::ptr; use std::mem;
use libctru::{PrintConsole, consoleInit, consoleSelect, consoleClear, consoleSetWindow};
use gfx::Screen; use gfx::Screen;
pub struct Console { pub struct Console {
context: ::libctru::PrintConsole, context: Box<PrintConsole>,
} }
impl Console { impl Console {
/// 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(screen: Screen) -> Self { pub fn init(screen: Screen) -> Self {
unsafe { unsafe {
let context = ptr::read(::libctru::consoleInit(screen.into(), ptr::null_mut())); let mut context = Box::new(mem::uninitialized::<PrintConsole>());
consoleInit(screen.into(), context.as_mut());
Console { context, } Console { context, }
} }
} }
pub fn select(&mut self) { /// Select this console as the current target for stdout
unsafe { ::libctru::consoleSelect(&mut self.context); } pub fn select(&self) {
unsafe { consoleSelect(self.context.as_ref() as *const _ as *mut _); }
} }
pub fn set_window(&mut self, x: i32, y: i32, width: i32, height: i32) { /// Clears all text from the console
unsafe { ::libctru::consoleSetWindow(&mut self.context, x, y, width, height) } pub fn clear(&self) {
unsafe { consoleClear() }
} }
pub fn clear(&mut self) { /// Resizes the active console to fit in a smaller portion of the screen.
unsafe { ::libctru::consoleClear() } ///
/// The first two arguments are the desired coordinates of the top-left corner
/// of the console, and the second pair is the new width and height
///
/// This function is unsafe because it does not validate that the input will produce
/// a console that actually fits on the screen
pub unsafe fn set_window(&mut self, x: i32, y: i32, width: i32, height: i32) {
consoleSetWindow(self.context.as_mut(), x, y, width, height);
} }
} }

Loading…
Cancel
Save