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

Re-exports§

pub use borrow::NativeBorrow;
pub use str_parse::ParseRStr;
pub use str_parse::ParsedRStr;
pub use rooted::TryFromRoot;

Modules§

borrow
Native vector borrow metadata and pre-conversion alias checks.
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).
rooted 🔒
Conversions whose borrowed result cannot outlive an R root.
str_parse 🔒
Conversions for types parsed from an R string: ParseRStr, ParsedRStr and try_from_sexp_via_str_parse!.
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_option_try_from_sexp 🔒
Implement TryFromSexp for Option<T> where T already implements TryFromSexp.
impl_set_try_from_sexp_bool 🔒
impl_set_try_from_sexp_numeric 🔒
impl_try_from_sexp_scalar_native 🔒
impl_vec_option_try_from_sexp_list 🔒
Implement TryFromSexp for Vec<Option<T>> from R list (VECSXP).
impl_vec_try_from_sexp_list 🔒
Implement TryFromSexp for Vec<T> from R list (VECSXP).
impl_vec_try_from_sexp_numeric 🔒
Implement TryFromSexp for Vec<$target> by coercing from integer/real/logical/raw.

Structs§

BatchedErrors 🔒
Bounded accumulator that folds per-element conversion failures into one batched SexpError::InvalidValue, with the elements numbered as R counts them (1-based).
ElementErrors 🔒
The failures of a list read element by element (map_vecsxp_batched), folded into one error that keeps the classes and fields of the check refusals among them.
SexpClassError
Error describing a value that is not the kind of R object a conversion reads, where that kind is a class rather than a SEXPTYPE (a data frame is a list with a class).
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§

__mx_external_arg 👻
The handle a generated wrapper borrows for a &T / &mut T / ExternalPtrRef / ExternalPtrMut argument: converted like an ExternalPtr<T> argument, without its T: Send bound, since a borrowed argument keeps the call on R’s main thread.
__mx_optional_external_arg 👻
__mx_external_arg for an Option argument: NULL is None.
any_na 🔒
Whether R’s anyNA(sexp) is TRUE: the check no_na’s R guard makes, done in Rust on an argument that the guard does not cover (see TryFromSexp::__mx_input_has_na).
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.
collect_coerced 🔒
Drive a per-element coercion, batching every failure into one diagnostic.
element_position 🔒
(element <k>) for one 0-based index: the position suffix of a single per-element failure, as BatchedErrors writes it.
external_ptr_from_sexp 🔒
An ExternalPtr<T> from a bare EXTPTRSXP, or from a class handle carrying one (R6 private$.ptr, S4 ptr slot, S7 .ptr attribute, a list or environment with .ptr; generated Env/S3 handles are already bare EXTPTRSXPs). A pointer unwrapped from a class handle is rooted for the handle’s lifetime: R code can drop the class handle’s reference while Rust holds the pointer, and the finalizer would then free the value.
from_numeric_vec_with 🔒
Shared SEXP-dispatch shell for coerced numeric/logical/raw vectors.
map_strsxp_with 🔒
Shared SEXP-dispatch shell for the STRSXP (character vector) walk.
map_vecsxp_batched 🔒
Read a list (VECSXP) element by element with read, which gets each element’s SEXP, and report every element that fails in one error (ElementErrors), its position numbered as R counts it. Anything but a VECSXP is a type error, and no element is read.
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.
no_element_check 👻
The ElementCheck that accepts every value: what a container starts from when nothing encloses it. Not public API.
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).
push_element_positions 🔒
Append (element 2) / (elements 2, 5) for the given 0-based indices, numbered as R counts them. The one spelling of an element position in a conversion message.
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.
scalar_rejection_reason 👻
Why a value that Rf_asInteger / Rf_asReal / Rf_asLogical (or a raw coercion) could not reduce to one non-NA scalar was refused, in R terms: got NULL, got length 0, got "abc" (a string that does not parse), NA is not allowed, or got <type> for a value of another kind.
try_from_sexp_coerced_bool 👻
Convert a coerce boolean parameter from a logical or integer zero/one. Numeric parameters already have multi-source TryFromSexp implementations; bool needs this extension while its ordinary conversion stays logical-only.
try_from_sexp_coerced_bool_vec 👻
Convert a coerce boolean vector, retaining indexed, batched diagnostics.
try_from_sexp_coerced_f64 👻
Convert a coerce native f64 parameter.
try_from_sexp_coerced_f64_vec 👻
Convert a coerce Vec<f64> parameter.
try_from_sexp_coerced_i32 👻
Convert a coerce native i32 parameter.
try_from_sexp_coerced_i32_vec 👻
Convert a coerce Vec<i32> parameter.
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.

Type Aliases§

ElementCheck 👻
An element type’s check on an R value, as the newtype containers pass it to TryFromSexp::__mx_try_from_sexp_with_check: T::check_sexp, with the checks of any enclosing newtypes before it. Not public API.