From a01a787113b16257d837f6d42e60095f8e7964fb Mon Sep 17 00:00:00 2001 From: obrusvit Date: Fri, 14 Jun 2024 22:50:34 +0200 Subject: [PATCH] refactor(core): remove StringType This commit removes the last usage of StringType. In the future, we use TString. [no changelog] --- core/embed/rust/src/strutil.rs | 13 ----- .../src/ui/model_tr/component/share_words.rs | 51 ++++++++----------- core/embed/rust/src/ui/model_tr/layout.rs | 3 +- 3 files changed, 21 insertions(+), 46 deletions(-) diff --git a/core/embed/rust/src/strutil.rs b/core/embed/rust/src/strutil.rs index 6ebf938540..49e3e2cd1e 100644 --- a/core/embed/rust/src/strutil.rs +++ b/core/embed/rust/src/strutil.rs @@ -9,19 +9,6 @@ use crate::micropython::{buffer::StrBuffer, obj::Obj}; #[cfg(feature = "translations")] use crate::translations::TR; -/// Trait for internal representation of strings. This is a legacy crutch before -/// we fully transition to `TString`. For now, it allows some manner of -/// compatibility between `&str` and `StrBuffer`. Implies the following -/// operations: -/// - dereference into a short-lived `&str` reference (AsRef) (probably not -/// strictly necessary anymore) -/// - create a new string from a string literal (From<&'static str>) -/// - infallibly convert into a `TString` (Into>), which is -/// then used for other operations. -pub trait StringType: AsRef + From<&'static str> + Into> {} - -impl StringType for T where T: AsRef + From<&'static str> + Into> {} - /// Unified-length String type, long enough for most simple use-cases. pub type ShortString = String<50>; diff --git a/core/embed/rust/src/ui/model_tr/component/share_words.rs b/core/embed/rust/src/ui/model_tr/component/share_words.rs index f1c98bf1a5..f1e920f281 100644 --- a/core/embed/rust/src/ui/model_tr/component/share_words.rs +++ b/core/embed/rust/src/ui/model_tr/component/share_words.rs @@ -1,5 +1,5 @@ use crate::{ - strutil::{ShortString, StringType, TString}, + strutil::{ShortString, TString}, translations::TR, ui::{ component::{ @@ -27,21 +27,15 @@ const INFO_TOP_OFFSET: i16 = 20; const MAX_WORDS: usize = 33; // super-shamir has 33 words, all other have less /// Showing the given share words. -pub struct ShareWords -where - T: StringType, -{ +pub struct ShareWords<'a> { area: Rect, scrollbar: Child, - share_words: Vec, + share_words: Vec, MAX_WORDS>, page_index: usize, } -impl ShareWords -where - T: StringType + Clone, -{ - pub fn new(share_words: Vec) -> Self { +impl<'a> ShareWords<'a> { + pub fn new(share_words: Vec, MAX_WORDS>) -> Self { let mut instance = Self { area: Rect::zero(), scrollbar: Child::new(ScrollBar::to_be_filled_later()), @@ -117,11 +111,13 @@ where if index >= self.share_words.len() { break; } - let word = &self.share_words[index]; let baseline = self.area.top_left() + Offset::y(y_offset); let ordinal = uformat!("{}.", index + 1); display_left(baseline + Offset::x(NUMBER_X_OFFSET), &ordinal, NUMBER_FONT); - display_left(baseline + Offset::x(WORD_X_OFFSET), word, WORD_FONT); + let word = &self.share_words[index]; + word.map(|w| { + display_left(baseline + Offset::x(WORD_X_OFFSET), w, WORD_FONT); + }); } } @@ -144,18 +140,17 @@ where .with_fg(theme::FG) .render(target); - shape::Text::new(baseline + Offset::x(WORD_X_OFFSET), word.as_ref()) - .with_font(WORD_FONT) - .with_fg(theme::FG) - .render(target); + word.map(|w| { + shape::Text::new(baseline + Offset::x(WORD_X_OFFSET), w) + .with_font(WORD_FONT) + .with_fg(theme::FG) + .render(target); + }); } } } -impl Component for ShareWords -where - T: StringType + Clone, -{ +impl<'a> Component for ShareWords<'a> { type Msg = Never; fn place(&mut self, bounds: Rect) -> Rect { @@ -198,10 +193,7 @@ where } } -impl Paginate for ShareWords -where - T: StringType + Clone, -{ +impl<'a> Paginate for ShareWords<'a> { fn page_count(&mut self) -> usize { // Not defining the logic here, as we do not want it to be `&mut`. self.total_page_count() @@ -216,10 +208,7 @@ where // DEBUG-ONLY SECTION BELOW #[cfg(feature = "ui_debug")] -impl crate::trace::Trace for ShareWords -where - T: StringType + Clone, -{ +impl<'a> crate::trace::Trace for ShareWords<'a> { fn trace(&self, t: &mut dyn crate::trace::Tracer) { t.component("ShareWords"); let content = if self.is_final_page() { @@ -231,8 +220,8 @@ where if index >= self.share_words.len() { break; } - let word: TString = self.share_words[index].clone().into(); - unwrap!(uwrite!(content, "{}. {}\n", index + 1, word)); + self.share_words[index] + .map(|word| unwrap!(uwrite!(content, "{}. {}\n", index + 1, word))); } content }; diff --git a/core/embed/rust/src/ui/model_tr/layout.rs b/core/embed/rust/src/ui/model_tr/layout.rs index 9318ba1525..da4883d709 100644 --- a/core/embed/rust/src/ui/model_tr/layout.rs +++ b/core/embed/rust/src/ui/model_tr/layout.rs @@ -6,7 +6,6 @@ use crate::{ error::Error, maybe_trace::MaybeTrace, micropython::{ - buffer::StrBuffer, gc::Gc, iter::IterBuf, list::List, @@ -1331,7 +1330,7 @@ extern "C" fn new_select_word(n_args: usize, args: *const Obj, kwargs: *mut Map) extern "C" fn new_show_share_words(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = |_args: &[Obj], kwargs: &Map| { let share_words_obj: Obj = kwargs.get(Qstr::MP_QSTR_share_words)?; - let share_words: Vec = util::iter_into_vec(share_words_obj)?; + let share_words: Vec = util::iter_into_vec(share_words_obj)?; let cancel_btn = Some(ButtonDetails::up_arrow_icon()); let confirm_btn =