From 5948589bdba0d463ac7f4c4e383dba490f96d5ed Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 5 Jan 2024 13:29:59 +0100 Subject: [PATCH] Fix suggestions --- ctru-rs/examples/software-keyboard.rs | 3 +-- ctru-rs/src/applets/swkbd.rs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/ctru-rs/examples/software-keyboard.rs b/ctru-rs/examples/software-keyboard.rs index c48b8a5..1d24a7d 100644 --- a/ctru-rs/examples/software-keyboard.rs +++ b/ctru-rs/examples/software-keyboard.rs @@ -5,7 +5,6 @@ use ctru::applets::swkbd::{Button, CallbackResult, SoftwareKeyboard}; use ctru::prelude::*; -use std::borrow::Cow; use std::ffi::CString; fn main() { @@ -27,7 +26,7 @@ fn main() { if str.to_str().unwrap().contains("boo") { return ( CallbackResult::Retry, - Some(Cow::Owned(CString::new("Ah, you scared me!").unwrap())), + Some(CString::new("Ah, you scared me!").unwrap()), ); } diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 7ae533d..41d40cd 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -9,20 +9,19 @@ use ctru_sys::{self, SwkbdState}; use bitflags::bitflags; use libc; -use std::borrow::Cow; -use std::ffi::CStr; +use std::ffi::{CStr, CString}; use std::fmt::Display; use std::iter::once; use std::str; -type CallbackFunction = dyn Fn(&CStr) -> (CallbackResult, Option>); +type CallbackFunction = dyn Fn(&CStr) -> (CallbackResult, Option); /// Configuration structure to setup the Software Keyboard applet. #[doc(alias = "SwkbdState")] -pub struct SoftwareKeyboard<'a> { +pub struct SoftwareKeyboard { state: Box, callback: Option>, - error_message: Option>, + error_message: Option, } /// Configuration structure to setup the Parental Lock applet. @@ -207,7 +206,7 @@ bitflags! { } } -impl SoftwareKeyboard<'_> { +impl SoftwareKeyboard { /// Initialize a new configuration for the Software Keyboard applet depending on how many "exit" buttons are available to the user (1, 2 or 3). /// /// # Example @@ -409,7 +408,7 @@ impl SoftwareKeyboard<'_> { /// if str.to_str().unwrap().contains("boo") { /// return ( /// CallbackResult::Retry, - /// Some(Cow::Owned(CString::new("Ah, you scared me!").unwrap())), + /// Some(CString::new("Ah, you scared me!").unwrap()), /// ); /// } /// @@ -441,6 +440,8 @@ impl SoftwareKeyboard<'_> { if let Some(callback) = &mut (*this).callback { let (res, cstr) = callback(text); + // Due to how `libctru` operates, the user is expected to keep the error message alive until + // the end of the Software Keyboard prompt. We ensure that happens by saving it within the configuration. (*this).error_message = cstr; if let Some(newstr) = &(*this).error_message { @@ -712,8 +713,7 @@ impl ParentalLock { /// # /// # } #[doc(alias = "swkbdInputText")] - #[allow(unused_variables)] - pub fn launch(&mut self, apt: &Apt, gfx: &Gfx) -> Result<(), Error> { + pub fn launch(&mut self, _apt: &Apt, _gfx: &Gfx) -> Result<(), Error> { unsafe { let mut buf = [0; 0]; ctru_sys::swkbdInputText(self.state.as_mut(), buf.as_mut_ptr(), 0); @@ -728,7 +728,7 @@ impl ParentalLock { } /// Creates a new [`SoftwareKeyboard`] configuration set to using a [`Kind::Normal`] keyboard and 2 [`Button`]s. -impl Default for SoftwareKeyboard<'_> { +impl Default for SoftwareKeyboard { fn default() -> Self { SoftwareKeyboard::new(Kind::Normal, ButtonConfig::LeftRight) }