From d8cffcff611b28c17d4df81fae04d7e5dc07ca1a Mon Sep 17 00:00:00 2001 From: Fenrir Date: Thu, 21 Mar 2024 02:15:04 -0600 Subject: [PATCH] Remove allocation in Swkbd::configure_button --- ctru-rs/src/applets/swkbd.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 09f37bd..df3b5e4 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -541,15 +541,18 @@ impl SoftwareKeyboard { /// # } #[doc(alias = "swkbdSetButton")] pub fn configure_button(&mut self, button: Button, text: &str, submit: bool) { - unsafe { - let nul_terminated: String = text.chars().chain(once('\0')).collect(); - ctru_sys::swkbdSetButton( - self.state.as_mut(), - button.into(), - nul_terminated.as_ptr(), - submit, - ); + let button_text = &mut self.state.button_text[button as usize]; + + for (idx, code_unit) in text + .encode_utf16() + .take(ctru_sys::SWKBD_MAX_BUTTON_TEXT_LEN as _) + .chain(once(0)) + .enumerate() + { + button_text[idx] = code_unit; } + + self.state.button_submits_text[button as usize] = submit; } /// Configure the maximum number of UTF-16 code units that can be entered into the software