Skip to main content

make_rust_condition_value_with_data

Function make_rust_condition_value_with_data 

Source
pub unsafe fn make_rust_condition_value_with_data(
    message: &str,
    kind: &str,
    class: Option<&str>,
    call: Option<SEXP>,
    data: Option<ConditionData>,
) -> SEXP
Expand description

Build a tagged condition-value SEXP for transport across the Rust→R boundary.

Used for all Rust-origin failures and user-facing conditions. The R-side switch in condition_check_lines reads .val$kind to select the condition type and .val$class to prepend optional user classes before the standard rust_* layering.

§Safety

Must be called from R’s main thread in a valid R allocation context (standard R API constraint): the body performs raw R allocation (Rf_allocVector, SET_VECTOR_ELT, SETATTRIB) which is UB off the main thread. In practice every caller reaches this through crate::unwind_protect::with_r_unwind_protect (or a proc-macro-generated panic handler), both of which run on the main thread. The returned SEXP is unprotected — caller must protect if needed.

§PROTECT discipline

Every fresh allocation (msg, kind, optional class, true-marker, and — when present — the data VECSXP, its names, and each field value) is added to a single ProtectScope before the next allocation that might trigger a GC barrier. The scope releases them all together (UNPROTECT) when it drops at function exit, on every branch — the RAII equivalent of the former hand-counted prot / Rf_unprotect(prot) ladder. This discipline was established by PR #344 commit af6b4875 to fix a recursive gc invocation segfault on R-devel.

§Arguments

  • message - Human-readable condition message
  • kind - Condition kind — one of the constants in kind.
  • class - Optional user-supplied class name to prepend to the layered vector
  • call - Optional R call SEXP for error context. When None, uses R_NilValue.
  • data - Optional named condition-data payload (from the macros’ data = ... form). When Some, each (name, value) becomes a named element of a list stored in slot [4]; the R helper splices these into the condition object so handlers can read e$<name>. When None, slot [4] is NULL.