Skip to content

Commit

Permalink
Boxing the closure is good, actually
Browse files Browse the repository at this point in the history
  • Loading branch information
FenrirWolf committed Feb 15, 2024
1 parent bf608bc commit abe2635
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ctru-rs/examples/software-keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {
// Custom filter callback to handle the given input.
// Using this callback it's possible to integrate the applet
// with custom error messages when the input is incorrect.
keyboard.set_filter_callback(Some(|text| {
keyboard.set_filter_callback(Some(Box::new(|text| {
if text.contains("boo") {
return (
CallbackResult::Retry,
Expand All @@ -28,7 +28,7 @@ fn main() {
}

(CallbackResult::Ok, None)
}));
})));

println!("Press A to enter some text or press Start to exit.");

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

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

/// Configuration structure to setup the Software Keyboard applet.
#[doc(alias = "SwkbdState")]
pub struct SoftwareKeyboard {
state: Box<SwkbdState>,
callback: Option<CallbackFunction>,
callback: Option<Box<CallbackFunction>>,
error_message: Option<CString>,
}

Expand Down Expand Up @@ -403,7 +403,7 @@ impl SoftwareKeyboard {
///
/// let mut keyboard = SoftwareKeyboard::default();
///
/// keyboard.set_filter_callback(Some(|text| {
/// keyboard.set_filter_callback(Some(Box::new(|text| {
/// if text.contains("boo") {
/// return (
/// CallbackResult::Retry,
Expand All @@ -412,10 +412,10 @@ impl SoftwareKeyboard {
/// }
///
/// (CallbackResult::Ok, None)
/// }));
/// })));
/// #
/// # }
pub fn set_filter_callback(&mut self, callback: Option<CallbackFunction>) {
pub fn set_filter_callback(&mut self, callback: Option<Box<CallbackFunction>>) {
self.callback = callback;
}

Expand All @@ -436,7 +436,7 @@ impl SoftwareKeyboard {

let result = {
// Run the callback if still available.
if let Some(callback) = this.callback {
if let Some(callback) = &mut this.callback {
let (result, error_message) = callback(text);

// Due to how `libctru` operates, the user is expected to keep the error message alive until
Expand Down

0 comments on commit abe2635

Please sign in to comment.