From b406fc22f389fef664cda158cc6bff13b936a75f Mon Sep 17 00:00:00 2001 From: matejcik Date: Wed, 5 Jun 2024 14:12:04 +0200 Subject: [PATCH] refactor(core/rust): clean up macro namespace --- core/embed/rust/src/crypto/mod.rs | 2 + core/embed/rust/src/error.rs | 3 +- core/embed/rust/src/lib.rs | 22 ++- core/embed/rust/src/micropython/macros.rs | 130 +++++++++--------- core/embed/rust/src/micropython/util.rs | 2 +- core/embed/rust/src/protobuf/error.rs | 5 +- core/embed/rust/src/protobuf/obj.rs | 1 + core/embed/rust/src/translations/blob.rs | 2 +- core/embed/rust/src/translations/flash.rs | 5 +- core/embed/rust/src/translations/obj.rs | 3 + core/embed/rust/src/trezorhal/storage.rs | 2 +- core/embed/rust/src/ui/backlight.rs | 4 +- core/embed/rust/src/ui/display/toif.rs | 2 +- core/embed/rust/src/ui/layout/obj.rs | 1 + core/embed/rust/src/ui/layout/result.rs | 2 +- .../embed/rust/src/ui/model_mercury/layout.rs | 12 +- core/embed/rust/src/ui/model_tr/layout.rs | 12 +- core/embed/rust/src/ui/model_tt/layout.rs | 12 +- 18 files changed, 132 insertions(+), 90 deletions(-) diff --git a/core/embed/rust/src/crypto/mod.rs b/core/embed/rust/src/crypto/mod.rs index 8e4c505d04..9b753f9632 100644 --- a/core/embed/rust/src/crypto/mod.rs +++ b/core/embed/rust/src/crypto/mod.rs @@ -1,3 +1,5 @@ +use crate::error::value_error; + pub mod cosi; pub mod ed25519; mod ffi; diff --git a/core/embed/rust/src/error.rs b/core/embed/rust/src/error.rs index 732e75ae61..51e59c670f 100644 --- a/core/embed/rust/src/error.rs +++ b/core/embed/rust/src/error.rs @@ -26,13 +26,14 @@ pub enum Error { ValueErrorParam(&'static CStr, Obj), } -#[macro_export] macro_rules! value_error { ($msg:expr) => { $crate::error::Error::ValueError($msg) }; } +pub(crate) use value_error; + #[cfg(feature = "micropython")] impl Error { /// Create an exception instance matching the error code. The result of this diff --git a/core/embed/rust/src/lib.rs b/core/embed/rust/src/lib.rs index 57c86e0dfa..23ed6a215d 100644 --- a/core/embed/rust/src/lib.rs +++ b/core/embed/rust/src/lib.rs @@ -13,35 +13,33 @@ extern crate num_derive; #[macro_use] -mod error; -// use trezorhal for its macros early -#[macro_use] -mod trezorhal; +mod macros; #[cfg(feature = "crypto")] mod crypto; +#[cfg(feature = "debug")] +mod debug; +mod error; mod io; mod maybe_trace; #[cfg(feature = "micropython")] -#[macro_use] mod micropython; #[cfg(feature = "protobuf")] mod protobuf; mod storage; +mod strutil; mod time; #[cfg(feature = "ui_debug")] mod trace; #[cfg(feature = "translations")] -pub mod translations; +mod translations; +mod trezorhal; +// mod ui is `pub` because of the re-export pattern in individual models, which +// would trigger a brickload of "unused symbol" warnings otherwise. +// TODO: maybe get rid of the re-export pattern :shrugs: #[cfg(feature = "ui")] -#[macro_use] pub mod ui; -pub mod strutil; - -#[cfg(feature = "debug")] -#[macro_use] -pub mod debug; #[cfg(feature = "debug")] #[cfg(not(test))] diff --git a/core/embed/rust/src/micropython/macros.rs b/core/embed/rust/src/micropython/macros.rs index daab06d6f2..c0ebe4e12e 100644 --- a/core/embed/rust/src/micropython/macros.rs +++ b/core/embed/rust/src/micropython/macros.rs @@ -1,74 +1,49 @@ -/// Create an object for an exported function taking no arguments. -macro_rules! obj_fn_0 { - ($f:expr) => {{ +macro_rules! _obj_fn_make_fixed { + ($type:ident, $member:ident, $f:expr) => {{ #[allow(unused_unsafe)] unsafe { use $crate::micropython::ffi; ffi::mp_obj_fun_builtin_fixed_t { base: ffi::mp_obj_base_t { - type_: &ffi::mp_type_fun_builtin_0, + type_: &$crate::micropython::ffi::$type, }, - fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { _0: Some($f) }, + fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { $member: Some($f) }, } } }}; } +/// Create an object for an exported function taking no arguments. +macro_rules! obj_fn_0 { + ($f:expr) => { + crate::micropython::macros::_obj_fn_make_fixed!(mp_type_fun_builtin_0, _0, $f) + }; +} + /// Create an object for an exported function taking 1 arg. macro_rules! obj_fn_1 { - ($f:expr) => {{ - #[allow(unused_unsafe)] - unsafe { - use $crate::micropython::ffi; - - ffi::mp_obj_fun_builtin_fixed_t { - base: ffi::mp_obj_base_t { - type_: &ffi::mp_type_fun_builtin_1, - }, - fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { _1: Some($f) }, - } - } - }}; + ($f:expr) => { + crate::micropython::macros::_obj_fn_make_fixed!(mp_type_fun_builtin_1, _1, $f) + }; } /// Create an object for an exported function taking 2 args. macro_rules! obj_fn_2 { - ($f:expr) => {{ - #[allow(unused_unsafe)] - unsafe { - use $crate::micropython::ffi; - - ffi::mp_obj_fun_builtin_fixed_t { - base: ffi::mp_obj_base_t { - type_: &ffi::mp_type_fun_builtin_2, - }, - fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { _2: Some($f) }, - } - } - }}; + ($f:expr) => { + crate::micropython::macros::_obj_fn_make_fixed!(mp_type_fun_builtin_2, _2, $f) + }; } /// Create an object for an exported function taking 3 args. macro_rules! obj_fn_3 { - ($f:expr) => {{ - #[allow(unused_unsafe)] - unsafe { - use $crate::micropython::ffi; - - ffi::mp_obj_fun_builtin_fixed_t { - base: ffi::mp_obj_base_t { - type_: &ffi::mp_type_fun_builtin_3, - }, - fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { _3: Some($f) }, - } - } - }}; + ($f:expr) => { + crate::micropython::macros::_obj_fn_make_fixed!(mp_type_fun_builtin_3, _3, $f) + }; } -/// Create an object for an exported function taking a variable number of args. -macro_rules! obj_fn_var { - ($min:expr, $max:expr, $f:expr) => {{ +macro_rules! _obj_fn_make_var { + ($min:expr, $max:expr, takes_kw: $takes_kw:expr, $var_or_kw:ident: $f:expr) => {{ #[allow(unused_unsafe)] unsafe { use $crate::micropython::ffi; @@ -77,29 +52,28 @@ macro_rules! obj_fn_var { base: ffi::mp_obj_base_t { type_: &ffi::mp_type_fun_builtin_var, }, - sig: ($min << 17u32) | ($max << 1u32) | 0, // min, max, takes_kw - fun: ffi::_mp_obj_fun_builtin_var_t__bindgen_ty_1 { var: Some($f) }, + sig: ($min << 17u32) | ($max << 1u32) | $takes_kw, + fun: ffi::_mp_obj_fun_builtin_var_t__bindgen_ty_1 { + $var_or_kw: Some($f), + }, } } }}; } +/// Create an object for an exported function taking a variable number of args +/// between min and max +macro_rules! obj_fn_var { + ($min:expr, $max:expr, $f:expr) => { + crate::micropython::macros::_obj_fn_make_var!($min, $max, takes_kw:0, var:$f) + }; +} + /// Create an object for an exported function taking key-value args. macro_rules! obj_fn_kw { - ($min:expr, $f:expr) => {{ - #[allow(unused_unsafe)] - unsafe { - use $crate::micropython::ffi; - - ffi::mp_obj_fun_builtin_var_t { - base: ffi::mp_obj_base_t { - type_: &ffi::mp_type_fun_builtin_var, - }, - sig: ($min << 17u32) | (0xffff << 1u32) | 1, // min, max, takes_kw - fun: ffi::_mp_obj_fun_builtin_var_t__bindgen_ty_1 { kw: Some($f) }, - } - } - }}; + ($min:expr, $f:expr) => { + crate::micropython::macros::_obj_fn_make_var!($min, 0xffff, takes_kw:1, kw:$f) + }; } /// Construct fixed static const `Map` from `key` => `val` pairs. @@ -261,6 +235,36 @@ macro_rules! attr_tuple { }); } +// required because they are used in expansion of macros below +pub(crate) use _obj_fn_make_fixed; +pub(crate) use _obj_fn_make_var; + +pub(crate) use attr_tuple; +pub(crate) use obj_dict; +pub(crate) use obj_fn_0; +pub(crate) use obj_fn_1; +pub(crate) use obj_fn_2; +pub(crate) use obj_fn_3; +pub(crate) use obj_fn_kw; +pub(crate) use obj_fn_var; +pub(crate) use obj_map; +pub(crate) use obj_module; +pub(crate) use obj_type; + +// from https://docs.rs/ufmt/latest/ufmt/ +// like `std::format!` it returns a `std::String` but uses `uwrite!` instead of +// `write!` +macro_rules! uformat { + // IMPORTANT use `tt` fragments instead of `expr` fragments (i.e. `$($exprs:expr),*`) + ($len:expr, $($tt:tt)*) => {{ + let mut s = heapless::String::<$len>::new(); + match ufmt::uwrite!(&mut s, $($tt)*) { + Ok(_) => Ok(s), + Err(e) => Err(e), + } + }}; +} + /// Print arbitrary amounts of slices into a terminal. /// Does not include a newline at the end. /// Does not do anything when not in debugging mode. diff --git a/core/embed/rust/src/micropython/util.rs b/core/embed/rust/src/micropython/util.rs index 5ec0cb27dd..07b40bfb6f 100644 --- a/core/embed/rust/src/micropython/util.rs +++ b/core/embed/rust/src/micropython/util.rs @@ -10,7 +10,7 @@ use super::{ qstr::Qstr, runtime::{catch_exception, raise_exception}, }; -use crate::error::Error; +use crate::error::{value_error, Error}; /// Perform a call and convert errors into a raised MicroPython exception. /// Should only called when returning from Rust to C. See `raise_exception` for diff --git a/core/embed/rust/src/protobuf/error.rs b/core/embed/rust/src/protobuf/error.rs index e29975ec76..23ea2ae86b 100644 --- a/core/embed/rust/src/protobuf/error.rs +++ b/core/embed/rust/src/protobuf/error.rs @@ -1,4 +1,7 @@ -use crate::{error::Error, micropython::qstr::Qstr}; +use crate::{ + error::{value_error, Error}, + micropython::qstr::Qstr, +}; pub const fn experimental_not_enabled() -> Error { value_error!(c"Experimental features are disabled.") diff --git a/core/embed/rust/src/protobuf/obj.rs b/core/embed/rust/src/protobuf/obj.rs index 5efecf4510..7b4c69033c 100644 --- a/core/embed/rust/src/protobuf/obj.rs +++ b/core/embed/rust/src/protobuf/obj.rs @@ -6,6 +6,7 @@ use crate::{ dict::Dict, ffi, gc::Gc, + macros::{obj_fn_1, obj_fn_2, obj_fn_3, obj_module, obj_type}, map::Map, module::Module, obj::{Obj, ObjBase}, diff --git a/core/embed/rust/src/translations/blob.rs b/core/embed/rust/src/translations/blob.rs index 8d734a8cbe..414f30101c 100644 --- a/core/embed/rust/src/translations/blob.rs +++ b/core/embed/rust/src/translations/blob.rs @@ -2,7 +2,7 @@ use core::{mem, str}; use crate::{ crypto::{cosi, ed25519, merkle::merkle_root, sha256}, - error::Error, + error::{value_error, Error}, io::InputStream, }; diff --git a/core/embed/rust/src/translations/flash.rs b/core/embed/rust/src/translations/flash.rs index a99b045de8..238e41d717 100644 --- a/core/embed/rust/src/translations/flash.rs +++ b/core/embed/rust/src/translations/flash.rs @@ -1,6 +1,9 @@ use spin::{RwLock, RwLockReadGuard}; -use crate::{error::Error, trezorhal::translations}; +use crate::{ + error::{value_error, Error}, + trezorhal::translations, +}; use super::blob::Translations; diff --git a/core/embed/rust/src/translations/obj.rs b/core/embed/rust/src/translations/obj.rs index 7ba6fb4db0..9b341abdc7 100644 --- a/core/embed/rust/src/translations/obj.rs +++ b/core/embed/rust/src/translations/obj.rs @@ -4,6 +4,9 @@ use crate::{ micropython::{ buffer::{get_buffer, StrBuffer}, ffi, + macros::{ + attr_tuple, obj_dict, obj_fn_0, obj_fn_1, obj_fn_2, obj_map, obj_module, obj_type, + }, map::Map, module::Module, obj::Obj, diff --git a/core/embed/rust/src/trezorhal/storage.rs b/core/embed/rust/src/trezorhal/storage.rs index 46fa04e573..6bf44a3b71 100644 --- a/core/embed/rust/src/trezorhal/storage.rs +++ b/core/embed/rust/src/trezorhal/storage.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] use super::ffi; -use crate::error::Error; +use crate::error::{value_error, Error}; use core::ptr; use num_traits::FromPrimitive; diff --git a/core/embed/rust/src/ui/backlight.rs b/core/embed/rust/src/ui/backlight.rs index ca2ce55ff6..98f6aff269 100644 --- a/core/embed/rust/src/ui/backlight.rs +++ b/core/embed/rust/src/ui/backlight.rs @@ -1,6 +1,8 @@ use crate::{ error::Error, - micropython::{ffi, obj::Obj, qstr::Qstr, simple_type::SimpleTypeObj, typ::Type, util}, + micropython::{ + ffi, macros::obj_type, obj::Obj, qstr::Qstr, simple_type::SimpleTypeObj, typ::Type, util, + }, ui::{ui_features::ModelUI, UIFeaturesCommon}, }; diff --git a/core/embed/rust/src/ui/display/toif.rs b/core/embed/rust/src/ui/display/toif.rs index 657f7d5b43..eff8808921 100644 --- a/core/embed/rust/src/ui/display/toif.rs +++ b/core/embed/rust/src/ui/display/toif.rs @@ -1,5 +1,5 @@ use crate::{ - error::Error, + error::{value_error, Error}, trezorhal::uzlib::{UzlibContext, UZLIB_WINDOW_SIZE}, ui::{ component::image::Image, diff --git a/core/embed/rust/src/ui/layout/obj.rs b/core/embed/rust/src/ui/layout/obj.rs index f01f1a8766..23ade324ac 100644 --- a/core/embed/rust/src/ui/layout/obj.rs +++ b/core/embed/rust/src/ui/layout/obj.rs @@ -9,6 +9,7 @@ use crate::{ micropython::{ buffer::StrBuffer, gc::Gc, + macros::{obj_dict, obj_fn_1, obj_fn_2, obj_fn_var, obj_map, obj_type}, map::Map, obj::{Obj, ObjBase}, qstr::Qstr, diff --git a/core/embed/rust/src/ui/layout/result.rs b/core/embed/rust/src/ui/layout/result.rs index f0dfb29f9f..bf91424b38 100644 --- a/core/embed/rust/src/ui/layout/result.rs +++ b/core/embed/rust/src/ui/layout/result.rs @@ -1,4 +1,4 @@ -use crate::micropython::{qstr::Qstr, simple_type::SimpleTypeObj, typ::Type}; +use crate::micropython::{macros::obj_type, qstr::Qstr, simple_type::SimpleTypeObj, typ::Type}; static CONFIRMED_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_CONFIRMED, }; static CANCELLED_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_CANCELLED, }; diff --git a/core/embed/rust/src/ui/model_mercury/layout.rs b/core/embed/rust/src/ui/model_mercury/layout.rs index 0d349a6a5d..2286c2b9d7 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -1,10 +1,18 @@ use core::{cmp::Ordering, convert::TryInto}; use crate::{ - error::Error, + error::{value_error, Error}, io::BinaryData, micropython::{ - gc::Gc, iter::IterBuf, list::List, map::Map, module::Module, obj::Obj, qstr::Qstr, util, + gc::Gc, + iter::IterBuf, + list::List, + macros::{obj_fn_1, obj_fn_kw, obj_module}, + map::Map, + module::Module, + obj::Obj, + qstr::Qstr, + util, }, strutil::TString, translations::TR, diff --git a/core/embed/rust/src/ui/model_tr/layout.rs b/core/embed/rust/src/ui/model_tr/layout.rs index d606bc2e4c..9318ba1525 100644 --- a/core/embed/rust/src/ui/model_tr/layout.rs +++ b/core/embed/rust/src/ui/model_tr/layout.rs @@ -6,8 +6,16 @@ use crate::{ error::Error, maybe_trace::MaybeTrace, micropython::{ - buffer::StrBuffer, gc::Gc, iter::IterBuf, list::List, map::Map, module::Module, obj::Obj, - qstr::Qstr, util, + buffer::StrBuffer, + gc::Gc, + iter::IterBuf, + list::List, + macros::{obj_fn_0, obj_fn_1, obj_fn_kw, obj_module}, + map::Map, + module::Module, + obj::Obj, + qstr::Qstr, + util, }, strutil::TString, translations::TR, diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 2076ff527a..525600e64f 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -1,10 +1,18 @@ use core::{cmp::Ordering, convert::TryInto}; use crate::{ - error::Error, + error::{value_error, Error}, io::BinaryData, micropython::{ - gc::Gc, iter::IterBuf, list::List, map::Map, module::Module, obj::Obj, qstr::Qstr, util, + gc::Gc, + iter::IterBuf, + list::List, + macros::{obj_fn_1, obj_fn_kw, obj_module}, + map::Map, + module::Module, + obj::Obj, + qstr::Qstr, + util, }, strutil::TString, translations::TR,