Skip to main content

Module from_r

Module from_r 

Source
Expand description

Conversions from R SEXP to Rust types.

This module provides TryFromSexp implementations for converting R values to Rust types:

R TypeRust TypeAccess Method
INTSXPi32, &[i32]INTEGER() / DATAPTR_RO
REALSXPf64, &[f64]REAL() / DATAPTR_RO
LGLSXPRLogical, &[RLogical]LOGICAL() / DATAPTR_RO
RAWSXPu8, &[u8]RAW() / DATAPTR_RO
CPLXSXPRcomplexCOMPLEX() / DATAPTR_RO
STRSXP&str, StringSTRING_ELT() + R_CHAR() (UTF-8 locale asserted at init)

§Submodules

ModuleContents
logicalRboolean.string_elt(bool, Option<bool>
coerced_scalarsMulti-source numeric scalars (i8..usize) + large integers (i64, u64)
referencesBorrowed views: &T, &mut T, &[T], Vec<&T>
strings&str, String, char from STRSXP
na_vectorsVec<Option<T>>, Box<[Option<T>]> with NA awareness
collectionsHashMap, BTreeMap, HashSet, BTreeSet
cow_and_pathsCow<[T]>, PathBuf, OsString, string sets

§Choosing the right inbound conversion

TryFromSexp is the strict inbound path: it returns Result<T, SexpError> and rejects mismatched SEXPTYPEs outright (no silent coercion). When you need to accept arguments coming from multiple R native types, reach for the crate::coerce::Coerce / crate::coerce::TryCoerce traits instead — those are the looser inbound path and the entry point for the multi-source scalars handled in coerced_scalars.

The strict-vs-lax pairing for outbound conversion lives on crate::into_r::IntoR (lax, default) vs crate::strict (#[miniextendr(strict)]). There is intentionally no TryFromSexpStrict trait — inbound is already strict-by-default because it returns Result.

§Thread Safety

The trait provides two methods:

Use try_from_sexp_unchecked when you’re certain you’re on the main thread:

  • Inside ALTREP callbacks
  • Inside standalone #[miniextendr] functions (they run on the main thread)
  • Inside extern "C-unwind" functions called directly by R

Modules§

coerced_scalars 🔒
Coerced scalar conversions (multi-source numeric) and large integer scalars.
collections 🔒
Collection conversions (HashMap, BTreeMap, HashSet, BTreeSet).
cow_and_paths 🔒
Cow, PathBuf, OsString, and string collection conversions.
logical 🔒
Logical type conversions (Rboolean, bool, Option variants).
na_vectors 🔒
NA-aware vector conversions (Vec<Option<T>>, Box<[Option<T>]>).
references 🔒
Reference conversions (borrowed views into R vectors).
strings 🔒
String conversions — STRSXP requires special handling via STRING_ELT.
tuples 🔒
Tuple conversions: read an R list (VECSXP) positionally into (A, B, ...).

Macros§

impl_option_map_try_from_sexp 🔒
impl_option_set_try_from_sexp 🔒
impl_set_try_from_sexp_bool 🔒
impl_set_try_from_sexp_numeric 🔒
impl_try_from_sexp_scalar_native 🔒
impl_vec_try_from_sexp_numeric 🔒
Implement TryFromSexp for Vec<$target> by coercing from integer/real/logical/raw.

Structs§

SexpLengthError
Error describing an unexpected R object length.
SexpNaError
Error for NA values in conversions that require non-missing values.
SexpTypeError
Error describing an unexpected R SEXPTYPE.

Enums§

SexpError
Unified conversion error when decoding an R SEXP.

Constants§

BATCHED_ERROR_CAP 🔒
Cap on the number of per-element failures listed in a batched vector conversion error; the remainder is summarized as "and N more".

Traits§

TryFromSexp
TryFrom-style trait for converting SEXP to Rust types.

Functions§

batch_conversion_errors 👻
Combine indexed per-element conversion failures into one batched SexpError::InvalidValue.
charsxp_to_cow 🔒
charsxp_to_cow is now just an alias — all CHARSXPs are UTF-8 (asserted at init), so there’s no non-UTF-8 fallback path. Returns Cow::Borrowed.
charsxp_to_str 🔒
Convert CHARSXP to &str — zero-copy from R’s string data.
charsxp_to_str_impl 🔒
Shared implementation: given a data pointer and CHARSXP, produce &str.
charsxp_to_str_unchecked 🔒
Unchecked version of charsxp_to_str (skips R thread checks on R_CHAR).
charsxp_to_string_lossy 🔒
Convert CHARSXP to an owned, lossy String.
coerce_slice_to_vec 🔒
Helper to coerce a slice element-wise into a Vec.
from_numeric_vec_with 🔒
Shared SEXP-dispatch shell for coerced numeric/logical/raw vectors.
is_na_real 🔒
Check if an f64 value is R’s NA_real_ (a specific NaN bit pattern).
map_strsxp_with 🔒
Shared SEXP-dispatch shell for the STRSXP (character vector) walk.
map_vecsxp_with 🔒
Shared SEXP-dispatch shell for the VECSXP (list) walk.
map_vecsxp_with_unchecked 🔒
Unchecked-FFI variant of map_vecsxp_with — uses len_unchecked / vector_elt_unchecked for the type-check and walk.
not_a_handle_error 🔒
Error for an ExternalPtr<T> argument that is neither a bare EXTPTRSXP nor a class handle wrapping one (audit A9 — class-wrapped handles like Foo$new(...) are unwrapped automatically; this fires only when no .ptr/slot/attribute could be recovered at all).
r_slice 🔒
Create a slice from an R data pointer, handling the zero-length case.
r_slice_mut 🔒
Mutable version of r_slice for from_raw_parts_mut.
scalar_charsxp 🔒
Shared scalar-STRSXP prologue: type-check + len == 1 + string_elt(0).
scalar_charsxp_unchecked 🔒
Unchecked-FFI variant of scalar_charsxp — uses len_unchecked / string_elt_unchecked.
try_from_sexp_numeric_set 🔒
Convert numeric/logical/raw vectors to a set type with element-wise coercion.
try_from_sexp_numeric_vec 🔒
Convert numeric/logical/raw vectors to Vec<T> with element-wise coercion.
type_mismatch_to_sexp_error 🔒
Map a downcast TypeMismatchError to the SexpError surfaced from ExternalPtr<T> argument conversion. Shared by the checked and unchecked TryFromSexp paths below.