mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-31 18:40:56 +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 qstr;
|
||||
pub mod runtime;
|
||||
pub mod simple_type;
|
||||
pub mod time;
|
||||
pub mod typ;
|
||||
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::{
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user