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.
Auto Trait Implementations§
impl Freeze for AltrepSexp
impl RefUnwindSafe for AltrepSexp
impl !Send for AltrepSexp
impl !Sync for AltrepSexp
impl Unpin for AltrepSexp
impl UnsafeUnpin for AltrepSexp
impl UnwindSafe for AltrepSexp
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> SizeEq<MaybeUninit<T>> for T
impl<T> SizeEq<MaybeUninit<T>> for T
type CastFrom = CastSizedExact
Source§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read moreimpl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> InvariantsEq<ManuallyDrop<T>> for Twhere
T: ?Sized,
impl<T> InvariantsEq<T> for Twhere
T: ?Sized,
impl<T> InvariantsEq<Unalign<T>> for T
impl<T> InvariantsEq<Wrapping<T>> for T
impl<Src, Dst, A, SV, DV, R> MutationCompatible<Src, A, SV, DV, (BecauseRead, R)> for Dst
impl<Src, Dst, A, SV, DV> MutationCompatible<Src, A, SV, DV, BecauseInvariantsEq> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: TransmuteFrom<Src, SV, DV> + InvariantsEq<Src> + ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Cell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ManuallyDrop<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ReadOnly<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
impl<T> TransmuteFrom<Unalign<T>, Valid, Valid> for T
impl<T> TransmuteFrom<UnsafeCell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Wrapping<T>, Valid, Valid> for T
impl<Src, Dst, A, SV, DV, C, R> TransmuteFromPtr<Src, A, SV, DV, C, R> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
C: CastExact<Src, Dst>,
Dst: TransmuteFrom<Src, SV, DV> + TryTransmuteFromPtr<Src, A, SV, DV, C, R> + ?Sized,
Src: ?Sized,
impl<Src, Dst, SV, DV, A, C, R> TryTransmuteFromPtr<Src, A, SV, DV, C, (BecauseMutationCompatible, R)> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: MutationCompatible<Src, A, SV, DV, R> + ?Sized,
C: CastExact<Src, Dst>,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 8 bytes