pub trait TraitView: Sized {
const TAG: mx_tag;
// Required method
unsafe fn from_raw_parts(data: *mut c_void, vtable: *const c_void) -> Self;
// Provided methods
unsafe fn try_from_sexp(sexp: SEXP) -> Option<Self> { ... }
unsafe fn try_from_sexp_or_error(
sexp: SEXP,
trait_name: &str,
) -> Result<Self, String> { ... }
}Expand description
Trait for view types that can be created from SEXP via trait dispatch.
This trait is implemented by the macro-generated <Trait>View structs.
It provides a common interface for:
- Querying whether an object implements a trait
- Creating a view from an SEXP
§Generated by #[miniextendr] on traits
When you write:
#[miniextendr]
pub trait Counter {
fn value(&self) -> i32;
fn increment(&mut self);
}The macro generates CounterView that implements TraitView:
impl TraitView for CounterView {
const TAG: mx_tag = TAG_COUNTER;
unsafe fn from_raw_parts(data: *mut c_void, vtable: *const c_void) -> Self {
Self {
data,
vtable: vtable.cast::<CounterVTable>(),
}
}
}§Usage
// Try to get a Counter view from an R object
let view = CounterView::try_from_sexp(obj)?;
// Call methods through the view
view.increment();
let val = view.value();§Safety
The from_raw_parts method is unsafe because:
datamust be a valid pointer to the concrete objectvtablemust be a valid pointer to the trait’s vtable- The pointers must remain valid for the lifetime of the view
Required Associated Constants§
Required Methods§
Sourceunsafe fn from_raw_parts(data: *mut c_void, vtable: *const c_void) -> Self
unsafe fn from_raw_parts(data: *mut c_void, vtable: *const c_void) -> Self
Create a view from raw data and vtable pointers.
§Safety
datamust be a valid, non-null pointer to the concrete objectvtablemust be a valid, non-null pointer to the trait’s vtable- Both pointers must remain valid for the lifetime of the view
Provided Methods§
Sourceunsafe fn try_from_sexp(sexp: SEXP) -> Option<Self>
unsafe fn try_from_sexp(sexp: SEXP) -> Option<Self>
Try to create a view from an R SEXP.
Queries the object for this trait’s vtable using mx_query. If the
object implements the trait, returns Some(view). Otherwise returns None.
§Safety
sexpmust be a valid R external pointer (EXTPTRSXP)- Must be called on R’s main thread
§Returns
Some(Self)if the object implements the traitNoneif the object does not implement the trait
§Example
let counter = unsafe { CounterView::try_from_sexp(obj) }
.expect("Object does not implement Counter");Sourceunsafe fn try_from_sexp_or_error(
sexp: SEXP,
trait_name: &str,
) -> Result<Self, String>
unsafe fn try_from_sexp_or_error( sexp: SEXP, trait_name: &str, ) -> Result<Self, String>
Try to create a view, returning an error message on failure.
Similar to try_from_sexp but returns an error string suitable for
use as an R error message if the object does not implement the trait.
§Safety
Same as try_from_sexp.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.