Skip to content

Instantly share code, notes, and snippets.

@carlganz
Last active August 18, 2020 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlganz/a829d6e2254ffb5461dccf80b73b27cf to your computer and use it in GitHub Desktop.
Save carlganz/a829d6e2254ffb5461dccf80b73b27cf to your computer and use it in GitHub Desktop.
R bindgen
extern crate bindgen;
use std::env;
use std::path::PathBuf;
fn main() {
println!("cargo:rustc-link-lib=R");
println!("cargo:rustc-link-search=native={}", "/usr/lib/R/lib");
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.trust_clang_mangling(false)
.header("wrapper.h")
.clang_arg("-I/usr/share/R/include")
.clang_arg("-L/usr/lib/R/lib")
.clang_arg("-lR")
.blacklist_type("FP_NAN")
.blacklist_type("FP_INFINITE")
.blacklist_type("FP_ZERO")
.blacklist_type("FP_SUBNORMAL")
.blacklist_type("FP_NORMAL")
.blacklist_type("max_align_t")
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
#[cfg(test)]
mod tests {
use std;
//use std::ffi::CString;
#[test]
fn should_work() {
unsafe{
let p = 5.0 as f64;
println!("{}", p);
let r = super::Rf_ScalarReal(p);
super::Rf_protect(r);
let c = super::REAL(r);
println!("{}", *c);
assert_eq!(p, *c);
super::Rf_unprotect(1);
}
}
}
#include <R.h>
#include <Rmath.h>
#include <Rinternals.h>
@ErichDonGubler
Copy link

In lib.rs: Shouldn't you call Rf_protect after getting the value of r?

@carlganz
Copy link
Author

Yes you are correct. Still segfaults though.

@CGMossa
Copy link

CGMossa commented Aug 17, 2020

Did you ever get this to work?

@carlganz
Copy link
Author

Nope :(

@CGMossa
Copy link

CGMossa commented Aug 17, 2020

Alright.. I'd still wish to interface R and Rust someday :D Some folks are working on it. Contact me if this is still a priority for you.

@carlganz
Copy link
Author

@CGMossa
Copy link

CGMossa commented Aug 17, 2020

Actually, there is one super promising organisation here: ExtendR.
I heard about it from @andy-thomason.

@andy-thomason
Copy link

We'll be very pleased to hear from you to help drive ExtendR development.

@andy-thomason
Copy link

@CGMossa
Copy link

CGMossa commented Aug 18, 2020

I've been tinkering with FFI ever since our last small correspondence. I full intend to contribute. I just started reading some of the code, and immediately got flushed. 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment