Skip to main content

Module references

Module references 

Source
Expand description

Reference conversions (borrowed views into R vectors).

Provides zero-copy access to R vector data through references. Callers must keep the source SEXP rooted for the entire reference lifetime.

Covers: &T, &mut T, Option<&T>, Vec<&T>, Vec<&[T]>, and mutable variants for all RNativeType types.

ยงTradeoff

Prefer borrowed &[T] over owned Vec<T> when you only need read access โ€” this is the fast path (no allocation, no copy). Reach for &mut [T] only when you genuinely need to mutate R-owned data in place; the R caller will observe those writes (ALTREP MAYBE_REFERENCED rules still apply). Failure mode: taking &mut [T] from a shared SEXP that R has handed to multiple callers writes through that aliased buffer.

For NA-aware reads use crate::from_r::na_vectors (Vec<Option<T>>) โ€” borrowed slices cannot express NA without losing the sentinel encoding. Outbound borrowed slices copy into R-owned storage through crate::IntoR. Mutable list inputs preflight all raw ranges before creating any references; this does not establish the root lifetime or exclude borrows held elsewhere by a direct caller (the remaining lifetime-boundary work is #1427).

Macrosยง

impl_ref_conversions_for ๐Ÿ”’

Structsยง

MutableRange ๐Ÿ”’
An unborrowed native buffer. No Rust reference exists during list preflight.

Functionsยง

map_mutable_list ๐Ÿ”’
Validate every element and every raw buffer before creating any references. Different ALTREP objects can expose identical or partially overlapping storage, so comparing SEXP identities (or just starting pointers) is not enough here. NULL is a missing element only for the optional forms.