Skip to main content

ExternalPtr

Derive Macro ExternalPtr 

Source
#[derive(ExternalPtr)]
{
    // Attributes available to this derive:
    #[externalptr]
    #[r_data]
}
Expand description

Derive macro for implementing TypedExternal on a type.

This makes the type compatible with ExternalPtr<T> for storing in R’s external pointers.

§Basic Usage

use miniextendr_api::TypedExternal;

#[derive(ExternalPtr)]
struct MyData {
    value: i32,
}

// Now you can use ExternalPtr<MyData>
let ptr = ExternalPtr::new(MyData { value: 42 });

§Trait ABI

Trait dispatch wrappers are automatically generated:

use miniextendr_api::miniextendr;

#[derive(ExternalPtr)]
struct MyCounter {
    value: i32,
}

#[miniextendr]
impl Counter for MyCounter {
    fn value(&self) -> i32 { self.value }
    fn increment(&mut self) { self.value += 1; }
}

This generates additional infrastructure for type-erased trait dispatch:

  • __MxWrapperMyCounter - Type-erased wrapper struct
  • __MX_BASE_VTABLE_MYCOUNTER - Base vtable with drop/query
  • __mx_wrap_mycounter() - Constructor returning *mut mx_erased

§Generated Code (Basic)

For a type MyData without traits:

impl TypedExternal for MyData {
    const TYPE_NAME: &'static str = "MyData";
    const TYPE_NAME_CSTR: &'static [u8] = b"MyData\0";
}