From ab3c62ae7150949bf5437d412a13ab4772edd291 Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 5 Feb 2024 16:24:07 +0100 Subject: [PATCH] feat(core/rust): introduce SimpleTypeObj --- core/embed/rust/src/micropython/mod.rs | 1 + .../embed/rust/src/micropython/simple_type.rs | 46 +++++++++++++++++++ core/embed/rust/src/ui/layout/result.rs | 37 ++------------- 3 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 core/embed/rust/src/micropython/simple_type.rs diff --git a/core/embed/rust/src/micropython/mod.rs b/core/embed/rust/src/micropython/mod.rs index 0ebe3afae..62dbc2f67 100644 --- a/core/embed/rust/src/micropython/mod.rs +++ b/core/embed/rust/src/micropython/mod.rs @@ -15,6 +15,7 @@ pub mod obj; pub mod print; pub mod qstr; pub mod runtime; +pub mod simple_type; pub mod time; pub mod typ; pub mod util; diff --git a/core/embed/rust/src/micropython/simple_type.rs b/core/embed/rust/src/micropython/simple_type.rs new file mode 100644 index 000000000..a11424631 --- /dev/null +++ b/core/embed/rust/src/micropython/simple_type.rs @@ -0,0 +1,46 @@ +use super::{ + obj::{Obj, ObjBase}, + typ::Type, +}; + +/// Simple MicroPython object type builder. +/// +/// This is a struct that can be used to build a MicroPython-exported type which +/// has no data. The caller must generate the type information using the +/// `obj_type!` macro, and use this type to create the corresponding type object +/// visible to MicroPython. +/// +/// Example: +/// ``` +/// use crate::micropython::{obj::Obj, simple_type::SimpleTypeObj, typ::Type}; +/// +/// static CONFIRMED_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_CONFIRMED, }; +/// +/// static CONFIRMED_OBJ: SimpleTypeObj = SimpleTypeObj::new(&CONFIRMED_TYPE); +/// +/// let my_type = CONFIRMED_OBJ.as_obj(); +/// ``` +#[repr(C)] +pub struct SimpleTypeObj { + base: ObjBase, +} + +impl SimpleTypeObj { + pub const fn new(base: &'static Type) -> Self { + Self { + base: base.as_base(), + } + } + + /// Convert SimpleType to a MicroPython object. + pub const fn as_obj(&'static self) -> Obj { + // SAFETY: + // - We are an object struct with a base and a type. + // - 'static lifetime holds us in place. + // - There's nothing to mutate. + unsafe { Obj::from_ptr(self as *const _ as *mut _) } + } +} + +// SAFETY: We are in a single-threaded environment. +unsafe impl Sync for SimpleTypeObj {} diff --git a/core/embed/rust/src/ui/layout/result.rs b/core/embed/rust/src/ui/layout/result.rs index 885cd2fe9..f0dfb29f9 100644 --- a/core/embed/rust/src/ui/layout/result.rs +++ b/core/embed/rust/src/ui/layout/result.rs @@ -1,38 +1,9 @@ -use crate::micropython::{ - obj::{Obj, ObjBase}, - qstr::Qstr, - typ::Type, -}; - -#[repr(C)] -pub struct ResultObj { - base: ObjBase, -} - -impl ResultObj { - /// Convert ResultObj to a MicroPython object. - pub const fn as_obj(&'static self) -> Obj { - // SAFETY: - // - We are an object struct with a base and a type. - // - 'static lifetime holds us in place. - // - There's nothing to mutate. - unsafe { Obj::from_ptr(self as *const _ as *mut _) } - } -} - -// SAFETY: We are in a single-threaded environment. -unsafe impl Sync for ResultObj {} +use crate::micropython::{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, }; static INFO_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_INFO, }; -pub static CONFIRMED: ResultObj = ResultObj { - base: CONFIRMED_TYPE.as_base(), -}; -pub static CANCELLED: ResultObj = ResultObj { - base: CANCELLED_TYPE.as_base(), -}; -pub static INFO: ResultObj = ResultObj { - base: INFO_TYPE.as_base(), -}; +pub static CONFIRMED: SimpleTypeObj = SimpleTypeObj::new(&CONFIRMED_TYPE); +pub static CANCELLED: SimpleTypeObj = SimpleTypeObj::new(&CANCELLED_TYPE); +pub static INFO: SimpleTypeObj = SimpleTypeObj::new(&INFO_TYPE);