Chapter 13 Rust Serialization

13.0.1 with rustc_serialize

This exmaple serializes a Rust struct and then uses the info to construct an R data.frame. data.frame is a special list in R.

A data frame is a list of variables of the same number of rows with unique row names, given class “data.frame”. If no variables are included, the row names determine the number of rows. (From R help)

extern crate rustc_serialize;
use rustc_serialize::json;

#[derive(RustcDecodable, RustcEncodable)]
pub struct TestStruct  {
    data_str:  Vec<String>,
    data_vector: Vec<u8>,
}

// #[rustr_export]
pub fn rusttor()->RResult<String>{
    let object = TestStruct {
        data_str:    vec!["r".into(),"u".into(),"s".into(),"t".into()],
        data_vector: vec![2,3,4,5],
    };
    let encoded = rtry!(json::encode(&object));
    Ok(encoded)
}
rust('
extern crate rustc_serialize;
use rustc_serialize::json;

#[derive(RustcDecodable, RustcEncodable)]
pub struct TestStruct  {
    data_str:  Vec<String>,
    data_vector: Vec<u8>,
}

// #[rustr_export]
pub fn rusttor()->RResult<String>{
    let object = TestStruct {
        data_str:    vec!["r".into(),"u".into(),"s".into(),"t".into()],
        data_vector: vec![2,3,4,5],
    };
    let encoded = rtry!(json::encode(&object));
    Ok(encoded)
}

', depend = '
[dependencies]
rustc-serialize = "0.3"
rustr = "*"
')
#> updating cached Cargo.toml.

as.data.frame(rjson::fromJSON(rusttor()))
#>   data_str data_vector
#> 1        r           2
#> 2        u           3
#> 3        s           4
#> 4        t           5

13.0.2 with Serde

This exmaple uses nightly Rust with serde. You can also use Stable Rust with serde. To learn more, Please read serde Document.

#![feature(custom_derive, plugin)]
#![plugin(serde_macros)]

#[macro_use]
extern crate rustr;
pub mod export;
pub use rustr::*;

extern crate serde;
extern crate serde_json;

#[derive(Serialize, Deserialize, Debug)]
struct Point {
    x: i32,
    y: i32,
}

// #[rustr_export]
pub fn serdeinr()->RResult<String>{
    let point = Point { x: 1, y: 2 };

    let serialized = rtry!(serde_json::to_string(&point));
    Ok(serialized)
}

// #[rustr_export]
pub fn serdefromr(x:String)->RResult<()>{
    let res: Point = rtry!(serde_json::from_str(&x));
    r_printf(&format!("{:?}",res));
    Ok(())
}
rust('
#![feature(custom_derive, plugin)]
#![plugin(serde_macros)]

#[macro_use]
extern crate rustr;
pub mod export;
pub use rustr::*;

extern crate serde;
extern crate serde_json;

#[derive(Serialize, Deserialize, Debug)]
struct Point {
    x: i32,
    y: i32,
}

// #[rustr_export]
pub fn serdeinr()->RResult<String>{
    let point = Point { x: 1, y: 2 };

    let serialized = rtry!(serde_json::to_string(&point));
    Ok(serialized)
}

// #[rustr_export]
pub fn serdefromr(x:String)->RResult<()>{
    let res: Point = rtry!(serde_json::from_str(&x));
    r_printf(&format!("{:?}",res));
    Ok(())
}
', depend = '
[dependencies]
serde = "*"
serde_json = "*"
serde_macros = "*"
rustr = "*"
', header = F)


x = serdeinr()
x
#> [1] "{\"x\":1,\"y\":2}"
serdefromr(x)
#> Point { x: 1, y: 2 }