extendr_api/
lang_macros.rsuse crate::robj::GetSexp;
use crate::robj::Robj;
use crate::single_threaded;
use libR_sys::*;
#[doc(hidden)]
#[macro_export]
macro_rules! push_args {
($args: expr, $name: ident = $val : expr) => {
$args.push((stringify!($name), Robj::from($val)));
};
($args: expr, $name: ident = $val : expr, $($rest: tt)*) => {
$args.push((stringify!($name), Robj::from($val)));
push_args!($args, $($rest)*);
};
($args: expr, $val : expr) => {
$args.push(("", Robj::from($val)));
};
($args: expr, $val : expr, $($rest: tt)*) => {
$args.push(("", Robj::from($val)));
push_args!($args, $($rest)*);
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! args {
() => {
Vec::<(&str, Robj)>::new()
};
($($rest: tt)*) => {
{
let mut args = Vec::<(&str, Robj)>::new();
push_args!(args, $($rest)*);
args
}
};
}
#[doc(hidden)]
pub unsafe fn append_with_name(tail: SEXP, obj: Robj, name: &str) -> SEXP {
single_threaded(|| {
let cons = Rf_cons(obj.get(), R_NilValue);
SET_TAG(cons, crate::make_symbol(name));
SETCDR(tail, cons);
cons
})
}
#[doc(hidden)]
pub unsafe fn append(tail: SEXP, obj: Robj) -> SEXP {
single_threaded(|| {
let cons = Rf_cons(obj.get(), R_NilValue);
SETCDR(tail, cons);
cons
})
}
#[doc(hidden)]
pub unsafe fn make_lang(sym: &str) -> Robj {
Robj::from_sexp(single_threaded(|| Rf_lang1(crate::make_symbol(sym))))
}
#[doc(hidden)]
#[macro_export]
macro_rules! append_lang {
($tail: ident, $name: ident = $val : expr) => {
$tail = append_with_name($tail, Robj::from($val), stringify!($name));
};
($tail: ident, $name: ident = $val : expr, $($rest: tt)*) => {
$tail = append_with_name($tail, Robj::from($val), stringify!($name));
append_lang!($tail, $($rest)*);
};
($tail: ident, $val : expr) => {
$tail = append($tail, Robj::from($val));
};
($tail: ident, $val : expr, $($rest: tt)*) => {
$tail = append($tail, Robj::from($val));
append_lang!($tail, $($rest)*);
};
}
#[macro_export]
macro_rules! lang {
($sym : expr) => {
unsafe {
make_lang($sym)
}
};
($sym : expr, $($rest: tt)*) => {
unsafe {
use extendr_api::robj::GetSexp;
let res = make_lang($sym);
let mut tail = res.get();
append_lang!(tail, $($rest)*);
let _ = tail;
res
}
};
}