#[macro_export]
macro_rules! body {
($gc:ident: $code:block) => {{
let $gc = unsafe { &$crate::Runtime::init() };
{
$code
}
}};
}
#[macro_export]
macro_rules! array {
($($x:expr),*) => {{
$crate::ToValue::to_value(&vec![$($crate::ToValue::to_value(&$x)),*])
}}
}
#[macro_export]
macro_rules! list {
($($x:expr),*) => {{
let mut l = $crate::list::empty();
for i in (&[$($x),*]).into_iter().rev() {
$crate::list::push_hd(&mut l, $crate::ToValue::to_value(i));
}
l
}};
}
#[macro_export]
macro_rules! import {
($vis:vis fn $name:ident($($arg:ident: $t:ty),*) $(-> $r:ty)?) => {
$vis unsafe fn $name(rt: &$crate::Runtime, $($arg: $t),*) -> Result<$crate::default_to_unit!($($r)?), $crate::Error> {
use $crate::{ToValue, FromValue};
type R = $crate::default_to_unit!($($r)?);
let ocaml_rs_named_func = match $crate::Value::named(stringify!($name)) {
Some(x) => x,
None => {
let msg = concat!(
stringify!($name),
" has not been registered using Callback.register"
);
return Err($crate::Error::Message(msg));
},
};
$(let $arg = $arg.to_value(rt);)*
let __unit = [$crate::Value::unit().raw()];
let __args = [$($arg.raw()),*];
let mut args = __args.as_slice();
if args.is_empty() {
args = &__unit;
}
let x = ocaml_rs_named_func.call_n(args)?;
Ok(R::from_value(x))
}
};
($($vis:vis fn $name:ident($($arg:ident: $t:ty),*) $(-> $r:ty)?;)+) => {
$(
$crate::import!($vis fn $name($($arg: $t),*) $(-> $r)?);
)*
}
}
#[macro_export]
macro_rules! function {
($x:expr, ($($argname:ident: $arg:ty),*) -> $r:ty) => {
|gc: &$crate::Runtime, $($argname: &$arg),*| -> Result<$r, $crate::Error> {
let args = [$($crate::ToValue::to_value($argname, gc)),*];
#[allow(unused_unsafe)]
unsafe { $crate::Value::call(&$x, gc, args) }
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! default_to_unit {
() => {
()
};
($rtyp:ty) => {
$rtyp
};
}