Skip to main content

try_recover_r_sexp

Function try_recover_r_sexp 

Source
pub unsafe fn try_recover_r_sexp(
    data_ptr: *const u8,
    expected_type: SEXPTYPE,
    expected_len: usize,
) -> Option<SEXP>
Expand description

Try to recover the source R SEXP from a data pointer.

Given a pointer that may point into an R vector’s data area, this subtracts the known SEXPREC header size to get a candidate SEXP, then verifies it:

  1. The SEXP type tag (bits 0-4 of sxpinfo) matches expected_type
  2. ALTREP(candidate) is false (only non-ALTREP vectors have fixed-offset data)
  3. XLENGTH(candidate) matches expected_len (safe for non-ALTREP)

Returns None if:

  • The offset hasn’t been initialized yet
  • The pointer doesn’t come from an R vector
  • The candidate SEXP has the wrong type or length
  • The candidate is an ALTREP vector (data not at fixed offset from SEXP)

§Safety

Must be called on R’s main thread. The data pointer must be valid (i.e., it must point to readable memory for at least expected_len elements, which is guaranteed if it came from an Arrow buffer).