Expand description
Conversions from Rust types to R SEXP.
This module provides the IntoR trait for converting Rust values to R SEXPs.
§Submodules
| Module | Contents |
|---|---|
large_integers | i64, u64, isize, usize → REALSXP, plus string/bool/Option scalars |
collections | HashMap, BTreeMap, HashSet, BTreeSet → named/unnamed lists |
result | Result<T, E> → list with ok/err fields |
altrep | Altrep<T> marker type, Lazy<T> alias, IntoRAltrep trait |
§Choosing the right outbound conversion
IntoR is the lax outbound path — it silently widens 64-bit integers
to R’s REALSXP when the value doesn’t fit INTSXP. The strict alternative
lives in crate::strict and is opted into via #[miniextendr(strict)],
which routes through crate::strict::checked_into_sexp_i64 and friends.
Failure mode of staying on the default lax path when you care about exact
representation: an i64 value just above i32::MAX lands in R as an
inexact f64 (and IDs/counters larger than 2^53 start colliding).
For storage-directed conversions (e.g. force Vec<i64> into INTSXP
when values fit), reach for crate::into_r_as::IntoRAs instead.
The inbound counterpart is crate::from_r::TryFromSexp (strict by
construction — returns Result); the inbound looser path is
crate::coerce::Coerce / crate::coerce::TryCoerce. There is
intentionally no TryFromSexpStrict trait.
§Thread Safety
The trait provides two methods:
IntoR::into_sexp- checked version with debug thread assertionsIntoR::into_sexp_unchecked- unchecked version for performance-critical paths
Use into_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
§The absence contract: what None becomes in R
None does not map to one universal R value — it depends on the
shape of T in Option<T>:
- Owned scalars (
Option<i32>,Option<f64>,Option<bool>,Option<String>, and friends inlarge_integers) → the matchingNA_<type>_. R has a native NA sentinel for each of these. Option<&T>whereT: Copy(e.g.Option<&i32>) →NULL. A reference has nothing to copy onNone, so there is no NA to produce. See the impl doc onOption<&T>inlarge_integers.Option<&str>is the one exception to the previous rule: it returnsNA_character_, matchingOption<String>, becausestris unsized and can’t use the genericCopy-bounded blanket impl — it has its own hand-written impl instead.- Containers (
Option<Vec<T>>,Option<HashMap<..>>,Option<HashSet<..>>,Option<BTreeMap<..>>,Option<BTreeSet<..>>) →NULL. No container type has a native R NA sentinel either.
Changing a return type from Option<i32> to Option<&i32> or
Option<Vec<i32>> therefore silently flips the R-visible absence value
from NA_integer_ to NULL, with no compiler warning. See
result for the analogous (and differently-shaped) contract on
Result::Err.
Re-exports§
Modules§
- altrep 🔒
- ALTREP marker type (
Altrep<T>/Lazy<T>). - collections 🔒
- Collection conversions (HashMap, BTreeMap, HashSet, BTreeSet) to R.
- large_
integers 🔒 - Large integer types → REALSXP (f64).
- result 🔒
Result<T, E>conversions to R.
Macros§
- impl_
into_ 🔒r_ vec_ native - impl_
into_ 🔒r_ via_ coerce - Macro for infallible widening IntoR via Coerce.
- impl_
lossy_ 🔒string_ into_ r - Generate IntoR impls for types with
to_string_lossy()(owned scalar, ref scalar, Option, Vec,Vec<Option>). Used for PathBuf/&Path and OsString/&OsStr. - impl_
option_ 🔒collection_ into_ r - impl_
scalar_ 🔒into_ r - Macro for scalar IntoR via SEXP::scalar_* methods.
- impl_
set_ 🔒coerce_ into_ r - Macro for
HashSet<T>/BTreeSet<T>whereTcoerces to i32 (R’s native integer type). - impl_
tuple_ 🔒into_ r - Macro to implement IntoR for tuples of various sizes. Converts Rust tuples to unnamed R lists (VECSXP).
- impl_
vec_ 🔒coerce_ into_ r - Macro for
Vec<T>whereTcoerces to a native R type. - impl_
vec_ 🔒map_ into_ r - impl_
vec_ 🔒option_ coerce_ into_ r - Macro for
Vec<Option<T>>whereTcoerces to a type with existing Option impl. - impl_
vec_ 🔒option_ into_ r - Macro for NA-aware
Vec<Option<T>> → Rvector conversions. - impl_
vec_ 🔒option_ logical_ into_ r - impl_
vec_ 🔒option_ smart_ i64_ into_ r - Macro for NA-aware
Vec<Option<T>> → Rsmart vector conversion. Checks if all non-None values fit i32 → INTSXP, otherwise REALSXP. - impl_
vec_ 🔒smart_ i64_ into_ r - into_
r_ 🔒infallible - Generate an infallible
IntoRimpl whose only real method isinto_sexp.
Traits§
- IntoR
- Trait for converting Rust types to R SEXP values.
- IntoR
Altrep - Extension trait for ALTREP conversions.
Functions§
- alloc_
r_ 🔒 ⚠vector - Allocate an R vector of type
Twithnelements and return(SEXP, &mut [T]). - alloc_
r_ 🔒 ⚠vector_ unchecked - Allocate an R vector (unchecked FFI variant).
- logical_
iter_ 🔒to_ lglsxp - Helper: allocate LGLSXP and fill from an i32 iterator (checked).
- logical_
iter_ 🔒 ⚠to_ lglsxp_ unchecked - Helper: allocate LGLSXP and fill from an i32 iterator (unchecked).
- opt_
str_ 🔒iter_ to_ strsxp - Helper: allocate STRSXP and fill from an optional-string iterator (checked).
- opt_
str_ 🔒 ⚠iter_ to_ strsxp_ unchecked - Helper: allocate STRSXP and fill from an optional-string iterator (unchecked).
- str_
iter_ 🔒to_ strsxp - Helper: allocate STRSXP and fill from a string iterator (checked).
- str_
iter_ 🔒 ⚠to_ strsxp_ unchecked - Helper: allocate STRSXP and fill from a string iterator (unchecked).
- vec_
of_ 🔒into_ r_ to_ list - Helper: convert a Vec of IntoR items to an R list (VECSXP).
- vec_
of_ 🔒maps_ to_ list - Helper to convert a Vec of map-like types to an R list of named lists.
- vec_
option_ 🔒of_ into_ r_ to_ list - Helper: convert
Vec<Option<C: IntoR>>to a VECSXP, withNonemapping toR_NilValue(NULL) andSome(v)mapping to whateverv.into_sexp()produces. - vec_
to_ 🔒 ⚠sexp - Convert a slice to an R vector (checked) using
copy_from_slice. - vec_
to_ 🔒 ⚠sexp_ unchecked - Convert a slice to an R vector (unchecked) using
copy_from_slice. - vecsxp_
from_ 🔒 ⚠iter - Build a VECSXP from an exact-size iterator of child
SEXPs (checked). - vecsxp_
from_ 🔒 ⚠iter_ unchecked _uncheckedtwin ofvecsxp_from_iter— usesListBuilder::set_unchecked. For ALTREP /with_r_thread/ unwind contexts. The caller’s adaptor must produce children viainto_sexp_unchecked().