Expand description
Conversions from R SEXP to Rust types.
This module provides TryFromSexp implementations for converting R values to Rust types:
| R Type | Rust Type | Access Method |
|---|---|---|
| INTSXP | i32, &[i32] | INTEGER() / DATAPTR_RO |
| REALSXP | f64, &[f64] | REAL() / DATAPTR_RO |
| LGLSXP | RLogical, &[RLogical] | LOGICAL() / DATAPTR_RO |
| RAWSXP | u8, &[u8] | RAW() / DATAPTR_RO |
| CPLXSXP | Rcomplex | COMPLEX() / DATAPTR_RO |
| STRSXP | &str, String | STRING_ELT() + R_CHAR() (UTF-8 locale asserted at init) |
§Submodules
| Module | Contents |
|---|---|
logical | Rboolean.string_elt(bool, Option<bool> |
coerced_scalars | Multi-source numeric scalars (i8..usize) + large integers (i64, u64) |
references | Borrowed views: &T, &mut T, &[T], Vec<&T> |
strings | &str, String, char from STRSXP |
na_vectors | Vec<Option<T>>, Box<[Option<T>]> with NA awareness |
collections | HashMap, BTreeMap, HashSet, BTreeSet |
cow_and_paths | Cow<[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:
TryFromSexp::try_from_sexp- checked version with debug thread assertionsTryFromSexp::try_from_sexp_unchecked- unchecked version for performance-critical paths
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§
- Sexp
Length Error - Error describing an unexpected R object length.
- Sexp
NaError - Error for NA values in conversions that require non-missing values.
- Sexp
Type Error - Error describing an unexpected R
SEXPTYPE.
Enums§
- Sexp
Error - 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§
- TryFrom
Sexp - 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_cowis now just an alias — all CHARSXPs are UTF-8 (asserted at init), so there’s no non-UTF-8 fallback path. ReturnsCow::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 onR_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— useslen_unchecked/vector_elt_uncheckedfor the type-check and walk. - not_
a_ 🔒handle_ error - Error for an
ExternalPtr<T>argument that is neither a bareEXTPTRSXPnor a class handle wrapping one (audit A9 — class-wrapped handles likeFoo$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_sliceforfrom_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— useslen_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
TypeMismatchErrorto theSexpErrorsurfaced fromExternalPtr<T>argument conversion. Shared by the checked and uncheckedTryFromSexppaths below.