Skip to content

Commit

Permalink
Return CString instead of String in filter callback
Browse files Browse the repository at this point in the history
  • Loading branch information
FenrirWolf committed Feb 15, 2024
1 parent abe2635 commit 4d20ace
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 3 additions & 1 deletion ctru-rs/examples/software-keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use ctru::applets::swkbd::{Button, CallbackResult, SoftwareKeyboard};
use ctru::prelude::*;

use std::ffi::CString;

fn main() {
let apt = Apt::new().unwrap();
let mut hid = Hid::new().unwrap();
Expand All @@ -23,7 +25,7 @@ fn main() {
if text.contains("boo") {
return (
CallbackResult::Retry,
Some(String::from("Ah, you scared me!")),
Some(CString::new("Ah, you scared me!").unwrap()),
);
}

Expand Down
7 changes: 3 additions & 4 deletions ctru-rs/src/applets/swkbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::fmt::Display;
use std::iter::once;
use std::str;

type CallbackFunction = dyn Fn(&str) -> (CallbackResult, Option<String>);
type CallbackFunction = dyn Fn(&str) -> (CallbackResult, Option<CString>);

/// Configuration structure to setup the Software Keyboard applet.
#[doc(alias = "SwkbdState")]
Expand Down Expand Up @@ -399,6 +399,7 @@ impl SoftwareKeyboard {
/// # fn main() {
/// #
/// use std::borrow::Cow;
/// use std::ffi::CString;
/// use ctru::applets::swkbd::{SoftwareKeyboard, CallbackResult};
///
/// let mut keyboard = SoftwareKeyboard::default();
Expand All @@ -407,7 +408,7 @@ impl SoftwareKeyboard {
/// if text.contains("boo") {
/// return (
/// CallbackResult::Retry,
/// Some(String::from("Ah, you scared me!")),
/// Some(CString::new("Ah, you scared me!").unwrap()),
/// );
/// }
///
Expand Down Expand Up @@ -442,8 +443,6 @@ impl SoftwareKeyboard {
// 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.
if let Some(error_message) = error_message {
let error_message = CString::from_vec_unchecked(error_message.into_bytes());

*pp_message = error_message.as_ptr();

this.error_message = Some(error_message);
Expand Down

0 comments on commit 4d20ace

Please sign in to comment.