Skip to main content

with_r_thread

Function with_r_thread 

Source
pub fn with_r_thread<F, R>(f: F) -> R
where F: FnOnce() -> R,
Expand description

Run a closure that calls R’s C API, after checking that this is R’s main thread.

On R’s main thread, runs f and returns its value. R’s C API may not be called from any other thread, so this has nowhere to send the closure: called from a Rayon or std::thread thread it panics instead. Inside the closure the *_unchecked FFI variants are fine (lint MXL301).

§Panics

  • If miniextendr_runtime_init() hasn’t been called yet
  • If called from a thread other than R’s main thread

§Example

ⓘ
use miniextendr_api::with_r_thread;

let sexp = with_r_thread(|| unsafe {
    miniextendr_api::sys::Rf_ScalarInteger_unchecked(1)
});