diff --git a/core/embed/rust/src/lib.rs b/core/embed/rust/src/lib.rs index aaba09e3d1..dc6277f7d4 100644 --- a/core/embed/rust/src/lib.rs +++ b/core/embed/rust/src/lib.rs @@ -44,6 +44,9 @@ mod trezorhal; #[cfg(feature = "ui")] pub mod ui; + +pub mod util; + #[cfg(feature = "debug")] #[cfg(not(test))] #[panic_handler] diff --git a/core/embed/rust/src/ui/api/bootloader_c.rs b/core/embed/rust/src/ui/api/bootloader_c.rs index a56d11feb2..ef0cab099f 100644 --- a/core/embed/rust/src/ui/api/bootloader_c.rs +++ b/core/embed/rust/src/ui/api/bootloader_c.rs @@ -7,9 +7,9 @@ use crate::{ }, ui::{ ui_bootloader::{BootloaderLayoutType as _, BootloaderUI}, - util::{from_c_array, from_c_str}, ModelUI, }, + util::{from_c_array, from_c_str}, }; #[no_mangle] diff --git a/core/embed/rust/src/ui/api/common_c.rs b/core/embed/rust/src/ui/api/common_c.rs index 97d938294c..f9b8037c3a 100644 --- a/core/embed/rust/src/ui/api/common_c.rs +++ b/core/embed/rust/src/ui/api/common_c.rs @@ -3,9 +3,7 @@ use crate::ui::{CommonUI, ModelUI}; -use crate::ui::shape; - -use crate::ui::util::from_c_str; +use crate::{ui::shape, util::from_c_str}; #[no_mangle] extern "C" fn display_rsod_rust( diff --git a/core/embed/rust/src/ui/api/prodtest_c.rs b/core/embed/rust/src/ui/api/prodtest_c.rs index d419f46729..530c85d181 100644 --- a/core/embed/rust/src/ui/api/prodtest_c.rs +++ b/core/embed/rust/src/ui/api/prodtest_c.rs @@ -5,9 +5,9 @@ use crate::{ }, ui::{ ui_prodtest::{ProdtestLayoutType, ProdtestUI}, - util::from_c_array, ModelUI, }, + util::from_c_array, }; #[cfg(feature = "touch")] diff --git a/core/embed/rust/src/ui/util.rs b/core/embed/rust/src/ui/util.rs index 9a6b26c813..8f606c307c 100644 --- a/core/embed/rust/src/ui/util.rs +++ b/core/embed/rust/src/ui/util.rs @@ -15,50 +15,6 @@ impl ResultExt for Result { } } -/// Constructs a string from a C string. -/// -/// # Safety -/// -/// The caller is responsible that the pointer is valid, which means that: -/// (a) it points to a memory containing a valid C string (zero-terminated -/// sequence of characters), and -/// (b) that the pointer has appropriate lifetime. -pub unsafe fn from_c_str<'a>(c_str: *const cty::c_char) -> Option<&'a str> { - if c_str.is_null() { - return None; - } - unsafe { - let bytes = core::ffi::CStr::from_ptr(c_str as _).to_bytes(); - if bytes.is_ascii() { - Some(core::str::from_utf8_unchecked(bytes)) - } else { - None - } - } -} - -/// Construct str from a C array. -/// -/// # Safety -/// -/// The caller is responsible that the pointer is valid, which means that: -/// (a) it points to a memory containing array of characters, with length `len`, -/// and -/// (b) that the pointer has appropriate lifetime. -pub unsafe fn from_c_array<'a>(c_str: *const cty::c_char, len: usize) -> Option<&'a str> { - if c_str.is_null() { - return None; - } - unsafe { - let slice = core::slice::from_raw_parts(c_str as *const u8, len); - if slice.is_ascii() { - Some(core::str::from_utf8_unchecked(slice)) - } else { - None - } - } -} - #[cfg(feature = "ui_debug")] static mut DISABLE_ANIMATION: bool = false; diff --git a/core/embed/rust/src/util/mod.rs b/core/embed/rust/src/util/mod.rs new file mode 100644 index 0000000000..37d55c7c47 --- /dev/null +++ b/core/embed/rust/src/util/mod.rs @@ -0,0 +1,43 @@ +/// Constructs a string from a C string. +/// +/// # Safety +/// +/// The caller is responsible that the pointer is valid, which means that: +/// (a) it points to a memory containing a valid C string (zero-terminated +/// sequence of characters), and +/// (b) that the pointer has appropriate lifetime. +pub unsafe fn from_c_str<'a>(c_str: *const cty::c_char) -> Option<&'a str> { + if c_str.is_null() { + return None; + } + unsafe { + let bytes = core::ffi::CStr::from_ptr(c_str as _).to_bytes(); + if bytes.is_ascii() { + Some(core::str::from_utf8_unchecked(bytes)) + } else { + None + } + } +} + +/// Construct str from a C array. +/// +/// # Safety +/// +/// The caller is responsible that the pointer is valid, which means that: +/// (a) it points to a memory containing array of characters, with length `len`, +/// and +/// (b) that the pointer has appropriate lifetime. +pub unsafe fn from_c_array<'a>(c_str: *const cty::c_char, len: usize) -> Option<&'a str> { + if c_str.is_null() { + return None; + } + unsafe { + let slice = core::slice::from_raw_parts(c_str as *const u8, len); + if slice.is_ascii() { + Some(core::str::from_utf8_unchecked(slice)) + } else { + None + } + } +}