Skip to main content

run_on_worker

Function run_on_worker 

Source
#[doc(hidden)]
pub fn run_on_worker<F, T>(f: F) -> Result<T, String>
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,
Expand description

Run a closure on the worker thread with proper cleanup on panic.

Returns Ok(T) on success, Err(String) if the closure panicked. The caller handles the error (either tagged error value or Rf_errorcall).

Without the worker-thread feature, runs inline on the current thread.

§Precondition: caller must be the R main thread

The main-thread event loop that drives with_r_thread callbacks runs on whatever thread invokes run_on_worker. R API calls fired from inside the closure are routed back to that thread — so if the caller isn’t the R main thread, the callbacks land on the wrong thread silently.

In normal usage this contract is satisfied automatically: #[miniextendr] entry points are reached via .Call, which is always on R’s main thread. Calling run_on_worker from a Rust-spawned thread is a programming error.

In debug builds the precondition is asserted via debug_assert!. Release builds skip the check (one fewer atomic load per dispatch); the .Call invariant is relied on instead.