mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-20 20:31:06 +00:00
feat(core/rust): introduce SimpleTypeObj
This commit is contained in:
parent
0bb57173db
commit
ab3c62ae71
@ -15,6 +15,7 @@ pub mod obj;
|
|||||||
pub mod print;
|
pub mod print;
|
||||||
pub mod qstr;
|
pub mod qstr;
|
||||||
pub mod runtime;
|
pub mod runtime;
|
||||||
|
pub mod simple_type;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
pub mod typ;
|
pub mod typ;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
46
core/embed/rust/src/micropython/simple_type.rs
Normal file
46
core/embed/rust/src/micropython/simple_type.rs
Normal file
@ -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 {}
|
@ -1,38 +1,9 @@
|
|||||||
use crate::micropython::{
|
use crate::micropython::{qstr::Qstr, simple_type::SimpleTypeObj, typ::Type};
|
||||||
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 {}
|
|
||||||
|
|
||||||
static CONFIRMED_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_CONFIRMED, };
|
static CONFIRMED_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_CONFIRMED, };
|
||||||
static CANCELLED_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_CANCELLED, };
|
static CANCELLED_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_CANCELLED, };
|
||||||
static INFO_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_INFO, };
|
static INFO_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_INFO, };
|
||||||
|
|
||||||
pub static CONFIRMED: ResultObj = ResultObj {
|
pub static CONFIRMED: SimpleTypeObj = SimpleTypeObj::new(&CONFIRMED_TYPE);
|
||||||
base: CONFIRMED_TYPE.as_base(),
|
pub static CANCELLED: SimpleTypeObj = SimpleTypeObj::new(&CANCELLED_TYPE);
|
||||||
};
|
pub static INFO: SimpleTypeObj = SimpleTypeObj::new(&INFO_TYPE);
|
||||||
pub static CANCELLED: ResultObj = ResultObj {
|
|
||||||
base: CANCELLED_TYPE.as_base(),
|
|
||||||
};
|
|
||||||
pub static INFO: ResultObj = ResultObj {
|
|
||||||
base: INFO_TYPE.as_base(),
|
|
||||||
};
|
|
||||||
|
Loading…
Reference in New Issue
Block a user