compile_rs_lib: Compile a Rust step once for reuse across branches (worker...

View source: R/run-step-rs.R

compile_rs_libR Documentation

Compile a Rust step once for reuse across branches (worker behind tarpolyglot_map)

Description

Compiles the ⁠#[extendr]⁠ functions in a Rust script with rextendr::rust_source() and returns a self-contained bundle (the compiled shared library plus its generated R wrappers) that run_rs_step_prebuilt() can reload in any branch without recompiling. This is the function the companion ⁠<name>_rust_lib⁠ target built by tar_target_rs(..., pattern = tarpolyglot_map(...)) calls; it is exported so the call resolves at run time, but package users should not call it directly.

Usage

compile_rs_lib(
  script,
  dependencies = NULL,
  features = NULL,
  profile = NULL,
  toolchain = NULL
)

Arguments

script

Path to the Rust script containing ⁠#[extendr]⁠ functions (required). Accepts a file path or an inline tar_code() carrier; see the "Script arguments" section below.

dependencies, features, profile

Passed to rextendr::rust_source(): crate dependencies (named list), Cargo features, and build profile (e.g. "dev" or "release").

toolchain

Optional rustup toolchain (e.g. "stable-x86_64-pc-windows-gnu"); sets RUSTUP_TOOLCHAIN for the build. Default NULL uses the rustup default toolchain.

Details

The bundle embeds the library bytes, so it travels with the target's value to any worker or machine, and reloading is a dyn.load() (near-instant) rather than a fresh cargo build. See tarpolyglot_map() for the motivation and run_rs_step() for the per-branch (recompiling) alternative.

Value

An object of class tp_rust_lib: a list with the library basename, the raw library bytes, and the generated wrapper objs (named list of R functions).

Script arguments

A worker receives whatever the constructor already resolved, which is one of two things: a path to a file on disk, or an inline carrier built by tar_code() that holds the code in memory. Both are accepted, so a direct call may pass either.

tar_target_path() is deliberately not a third form at this level. It is a constructor-level convenience: tar_target_py() and the other constructors rewrite it while the pipeline's DAG is built, so that by the time a worker runs it has already become the ordinary file path held by the upstream target. Handing the result of tar_target_path() straight to a worker therefore does not resolve to a file. The three forms as written in ⁠_targets.R⁠, and which of them tracks your edits, are covered in vignette("scripts").

See Also

tarpolyglot_map(), run_rs_step_prebuilt(), tar_target_rs()

Examples

# Compiling needs a Rust toolchain, so this is gated on
# TARPOLYGLOT_EXAMPLES=true and runs in a temporary directory.
if (identical(Sys.getenv("TARPOLYGLOT_EXAMPLES"), "true")) {
  targets::tar_dir({
    # Normally invoked by tar_target_rs(pattern = tarpolyglot_map(...)).
    writeLines("#[extendr] fn square(x: f64) -> f64 { x * x }", "square.rs")
    lib <- compile_rs_lib(script = "square.rs")
    class(lib)
  })
}

tarpolyglot documentation built on Nov. 22, 2026, 5:07 p.m.