pub struct AltrepSexp {
sexp: SEXP,
_not_send: PhantomData<Rc<()>>,
}Expand description
A SEXP known to be ALTREP. !Send + !Sync — must be materialized on the
R main thread before data can be accessed or sent to other threads.
This type prevents ALTREP vectors from being accidentally sent to rayon
or other worker threads where DATAPTR_RO would invoke R internals
(undefined behavior).
§As a #[miniextendr] parameter
AltrepSexp implements TryFromSexp, so it
can be used directly as a function parameter. It only accepts ALTREP
vectors — non-ALTREP input produces an error.
#[miniextendr]
pub fn altrep_info(x: AltrepSexp) -> String {
format!("{:?}, len={}", x.sexptype(), x.len())
}altrep_info(1:10) # OK — 1:10 is ALTREP
altrep_info(c(1L, 2L, 3L)) # Error: "expected an ALTREP vector"§Construction
AltrepSexp::try_wrap— runtime check, returnsNoneif not ALTREPAltrepSexp::from_raw— unsafe, caller assertsALTREP(sexp) != 0
§Materialization
All materialization methods must be called on the R main thread.
AltrepSexp::materialize— forces R to materialize, returns plain SEXPAltrepSexp::materialize_integer— materialize INTSXP and return&[i32]AltrepSexp::materialize_real— materialize REALSXP and return&[f64]AltrepSexp::materialize_logical— materialize LGLSXP and return&[i32]AltrepSexp::materialize_raw— materialize RAWSXP and return&[u8]AltrepSexp::materialize_complex— materialize CPLXSXP and return&[Rcomplex]AltrepSexp::materialize_strings— materialize STRSXP toVec<Option<String>>
§Thread safety
AltrepSexp is !Send + !Sync (via PhantomData<Rc<()>>). This is a
compile-time guarantee: you cannot send an un-materialized ALTREP vector
to another thread. Call one of the materialize_* methods first to get
a Send + Sync slice or SEXP.
Fields§
§sexp: SEXP§_not_send: PhantomData<Rc<()>>PhantomData<Rc<()>> makes this type !Send + !Sync.
Implementations§
Source§impl AltrepSexp
impl AltrepSexp
Sourcepub fn try_wrap(sexp: SEXP) -> Option<Self>
pub fn try_wrap(sexp: SEXP) -> Option<Self>
Check a SEXP and wrap if ALTREP. Returns None if not ALTREP.
Sourcepub unsafe fn materialize(self) -> SEXP
pub unsafe fn materialize(self) -> SEXP
Force materialization and return the (now materialized) SEXP.
For contiguous types (INTSXP, REALSXP, LGLSXP, RAWSXP, CPLXSXP),
calls DATAPTR_RO to trigger ALTREP materialization.
For STRSXP, iterates STRING_ELT to force element materialization.
After this call, the SEXP’s data pointer is stable and can be safely
accessed from any thread (the SEXP itself is still Send + Sync).
§Safety
Must be called on the R main thread.
Sourcepub unsafe fn materialize_real(&self) -> &[f64]
pub unsafe fn materialize_real(&self) -> &[f64]
Materialize and return a typed slice of f64 (REALSXP).
§Safety
Must be called on the R main thread. The SEXP must be REALSXP.
Sourcepub unsafe fn materialize_integer(&self) -> &[i32]
pub unsafe fn materialize_integer(&self) -> &[i32]
Materialize and return a typed slice of i32 (INTSXP).
§Safety
Must be called on the R main thread. The SEXP must be INTSXP.
Sourcepub unsafe fn materialize_logical(&self) -> &[i32]
pub unsafe fn materialize_logical(&self) -> &[i32]
Materialize and return a typed slice of i32 (LGLSXP, R’s internal logical storage).
§Safety
Must be called on the R main thread. The SEXP must be LGLSXP.
Sourcepub unsafe fn materialize_raw(&self) -> &[u8] ⓘ
pub unsafe fn materialize_raw(&self) -> &[u8] ⓘ
Materialize and return a typed slice of u8 (RAWSXP).
§Safety
Must be called on the R main thread. The SEXP must be RAWSXP.
Sourcepub unsafe fn materialize_complex(&self) -> &[Rcomplex]
pub unsafe fn materialize_complex(&self) -> &[Rcomplex]
Materialize and return a typed slice of Rcomplex (CPLXSXP).
§Safety
Must be called on the R main thread. The SEXP must be CPLXSXP.
Sourcepub unsafe fn materialize_strings(&self) -> Vec<Option<String>>
pub unsafe fn materialize_strings(&self) -> Vec<Option<String>>
Materialize strings into owned Rust data.
Each element is None for NA_character_, or Some(String) otherwise.
§Safety
Must be called on the R main thread. The SEXP must be STRSXP.
Trait Implementations§
Source§impl Debug for AltrepSexp
impl Debug for AltrepSexp
Source§impl IntoR for AltrepSexp
Convert AltrepSexp to R by returning the inner SEXP.
impl IntoR for AltrepSexp
Convert AltrepSexp to R by returning the inner SEXP.
This allows AltrepSexp to be used as a return type from #[miniextendr]
functions, transparently passing the ALTREP SEXP back to R.
Source§type Error = Infallible
type Error = Infallible
Source§fn try_into_sexp(self) -> Result<SEXP, Self::Error>
fn try_into_sexp(self) -> Result<SEXP, Self::Error>
Source§impl TryFromSexp for AltrepSexp
Conversion from R SEXP to AltrepSexp.
impl TryFromSexp for AltrepSexp
Conversion from R SEXP to AltrepSexp.
Only succeeds if the input is an ALTREP vector (ALTREP(sexp) != 0).
Non-ALTREP input produces SexpError::InvalidValue.
This is the inverse of TryFromSexp for SEXP,
which accepts any SEXP but auto-materializes ALTREP.