Skip to main content

AltrepSexp

Struct AltrepSexp 

Source
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

§Materialization

All materialization methods must be called on the R main thread.

§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

Source

pub unsafe fn from_raw(sexp: SEXP) -> Self

Wrap a SEXP that is known to be ALTREP.

§Safety

Caller must ensure ALTREP(sexp) is true (non-zero).

Source

pub fn try_wrap(sexp: SEXP) -> Option<Self>

Check a SEXP and wrap if ALTREP. Returns None if not ALTREP.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub unsafe fn as_raw(&self) -> SEXP

Get the inner SEXP without materializing.

§Safety

The returned SEXP is still ALTREP. Do not call DATAPTR_RO on it from a non-R thread.

Source

pub fn sexptype(&self) -> SEXPTYPE

Get the SEXPTYPE of the underlying vector.

Source

pub fn len(&self) -> usize

Get the length of the underlying vector.

Source

pub fn is_empty(&self) -> bool

Check if the underlying vector is empty.

Trait Implementations§

Source§

impl Debug for AltrepSexp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

The error type for fallible conversions. Read more
Source§

fn try_into_sexp(self) -> Result<SEXP, Self::Error>

Try to convert this value to an R SEXP. Read more
Source§

unsafe fn try_into_sexp_unchecked(self) -> Result<SEXP, Self::Error>

Try to convert to SEXP without thread safety checks. Read more
Source§

fn into_sexp(self) -> SEXP

Convert this value to an R SEXP, panicking on error. Read more
Source§

unsafe fn into_sexp_unchecked(self) -> SEXP
where Self: Sized,

Convert to SEXP without thread safety checks, panicking on error. Read more
Source§

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.

Source§

type Error = SexpError

The error type returned when conversion fails.
Source§

fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error>

Attempt to convert an R SEXP to this Rust type. Read more
Source§

unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error>

Convert from SEXP without thread safety checks. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> RDebug for T
where T: Debug,

Source§

fn debug_str(&self) -> String

Get a compact debug string representation.
Source§

fn debug_str_pretty(&self) -> String

Get a pretty-printed debug string with indentation.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.