Expand description
§#[miniextendr] on trait impls - Trait Implementation Registration
This module handles #[miniextendr] applied to trait implementations,
generating the vtable static for cross-package trait dispatch, plus optional
R-callable wrappers for direct method access.
§Overview
When #[miniextendr] is applied to an impl Trait for Type block, it:
- Detects the trait from the impl syntax (no attribute args needed)
- Generates vtable static using the trait’s
__<trait>_build_vtablefunction - Generates C wrappers for each trait method (for R
.Callaccess) - Generates R wrapper code for the trait methods
- Passes through the original impl block unchanged
§Usage
use miniextendr_api::miniextendr;
// The trait must have been defined with #[miniextendr]
// which generates __counter_build_vtable::<T>()
struct MyCounter { value: i32 }
#[miniextendr]
impl Counter for MyCounter {
fn value(&self) -> i32 {
self.value
}
fn increment(&mut self) {
self.value += 1;
}
fn add(&mut self, n: i32) {
self.value += n;
}
}Generates (conceptually):
// Original impl block (passed through)
impl Counter for MyCounter {
fn value(&self) -> i32 { self.value }
fn increment(&mut self) { self.value += 1; }
fn add(&mut self, n: i32) { self.value += n; }
}
// Generated vtable static
pub static __VTABLE_COUNTER_FOR_MYCOUNTER: CounterVTable =
__counter_build_vtable::<MyCounter>();§How It Works
- Parse
impl Trait for Typeto extract trait path and concrete type - Generate vtable static name:
__VTABLE_{TRAIT}_FOR_{TYPE} - Generate vtable builder call:
__{trait}_build_vtable::<Type>() - The vtable builder was generated by
#[miniextendr]on the trait
§Trait Detection
The macro reads the trait directly from the impl syntax:
#[miniextendr]
impl path::to::Counter for MyType { ... }
// ^^^^^^^^^^^^^^^^^ detected automaticallyNo extra arguments are needed; the trait path is explicit in the impl syntax.
§Name Generation
- Vtable static:
__VTABLE_{TRAIT}_FOR_{TYPE}(uppercase, underscores) - Vtable builder:
__{trait}_build_vtable(lowercase)
For impl foo::Counter for my_mod::MyType:
- Static:
__VTABLE_COUNTER_FOR_MYTYPE - Builder call:
foo::__counter_build_vtable::<my_mod::MyType>()
§Integration with ExternalPtr / TypedExternal
The generated vtable static is automatically registered via linkme distributed slices.
#[derive(ExternalPtr)]
struct MyCounter { value: i32 }
#[miniextendr]
impl Counter for MyCounter { /* ... */ }ExternalPtr<T> provides the type identity for the external pointer.
Trait dispatch is wired automatically.
§Thread Safety
The generated vtable is a static constant, safe to access from any thread.
Trait shims now mirror inherent impls: instance methods stay on the main
thread, while static trait methods run on the worker thread unless
main_thread is explicitly requested.
Modules§
- r_
wrappers 🔒 - R wrapper generation for trait methods across all class systems.
- vtable 🔒
- Vtable static generation, C wrapper generation, and method attribute parsing for trait impls.
Structs§
- Tpie
Input 🔒 - Input to the
__mx_trait_impl_expand!proc macro. - Tpie
Method 🔒 - A single method entry in TPIE metadata.
- Trait
Const 🔒 - Parsed associated constant from a trait impl block.
- Trait
Method 🔒 - Parsed method from a trait impl block.
Functions§
- expand_
miniextendr_ impl_ trait - Expand
#[miniextendr]applied to a trait implementation. - expand_
tpie - Entry point for the
__mx_trait_impl_expand!proc macro. - extract_
trait_ 🔒and_ type - Extract the trait path and concrete type from an impl block.
- generate_
tpie_ 🔒invocation - Generate vtable static + TPIE macro invocation for an empty trait impl.
- rewrite_
self_ 🔒in_ sig - Rewrite
Self→ concrete type in a method signature. - rewrite_
self_ 🔒type - Recursively replace
Selfwith the concrete type in a type tree. - trait_
method_ 🔒body_ lines - Generate R function body lines with optional error_in_r checking.
- trait_
method_ 🔒preamble_ lines - Generate R function body preamble lines (r_entry, on.exit, lifecycle, r_post_checks).
- type_
to_ 🔒uppercase_ name - Convert a type to an uppercase identifier-safe name.
- unwrap_
group_ 🔒type - Unwrap invisible Group tokens from
macro_rules!$t:tycaptures.