yutannihilation / savvy

A simple R extension interface using Rust
https://yutannihilation.github.io/savvy/guide/
MIT License
70 stars 4 forks source link

How to represent optional arguments on the Rust side? #242

Closed eitsupi closed 5 months ago

eitsupi commented 5 months ago

Sometimes there are optional arguments like Option<String> in Rust. Is there a recommended way to create a wrapper in savvy for a function with such arguments?

yutannihilation commented 5 months ago

Hm, good point. Let me consider adding a feature.

yutannihilation commented 5 months ago

This should work now.

#[savvy]
fn default_value_vec(x: Option<IntegerSexp>) -> savvy::Result<Sexp> {
    if let Some(x) = x {
        x.iter().sum::<i32>().try_into()
    } else {
        (-1).try_into()
    }
}
eitsupi commented 5 months ago

Awsome! Thanks for the quick update!