IntoList

Derive Macro IntoList 

Source
#[derive(IntoList)]
{
    // Attributes available to this derive:
    #[into_list]
}
Expand description

Derives an implementation of From<Struct> for Robj and From<&Struct> for Robj on this struct.

This allows the struct to be converted to a named list in R, where the list names correspond to the field names of the Rust struct.

§Examples

In the below example, converted contains an R list object with the same fields as the Foo struct.

use extendr_api::prelude::*;
use extendr_macros::IntoList;

#[derive(IntoList)]
struct Foo {
    a: u32,
    b: String
}
let converted: Robj = Foo {
    a: 5,
    b: String::from("bar")
}.into();
assert_eq!(converted, R!(r"list(a=5, b='bar')")?);

See TryFromRobj for a derive-macro in the other direction, i.e. instantiation of a rust type, by an R list with fields corresponding to said type.

Supported field attributes

  • #[into_list(ignore)] omits the field from being added to the R list()

§Details

Note, the From<Struct> for Robj behaviour is different from what is obtained by applying the standard #[extendr] macro to an impl block. The #[extendr] behaviour returns to R a pointer to Rust memory, and generates wrapper functions for calling Rust functions on that pointer. The implementation from #[derive(IntoList)] actually converts the Rust structure into a native R list, which allows manipulation and access to internal fields, but it’s a one-way conversion, and converting it back to Rust will produce a copy of the original struct.