#[repr(transparent)]pub struct RArray<T, const NDIM: usize> {
sexp: SEXP,
_marker: PhantomData<*const T>,
}Expand description
An N-dimensional R array.
This type wraps an R array SEXP. The dimension count NDIM is tracked
at compile time, but dimension sizes are read from the R object.
§Type Parameters
T: The element type, must implementRNativeTypeNDIM: The number of dimensions (compile-time constant)
§Thread Safety
This type is !Send and !Sync because its methods require access to
R APIs that must run on the R main thread.
Fields§
§sexp: SEXP§_marker: PhantomData<*const T>Implementations§
Source§impl<T, const NDIM: usize> RArray<T, NDIM>
impl<T, const NDIM: usize> RArray<T, NDIM>
Sourcepub const unsafe fn from_sexp_unchecked(sexp: SEXP) -> Self
pub const unsafe fn from_sexp_unchecked(sexp: SEXP) -> Self
Create an RArray from a SEXP without validation.
§Safety
- The SEXP must be protected from GC
- The SEXP must have the correct type for
T - The SEXP must have exactly
NDIMdimensions
Sourcepub fn into_inner(self) -> SEXP
pub fn into_inner(self) -> SEXP
Consume and return the underlying SEXP.
Source§impl<T: RNativeType, const NDIM: usize> RArray<T, NDIM>
impl<T: RNativeType, const NDIM: usize> RArray<T, NDIM>
Sourcepub unsafe fn as_slice_mut(&mut self) -> &mut [T]
pub unsafe fn as_slice_mut(&mut self) -> &mut [T]
Get the data as a mutable slice (column-major order).
§Safety
- The SEXP must be protected and valid
- No other references to the data may exist
Sourcepub unsafe fn to_vec(&self) -> Vec<T>where
T: Copy,
pub unsafe fn to_vec(&self) -> Vec<T>where
T: Copy,
Copy array data to an owned Vec<T>.
This method copies the data, making it safe to use in worker threads or pass to parallel computation. The copy is performed on the current thread (which must be the R main thread).
§Safety
The SEXP must be protected and valid.
§Example
use miniextendr_api::rarray::RMatrix;
#[miniextendr(unsafe(main_thread))]
fn process_matrix(m: RMatrix<f64>) -> f64 {
// Copy data - Vec<f64> is Send and can be used in worker threads
let data: Vec<f64> = unsafe { m.to_vec() };
// Now data can be passed to parallel computation
data.iter().sum()
}Source§impl<T: RNativeType> RArray<T, 2>
impl<T: RNativeType> RArray<T, 2>
Sourcepub unsafe fn set_rc(&mut self, row: usize, col: usize, value: T)where
T: Copy,
pub unsafe fn set_rc(&mut self, row: usize, col: usize, value: T)where
T: Copy,
Set an element by row and column.
§Safety
- The SEXP must be protected and valid
- No other references to the data may exist
Source§impl<T: RNativeType, const NDIM: usize> RArray<T, NDIM>
impl<T: RNativeType, const NDIM: usize> RArray<T, NDIM>
Sourcefn get_attr_opt(&self, name: SEXP) -> Option<SEXP>
fn get_attr_opt(&self, name: SEXP) -> Option<SEXP>
Get an arbitrary attribute by symbol (unchecked internal helper).
§Safety
- The SEXP must be valid.
whatmust be a valid symbol SEXP.
Sourcepub unsafe fn get_dimnames(&self) -> Option<SEXP>
pub unsafe fn get_dimnames(&self) -> Option<SEXP>
Get the dimnames attribute if present.
Equivalent to R’s GET_DIMNAMES(x).
§Safety
The SEXP must be valid.
Sourcepub unsafe fn get_rownames(&self) -> Option<SEXP>
pub unsafe fn get_rownames(&self) -> Option<SEXP>
Get row names from the dimnames attribute.
Equivalent to R’s GET_ROWNAMES(x) / Rf_GetRowNames(x).
§Safety
The SEXP must be valid.
Sourcepub unsafe fn get_colnames(&self) -> Option<SEXP>
pub unsafe fn get_colnames(&self) -> Option<SEXP>
Get column names from the dimnames attribute.
Equivalent to R’s GET_COLNAMES(x) / Rf_GetColNames(x).
§Safety
The SEXP must be valid.
Sourcepub unsafe fn set_class(&mut self, class: SEXP)
pub unsafe fn set_class(&mut self, class: SEXP)
Set the class attribute.
Equivalent to R’s SET_CLASS(x, n).
§Safety
The SEXP must be valid and not shared.
Sourcepub unsafe fn set_dimnames(&mut self, dimnames: SEXP)
pub unsafe fn set_dimnames(&mut self, dimnames: SEXP)
Set the dimnames attribute.
Equivalent to R’s SET_DIMNAMES(x, n).
§Safety
The SEXP must be valid and not shared.
Source§impl<T: RNativeType, const NDIM: usize> RArray<T, NDIM>
impl<T: RNativeType, const NDIM: usize> RArray<T, NDIM>
Sourcepub unsafe fn new<F>(dims: [usize; NDIM], init: F) -> Self
pub unsafe fn new<F>(dims: [usize; NDIM], init: F) -> Self
Allocate a new R array with the given dimensions.
The array is allocated. The closure receives a mutable slice to initialize the data.
§Safety
Must be called from the R main thread (or via routed FFI). The returned RArray holds an unprotected SEXP - caller must protect.
§Example
let matrix = unsafe {
RMatrix::<f64>::new([3, 4], |slice| {
for (i, v) in slice.iter_mut().enumerate() {
*v = i as f64;
}
})
};