|
|
|
@ -9,25 +9,44 @@ use crate::{
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub fn try_or_raise<T>(func: impl FnOnce() -> Result<T, Error>) -> T {
|
|
|
|
|
func().unwrap_or_else(|err| raise_exception(err))
|
|
|
|
|
/// Perform a call and convert errors into a raised micropython exception.
|
|
|
|
|
/// Should only called when returning from Rust to C.
|
|
|
|
|
/// See `raise_exception` for details.
|
|
|
|
|
pub unsafe fn try_or_raise<T>(func: impl FnOnce() -> Result<T, Error>) -> T {
|
|
|
|
|
func().unwrap_or_else(|err| unsafe {
|
|
|
|
|
raise_exception(err);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn try_with_kwargs(kwargs: *const Map, func: impl FnOnce(&Map) -> Result<Obj, Error>) -> Obj {
|
|
|
|
|
try_or_raise(|| {
|
|
|
|
|
/// Extract kwargs from a C call and pass them into Rust. Raise exception if an
|
|
|
|
|
/// error occurs.
|
|
|
|
|
/// TODO when does uPy call this?
|
|
|
|
|
/// Should only called when returning from Rust to C.
|
|
|
|
|
/// See `raise_exception` for details.
|
|
|
|
|
pub unsafe fn try_with_kwargs(
|
|
|
|
|
kwargs: *const Map,
|
|
|
|
|
func: impl FnOnce(&Map) -> Result<Obj, Error>,
|
|
|
|
|
) -> Obj {
|
|
|
|
|
let block = || {
|
|
|
|
|
let kwargs = unsafe { kwargs.as_ref() }.ok_or(Error::MissingKwargs)?;
|
|
|
|
|
|
|
|
|
|
func(kwargs)
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
unsafe { try_or_raise(block) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn try_with_args_and_kwargs(
|
|
|
|
|
/// Extract args and kwargs from a C call and pass them into Rust. Raise
|
|
|
|
|
/// exception if an error occurs.
|
|
|
|
|
/// TODO when would uPy call this?
|
|
|
|
|
/// Should only called when returning from Rust to C.
|
|
|
|
|
/// See `raise_exception` for details.
|
|
|
|
|
pub unsafe fn try_with_args_and_kwargs(
|
|
|
|
|
n_args: usize,
|
|
|
|
|
args: *const Obj,
|
|
|
|
|
kwargs: *const Map,
|
|
|
|
|
func: impl FnOnce(&[Obj], &Map) -> Result<Obj, Error>,
|
|
|
|
|
) -> Obj {
|
|
|
|
|
try_or_raise(|| {
|
|
|
|
|
let block = || {
|
|
|
|
|
let args = if args.is_null() {
|
|
|
|
|
&[]
|
|
|
|
|
} else {
|
|
|
|
@ -36,16 +55,21 @@ pub fn try_with_args_and_kwargs(
|
|
|
|
|
let kwargs = unsafe { kwargs.as_ref() }.ok_or(Error::MissingKwargs)?;
|
|
|
|
|
|
|
|
|
|
func(args, kwargs)
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
unsafe { try_or_raise(block) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn try_with_args_and_kwargs_inline(
|
|
|
|
|
/// Extract args and kwargs from a C call where args and kwargs are inlined, and
|
|
|
|
|
/// pass them into Rust. Raise exception if an error occurs.
|
|
|
|
|
/// Should only called when returning from Rust to C.
|
|
|
|
|
/// See `raise_exception` for details.
|
|
|
|
|
pub unsafe fn try_with_args_and_kwargs_inline(
|
|
|
|
|
n_args: usize,
|
|
|
|
|
n_kw: usize,
|
|
|
|
|
args: *const Obj,
|
|
|
|
|
func: impl FnOnce(&[Obj], &Map) -> Result<Obj, Error>,
|
|
|
|
|
) -> Obj {
|
|
|
|
|
try_or_raise(|| {
|
|
|
|
|
let block = || {
|
|
|
|
|
let args_slice: &[Obj];
|
|
|
|
|
let kwargs_slice: &[MapElem];
|
|
|
|
|
|
|
|
|
@ -59,5 +83,6 @@ pub fn try_with_args_and_kwargs_inline(
|
|
|
|
|
|
|
|
|
|
let kw_map = Map::from_fixed(kwargs_slice);
|
|
|
|
|
func(args_slice, &kw_map)
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
unsafe { try_or_raise(block) }
|
|
|
|
|
}
|
|
|
|
|