Skip to main content

TraitView

Trait TraitView 

Source
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:

  • data must be a valid pointer to the concrete object
  • vtable must be a valid pointer to the trait’s vtable
  • The pointers must remain valid for the lifetime of the view

Required Associated Constants§

Source

const TAG: mx_tag

The type tag for this trait.

This is a compile-time constant generated by #[miniextendr] on the trait.

Required Methods§

Source

unsafe fn from_raw_parts(data: *mut c_void, vtable: *const c_void) -> Self

Create a view from raw data and vtable pointers.

§Safety
  • data must be a valid, non-null pointer to the concrete object
  • vtable must be a valid, non-null pointer to the trait’s vtable
  • Both pointers must remain valid for the lifetime of the view

Provided Methods§

Source

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
  • sexp must be a valid R external pointer (EXTPTRSXP)
  • Must be called on R’s main thread
§Returns
  • Some(Self) if the object implements the trait
  • None if the object does not implement the trait
§Example
let counter = unsafe { CounterView::try_from_sexp(obj) }
    .expect("Object does not implement Counter");
Source

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".

Implementors§

Source§

impl TraitView for RCloneView

Source§

const TAG: mx_tag = TAG_RCLONE

Source§

impl TraitView for RCopyView

Source§

const TAG: mx_tag = TAG_RCOPY

Source§

impl TraitView for RDebugView

Source§

const TAG: mx_tag = TAG_RDEBUG

Source§

impl TraitView for RDefaultView

Source§

const TAG: mx_tag = TAG_RDEFAULT

Source§

impl TraitView for RDisplayView

Source§

const TAG: mx_tag = TAG_RDISPLAY

Source§

impl TraitView for RErrorView

Source§

const TAG: mx_tag = TAG_RERROR

Source§

impl TraitView for RFromStrView

Source§

const TAG: mx_tag = TAG_RFROMSTR

Source§

impl TraitView for RHashView

Source§

const TAG: mx_tag = TAG_RHASH

Source§

impl TraitView for RIteratorView

Source§

const TAG: mx_tag = TAG_RITERATOR

Source§

impl TraitView for ROrdView

Source§

const TAG: mx_tag = TAG_RORD

Source§

impl TraitView for RPartialOrdView

Source§

const TAG: mx_tag = TAG_RPARTIALORD