Skip to main content

Module conv

Module conv 

Source
Expand description

§Type Conversion Helpers for Method Shims

This module provides type conversion functions used by the generated method shims from #[miniextendr] traits. These functions wrap the existing TryFromSexp and IntoR traits with appropriate error handling.

§Purpose

Method shims generated by #[miniextendr] traits need to:

  1. Convert SEXP arguments to Rust types
  2. Convert Rust return values to SEXP
  3. Return structured argument errors inside the shim’s unwind boundary

The shims convert their arguments themselves, with the C wrappers’ structured kind = "conversion" condition (#2029); this module holds the other conversions: return values, the View’s arguments and results, and the arity check.

§Error Handling

These helpers panic with a diagnostic on conversion or argument-count failure. The shim’s with_r_unwind_protect_shim catches it, so Rust-owned values are dropped before returning across the C ABI. A View re-panics with that condition for the consumer’s outer boundary. Call these helpers only inside such a boundary: their panics must not cross a C ABI unguarded.

§Thread Safety

All functions in this module must be called on R’s main thread, as they interact with R’s SEXP values and error handling.

Functions§

check_arity
Check that the number of arguments matches expected arity.
from_sexp⚠
Convert an R SEXP to a Rust type, panicking on failure.
missing_to_sexp⚠
Convert a Missing<T> trait-method argument to the SEXP the vtable shim receives: Missing::Absent is R’s missing-argument sentinel, which the shim’s TryFromSexp for Missing<T> reads back as Absent (the same sentinel an R wrapper forwards for an omitted argument), and Present(value) converts value with to_sexp, so T needs IntoR like any other argument type (Missing<AsNumeric> crosses as the sentinel or the number).
nil⚠
Return R’s NULL value.
to_sexp⚠
Convert a Rust value to an R SEXP.
try_from_sexp⚠
Convert an R SEXP to a Rust type, returning a Result.