#[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;
}
})
};Trait Implementations§
Source§impl<T: RNativeType, const NDIM: usize> IntoR for RArray<T, NDIM>
impl<T: RNativeType, const NDIM: usize> IntoR for RArray<T, NDIM>
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§unsafe fn try_into_sexp_unchecked(self) -> Result<SEXP, Self::Error>
unsafe fn try_into_sexp_unchecked(self) -> Result<SEXP, Self::Error>
Source§unsafe fn into_sexp_unchecked(self) -> SEXP
unsafe fn into_sexp_unchecked(self) -> SEXP
Source§impl<T: RNativeType, const NDIM: usize> TryFromSexp for RArray<T, NDIM>
impl<T: RNativeType, const NDIM: usize> TryFromSexp for RArray<T, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<bool, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<bool, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<f32, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<f32, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<i16, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<i16, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<i64, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<i64, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<i8, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<i8, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<isize, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<isize, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<u16, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<u16, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<u32, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<u32, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<u64, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<u64, NDIM>
Source§impl<const NDIM: usize> TryFromSexp for RArray<usize, NDIM>
impl<const NDIM: usize> TryFromSexp for RArray<usize, NDIM>
impl<T: Copy, const NDIM: usize> Copy for RArray<T, NDIM>
Auto Trait Implementations§
impl<T, const NDIM: usize> Freeze for RArray<T, NDIM>
impl<T, const NDIM: usize> RefUnwindSafe for RArray<T, NDIM>where
T: RefUnwindSafe,
impl<T, const NDIM: usize> !Send for RArray<T, NDIM>
impl<T, const NDIM: usize> !Sync for RArray<T, NDIM>
impl<T, const NDIM: usize> Unpin for RArray<T, NDIM>
impl<T, const NDIM: usize> UnsafeUnpin for RArray<T, NDIM>
impl<T, const NDIM: usize> UnwindSafe for RArray<T, NDIM>where
T: RefUnwindSafe,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> Printable for T
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