Skip to content

Commit

Permalink
wp: extendr-macros: Allow mut in input variables
Browse files Browse the repository at this point in the history
  • Loading branch information
CGMossa committed Feb 28, 2024
1 parent 291115d commit d7abd09
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions extendr-macros/src/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,12 @@ pub fn translate_formal(input: &FnArg, self_ty: Option<&syn::Type>) -> FnArg {
match input {
// function argument.
FnArg::Typed(pattype) => {
let pat = &pattype.pat;
parse_quote! { #pat : extendr_api::SEXP }
let pat = pattype.pat.as_ref();
let pat_ident = match pat {
syn::Pat::Ident(ref pat_ident) => &pat_ident.ident,
_ => todo!(),
};
parse_quote! { #pat_ident : extendr_api::SEXP }
}
// &self / &mut self
FnArg::Receiver(ref receiver) => {
Expand All @@ -336,7 +340,12 @@ fn translate_meta_arg(input: &mut FnArg, self_ty: Option<&syn::Type>) -> Expr {
FnArg::Typed(ref mut pattype) => {
let pat = pattype.pat.as_ref();
let ty = pattype.ty.as_ref();
let name_string = quote! { #pat }.to_string();
let pat_ident = if let syn::Pat::Ident(ref pat_ident) = pat {
&pat_ident.ident
} else {
panic!()
};
let name_string = quote! { #pat_ident }.to_string();
let type_string = type_name(ty);
let default = if let Some(default) = get_named_lit(&mut pattype.attrs, "default") {
quote!(Some(#default))
Expand Down Expand Up @@ -380,11 +389,12 @@ fn translate_to_robj(input: &FnArg) -> syn::Stmt {
FnArg::Typed(ref pattype) => {
let pat = &pattype.pat.as_ref();
if let syn::Pat::Ident(ref ident) = pat {
let varname = format_ident!("_{}_robj", ident.ident);
let ident = &ident.ident;
let varname = format_ident!("_{}_robj", ident);
//TODO: no need to protect incoming data from R
parse_quote! { let #varname = extendr_api::robj::Robj::from_sexp(#pat); }
parse_quote! { let #varname = extendr_api::robj::Robj::from_sexp(#ident); }
} else {
panic!("expect identifier as arg name")
parse_quote!{ compile_error!("expect identifier as arg name") }
}
}
FnArg::Receiver(_) => {
Expand Down

0 comments on commit d7abd09

Please sign in to comment.