Skip to main content

SEXP

Struct SEXP 

Source
#[repr(transparent)]
pub struct SEXP(pub *mut SEXPREC);
Expand description

R’s pointer type for S-expressions.

This is a newtype wrapper around *mut SEXPREC that implements Send and Sync. SEXP is just a handle (pointer) - the actual data it points to is managed by R’s garbage collector and should only be accessed on R’s main thread.

§Safety

While SEXP is Send+Sync (allowing it to be passed between threads), the data it points to must only be accessed on R’s main thread. The miniextendr runtime enforces this through the worker thread pattern.

§Equality Semantics

IMPORTANT: The derived PartialEq compares pointer equality, not semantic equality. For proper R semantics (comparing object contents), use R_compute_identical.

// Pointer equality (fast, often wrong for R semantics)
if sexp1 == sexp2 { ... }  // Only true if same pointer

// Semantic equality (correct R semantics)
if R_compute_identical(sexp1, sexp2, 16) != 0 { ... }

Hash trait removed: SEXP no longer implements Hash because proper hashing would require deep content inspection via R_compute_identical, which is too expensive for general use. If you need SEXP as a HashMap key, use pointer identity:

// Store by pointer identity (common pattern for R symbol lookups)
let mut map: HashMap<*mut SEXPREC, Value> = HashMap::new();
map.insert(sexp.as_ptr(), value);

Tuple Fields§

§0: *mut SEXPREC

Implementations§

Source§

impl SEXP

Source

pub const fn null() -> Self

Create a C null pointer SEXP (0x0).

This is not R’s NULL value (R_NilValue). R’s NULL is a real heap-allocated singleton; a C null pointer is just address zero. Passing SEXP::null() where R expects R_NilValue will corrupt R’s GC state and likely segfault.

Use SEXP::nil() for R’s NULL. Only use null() for low-level pointer initialization, ALTREP Sum/Min/Max “can’t compute” returns (R checks != NULL, not != R_NilValue), or comparison against uninitialized pointers.

See also: SEXP::nil(), SEXP::is_null(), SexpExt::is_nil()

Source

pub fn nil() -> Self

Return R’s NULL singleton (R_NilValue).

This is not a C null pointer — it points to R’s actual nil object on the heap. Use this for .Call() return values, SEXP arguments to R API functions, and any slot in R data structures.

See also: SEXP::null(), SexpExt::is_nil(), SEXP::is_null()

Source

pub const fn is_null(self) -> bool

Check if this SEXP is a C null pointer (0x0).

To check if an SEXP is R’s NULL (R_NilValue), use SexpExt::is_nil() instead.

See also: SexpExt::is_nil(), SexpExt::is_null_or_nil()

Source

pub const fn as_ptr(self) -> *mut SEXPREC

Get the raw pointer.

Source

pub const fn from_ptr(ptr: *mut SEXPREC) -> Self

Create from a raw pointer.

Source

pub fn charsxp(s: &str) -> SEXP

Create a CHARSXP from a Rust &str (UTF-8).

Source

pub fn na_string() -> SEXP

R’s NA_character_ singleton.

Source

pub fn blank_string() -> SEXP

R’s empty string "" singleton.

Source

pub fn install_char(charsxp: SEXP) -> SEXP

Create an R symbol (SYMSXP) from a CHARSXP.

Equivalent to Rf_installChar(charsxp). The symbol is interned in R’s global symbol table and never garbage collected.

Source

pub fn symbol(name: &str) -> SEXP

Create an R symbol (SYMSXP) from a Rust &str.

Combines SEXP::charsxp() + Rf_installChar into one call. The symbol is interned and never garbage collected.

Source

pub fn scalar_integer(x: i32) -> SEXP

Create a length-1 integer vector.

Source

pub fn scalar_real(x: f64) -> SEXP

Create a length-1 real vector.

Source

pub fn scalar_logical(x: bool) -> SEXP

Create a length-1 logical vector.

Source

pub fn scalar_logical_raw(x: i32) -> SEXP

Create a length-1 logical vector from raw i32 (0=FALSE, 1=TRUE, NA_LOGICAL=NA). Accepts 0 (FALSE), 1 (TRUE), or NA_LOGICAL (i32::MIN) for NA. Prefer scalar_logical for non-NA values.

Source

pub fn scalar_raw(x: u8) -> SEXP

Create a length-1 raw vector.

Source

pub fn scalar_complex(x: Rcomplex) -> SEXP

Create a length-1 complex vector.

Source

pub fn scalar_string(charsxp: SEXP) -> SEXP

Create a length-1 character vector from a CHARSXP.

Source

pub fn scalar_string_from_str(s: &str) -> SEXP

Create a length-1 character vector from a Rust &str.

Source

pub fn names_symbol() -> SEXP

R’s names attribute symbol.

Source

pub fn dim_symbol() -> SEXP

R’s dim attribute symbol.

Source

pub fn dimnames_symbol() -> SEXP

R’s dimnames attribute symbol.

Source

pub fn class_symbol() -> SEXP

R’s class attribute symbol.

Source

pub fn levels_symbol() -> SEXP

R’s levels attribute symbol (factors).

Source

pub fn tsp_symbol() -> SEXP

R’s tsp attribute symbol (time series).

Source

pub fn base_namespace() -> SEXP

R’s base namespace environment.

Source

pub fn missing_arg() -> SEXP

R’s missing argument sentinel.

Trait Implementations§

Source§

impl AltrepSexpExt for SEXP

Source§

unsafe fn altrep_data1<T: TypedExternal>(&self) -> Option<ExternalPtr<T>>

Extract the ALTREP data1 slot as a typed ExternalPtr<T>. Read more
Source§

unsafe fn altrep_data1_mut_ref<T: TypedExternal>( &self, ) -> Option<&'static mut T>

Get a mutable reference to data in the ALTREP data1 slot. Read more
Source§

unsafe fn altrep_data2(&self) -> SEXP

Get the ALTREP data2 slot. Read more
Source§

unsafe fn set_altrep_data2(&self, data2: SEXP)

Set the ALTREP data2 slot. Read more
Source§

impl Clone for SEXP

Source§

fn clone(&self) -> SEXP

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SEXP

Source§

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

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

impl Default for SEXP

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<*mut SEXPREC> for SEXP

Source§

fn from(ptr: *mut SEXPREC) -> Self

Converts to this type from the input type.
Source§

impl From<SEXP> for *mut SEXPREC

Source§

fn from(sexp: SEXP) -> Self

Converts to this type from the input type.
Source§

impl IntoR for SEXP

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 PairListExt for SEXP

Source§

fn cons(self, cdr: SEXP) -> SEXP

Create a cons cell with this SEXP as CAR and cdr as CDR.
Source§

fn lcons(self, cdr: SEXP) -> SEXP

Create a language cons cell with this SEXP as CAR and cdr as CDR.
Source§

fn car(&self) -> SEXP

Get the CAR (head/value) of this pairlist node.
Source§

fn cdr(&self) -> SEXP

Get the CDR (tail/rest) of this pairlist node.
Source§

fn tag(&self) -> SEXP

Get the TAG (name symbol) of this pairlist node.
Source§

fn set_tag(&self, tag: SEXP)

Set the TAG (name symbol) of this pairlist node.
Source§

fn set_car(&self, value: SEXP) -> SEXP

Set the CAR (value) of this pairlist node.
Source§

fn set_cdr(&self, tail: SEXP) -> SEXP

Set the CDR (tail) of this pairlist node.
Source§

unsafe fn cons_unchecked(self, cdr: SEXP) -> SEXP

Create a cons cell (no thread check). Read more
Source§

unsafe fn car_unchecked(&self) -> SEXP

Get the CAR (no thread check). Read more
Source§

unsafe fn cdr_unchecked(&self) -> SEXP

Get the CDR (no thread check). Read more
Source§

unsafe fn set_tag_unchecked(&self, tag: SEXP)

Set the TAG (no thread check). Read more
Source§

unsafe fn set_car_unchecked(&self, value: SEXP) -> SEXP

Set the CAR (no thread check). Read more
Source§

unsafe fn set_cdr_unchecked(&self, tail: SEXP) -> SEXP

Set the CDR (no thread check). Read more
Source§

impl PartialEq for SEXP

Source§

fn eq(&self, other: &SEXP) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl SexpExt for SEXP

Source§

fn type_of(&self) -> SEXPTYPE

Get the type of this SEXP. Read more
Source§

fn is_null_or_nil(&self) -> bool

Check if this SEXP is null or R_NilValue.
Source§

fn len(&self) -> usize

Get the length of this SEXP as usize. Read more
Source§

fn xlength(&self) -> R_xlen_t

Get the length as R_xlen_t. Read more
Source§

unsafe fn len_unchecked(&self) -> usize

Get the length without thread checks. Read more
Source§

unsafe fn as_slice<T: RNativeType>(&self) -> &'static [T]

Get a slice view of this SEXP’s data. Read more
Source§

unsafe fn as_mut_slice<T: RNativeType>(&self) -> &'static mut [T]

Get a mutable slice view of this SEXP’s data. Read more
Source§

unsafe fn as_slice_unchecked<T: RNativeType>(&self) -> &'static [T]

Get a slice view without thread checks. Read more
Source§

fn is_integer(&self) -> bool

Check if this SEXP is an integer vector (INTSXP).
Source§

fn is_real(&self) -> bool

Check if this SEXP is a real/numeric vector (REALSXP).
Source§

fn is_logical(&self) -> bool

Check if this SEXP is a logical vector (LGLSXP).
Source§

fn is_character(&self) -> bool

Check if this SEXP is a character/string vector (STRSXP).
Source§

fn is_raw(&self) -> bool

Check if this SEXP is a raw vector (RAWSXP).
Source§

fn is_complex(&self) -> bool

Check if this SEXP is a complex vector (CPLXSXP).
Source§

fn is_list(&self) -> bool

Check if this SEXP is a list/generic vector (VECSXP).
Source§

fn is_external_ptr(&self) -> bool

Check if this SEXP is an external pointer (EXTPTRSXP).
Source§

fn is_environment(&self) -> bool

Check if this SEXP is an environment (ENVSXP).
Source§

fn is_symbol(&self) -> bool

Check if this SEXP is a symbol (SYMSXP).
Source§

fn is_language(&self) -> bool

Check if this SEXP is a language object (LANGSXP).
Source§

fn is_altrep(&self) -> bool

Check if this SEXP is an ALTREP object. Read more
Source§

fn is_empty(&self) -> bool

Check if this SEXP contains any elements.
Source§

fn is_nil(&self) -> bool

Check if this SEXP is R’s NULL (NILSXP).
Source§

fn is_factor(&self) -> bool

Check if this SEXP is a factor. Read more
Source§

fn is_pair_list(&self) -> bool

Check if this SEXP is a pairlist (LISTSXP or NILSXP). Read more
Source§

fn is_matrix(&self) -> bool

Check if this SEXP is a matrix. Read more
Source§

fn is_array(&self) -> bool

Check if this SEXP is an array. Read more
Source§

fn is_function(&self) -> bool

Check if this SEXP is a function (closure, builtin, or special). Read more
Source§

fn is_s4(&self) -> bool

Check if this SEXP is an S4 object. Read more
Source§

fn is_data_frame(&self) -> bool

Check if this SEXP is a data.frame. Read more
Source§

fn is_numeric(&self) -> bool

Check if this SEXP is a numeric type (integer, logical, or real, excluding factors). Read more
Source§

fn is_number(&self) -> bool

Check if this SEXP is a number type (numeric or complex). Read more
Source§

fn is_vector_atomic(&self) -> bool

Check if this SEXP is an atomic vector. Read more
Source§

fn is_vector_list(&self) -> bool

Check if this SEXP is a vector list (VECSXP or EXPRSXP).
Source§

fn is_vector(&self) -> bool

Check if this SEXP is a vector (atomic vector or list).
Source§

fn is_object(&self) -> bool

Check if this SEXP is an R “object” (has a class attribute).
Source§

fn coerce(&self, target: SEXPTYPE) -> SEXP

Coerce this SEXP to the given type, returning a new SEXP. Read more
Source§

fn as_logical(&self) -> Option<bool>

Extract a scalar logical value. Read more
Source§

fn as_integer(&self) -> Option<i32>

Extract a scalar integer value. Read more
Source§

fn as_real(&self) -> Option<f64>

Extract a scalar real value. Read more
Source§

fn as_char(&self) -> SEXP

Extract a scalar CHARSXP from this SEXP. Read more
Source§

fn get_attr(&self, name: SEXP) -> SEXP

Get an attribute by symbol.
Source§

fn set_attr(&self, name: SEXP, val: SEXP)

Set an attribute by symbol.
Source§

fn get_names(&self) -> SEXP

Get the names attribute.
Source§

fn set_names(&self, names: SEXP)

Set the names attribute.
Source§

fn get_class(&self) -> SEXP

Get the class attribute.
Source§

fn set_class(&self, class: SEXP)

Set the class attribute.
Source§

fn get_dim(&self) -> SEXP

Get the dim attribute.
Source§

fn set_dim(&self, dim: SEXP)

Set the dim attribute.
Source§

fn get_dimnames(&self) -> SEXP

Get the dimnames attribute.
Source§

fn set_dimnames(&self, dimnames: SEXP)

Set the dimnames attribute.
Source§

fn get_levels(&self) -> SEXP

Get the levels attribute (factors).
Source§

fn set_levels(&self, levels: SEXP)

Set the levels attribute (factors).
Source§

fn get_row_names(&self) -> SEXP

Get the row.names attribute.
Source§

fn set_row_names(&self, row_names: SEXP)

Set the row.names attribute.
Source§

fn inherits_class(&self, class: &CStr) -> bool

Check if this SEXP inherits from a class. Read more
Source§

fn string_elt(&self, i: isize) -> SEXP

Get the i-th CHARSXP element from a STRSXP. Read more
Source§

fn string_elt_str(&self, i: isize) -> Option<&str>

Get the i-th string element as Option<&str>. Read more
Source§

fn set_string_elt(&self, i: isize, charsxp: SEXP)

Set the i-th CHARSXP element of a STRSXP. Read more
Source§

fn is_na_string(&self) -> bool

Check if this CHARSXP is NA_character_.
Source§

fn vector_elt(&self, i: isize) -> SEXP

Get the i-th element of a VECSXP (generic vector / list). Read more
Source§

fn set_vector_elt(&self, i: isize, val: SEXP)

Set the i-th element of a VECSXP. Read more
Source§

fn integer_elt(&self, i: isize) -> i32

Get the i-th integer element.
Source§

fn real_elt(&self, i: isize) -> f64

Get the i-th real element.
Source§

fn logical_elt(&self, i: isize) -> i32

Get the i-th logical element (raw i32: 0/1/NA_LOGICAL).
Source§

fn complex_elt(&self, i: isize) -> Rcomplex

Get the i-th complex element.
Source§

fn raw_elt(&self, i: isize) -> u8

Get the i-th raw element.
Source§

fn set_integer_elt(&self, i: isize, v: i32)

Set the i-th integer element.
Source§

fn set_real_elt(&self, i: isize, v: f64)

Set the i-th real element.
Source§

fn set_logical_elt(&self, i: isize, v: i32)

Set the i-th logical element (raw i32: 0/1/NA_LOGICAL).
Source§

fn set_complex_elt(&self, i: isize, v: Rcomplex)

Set the i-th complex element.
Source§

fn set_raw_elt(&self, i: isize, v: u8)

Set the i-th raw element.
Source§

fn printname(&self) -> SEXP

Get the print name (CHARSXP) of a symbol (SYMSXP). Read more
Source§

fn r_char(&self) -> *const c_char

Get the C string pointer from a CHARSXP. Read more
Source§

fn r_char_str(&self) -> Option<&str>

Get a &str from a CHARSXP. Returns None for NA_character_.
Source§

fn resize(&self, newlen: R_xlen_t) -> SEXP

Resize a vector to a new length, returning a (possibly new) SEXP. Read more
Source§

fn duplicate(&self) -> SEXP

Deep-copy this SEXP. Equivalent to R’s Rf_duplicate(x).
Source§

fn shallow_duplicate(&self) -> SEXP

Shallow-copy this SEXP. Equivalent to R’s Rf_shallow_duplicate(x).
Source§

unsafe fn string_elt_unchecked(&self, i: isize) -> SEXP

Get the i-th CHARSXP from a STRSXP. No thread check. Read more
Source§

unsafe fn set_string_elt_unchecked(&self, i: isize, charsxp: SEXP)

Set the i-th CHARSXP of a STRSXP. No thread check. Read more
Source§

unsafe fn vector_elt_unchecked(&self, i: isize) -> SEXP

Get the i-th element of a VECSXP. No thread check. Read more
Source§

unsafe fn set_vector_elt_unchecked(&self, i: isize, val: SEXP)

Set the i-th element of a VECSXP. No thread check. Read more
Source§

unsafe fn get_attr_unchecked(&self, name: SEXP) -> SEXP

Get an attribute by symbol. No thread check. Read more
Source§

unsafe fn set_attr_unchecked(&self, name: SEXP, val: SEXP)

Set an attribute by symbol. No thread check. Read more
Source§

unsafe fn r_char_unchecked(&self) -> *const c_char

Get C string pointer from a CHARSXP. No thread check. Read more
Source§

fn get_attr_opt(&self, name: SEXP) -> Option<SEXP>

Get an attribute by symbol, returning None for R_NilValue.
Source§

impl TryFromSexp for SEXP

Pass-through conversion for raw SEXP values with ALTREP auto-materialization.

This allows SEXP to be used directly in #[miniextendr] function signatures. When R passes an ALTREP vector (e.g., 1:10, seq_len(N)), ensure_materialized is called automatically to force materialization on the R main thread. After this, the SEXP’s data pointer is stable and safe to access from any thread.

§ALTREP handling

InputResult
Regular SEXPPassed through unchanged
ALTREP SEXPMaterialized via ensure_materialized, then passed through

To receive ALTREP without materializing, use AltrepSexp as the parameter type instead. To receive the raw SEXP without any conversion (including no materialization), use extern "C-unwind".

See docs/ALTREP_SEXP.md for the full guide.

§Safety

SEXP handles are only valid on R’s main thread. Use with #[miniextendr(unsafe(main_thread))] functions.

Source§

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

Converts a SEXP, auto-materializing ALTREP vectors.

If the input is ALTREP, ensure_materialized is called to force materialization on the R main thread. After materialization the data pointer is stable and the SEXP can be safely sent to other threads.

Source§

type Error = SexpError

The error type returned when conversion fails.
Source§

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

Convert from SEXP without thread safety checks. Read more
Source§

impl Copy for SEXP

Source§

impl Eq for SEXP

Source§

impl Send for SEXP

Source§

impl StructuralPartialEq for SEXP

Source§

impl Sync for SEXP

Auto Trait Implementations§

§

impl Freeze for SEXP

§

impl RefUnwindSafe for SEXP

§

impl Unpin for SEXP

§

impl UnsafeUnpin for SEXP

§

impl UnwindSafe for SEXP

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> RClone for T
where T: Clone,

Source§

fn clone(&self) -> T

Create a deep copy of this value.
Source§

impl<T> RCopy for T
where T: Copy,

Source§

fn copy(&self) -> T

Create a bitwise copy of this value. Read more
Source§

fn is_copy(&self) -> bool

Check if this type implements Copy. Read more
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> RDefault for T
where T: Default,

Source§

fn default() -> T

Create a new instance with default values.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.