Expand description
§Impl-block Parsing and Wrapper Generation
This module handles #[miniextendr] applied to inherent impl blocks,
generating R wrappers for different class systems.
§Architecture Overview
┌─────────────────────────────────────────────────────────────────────────┐
│ #[miniextendr(r6)] │
│ impl MyType { ... } │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ PARSING PHASE │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐ │
│ │ ImplAttrs │ │ ParsedMethod │ │ ParsedImpl │ │
│ │ - class_system │ │ - ident │───▶│ - type_ident │ │
│ │ - class_name │ │ - receiver │ │ - class_system │ │
│ └─────────────────┘ │ - sig │ │ - methods[] │ │
│ │ - doc_tags │ │ - doc_tags │ │
│ │ - method_attrs │ └─────────────────────┘ │
│ └─────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ CODE GENERATION PHASE │
│ │
│ For each method: │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ generate_c_wrapper_for_method() │ │
│ │ └─▶ CWrapperContext (shared builder from c_wrapper_builder) │ │
│ │ - thread strategy (main vs worker) │ │
│ │ - SEXP→Rust conversion │ │
│ │ - return handling │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │
│ For the whole impl: │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ generate_{class_system}_r_wrapper() │ │
│ │ - generate_env_r_wrapper() → Type$method(self, ...) │ │
│ │ - generate_r6_r_wrapper() → R6Class with methods │ │
│ │ - generate_s3_r_wrapper() → generic + method.Type │ │
│ │ - generate_s4_r_wrapper() → setClass + setMethod │ │
│ │ - generate_s7_r_wrapper() → new_class + method<- │ │
│ └─────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ OUTPUTS │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────────┐ │
│ │ C wrapper fns │ │ R wrapper code │ │ R_CallMethodDef │ │
│ │ C_Type__method │ │ (as const str) │ │ registration entries │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘§Supported Class Systems
| System | Syntax | R Pattern | Use Case |
|---|---|---|---|
| Env | #[miniextendr] | obj$method() | Simple, environment-based dispatch |
| R6 | #[miniextendr(r6)] | R6Class with $new() | OOP with encapsulation |
| S3 | #[miniextendr(s3)] | generic(obj) dispatch | Idiomatic R generics |
| S4 | #[miniextendr(s4)] | setClass/setMethod | Formal OOP, multiple dispatch |
| S7 | #[miniextendr(s7)] | new_class/new_generic | Modern R OOP |
| vctrs | #[miniextendr(vctrs)] | new_vctr/new_rcrd/new_list_of | vctrs-compatible vectors |
§Method Categorization
Methods are categorized by their receiver type:
| Receiver | ReceiverKind | Generated as |
|---|---|---|
&self | Ref | Instance method (immutable) |
&mut self | RefMut | Instance method (mutable, chainable) |
self: &ExternalPtr<Self> | ExternalPtrRef | Instance method (immutable, full ExternalPtr access) |
self: &mut ExternalPtr<Self> | ExternalPtrRefMut | Instance method (mutable, full ExternalPtr access) |
self: ExternalPtr<Self> | ExternalPtrValue | Instance method (owned ExternalPtr, full access) |
self | Value | Consuming method (not supported in v1) |
| (none) | None | Static method or constructor |
Special methods:
- Constructor: Returns
Self, marked with#[miniextendr(constructor)]or namednew - Finalizer: R6 only, marked with
#[miniextendr(r6(finalize))] - Private: R6 only, marked with
#[miniextendr(r6(private))]
§Shared Builders
This module uses shared infrastructure from:
crate::c_wrapper_builder: C wrapper generation with thread strategycrate::r_wrapper_builder: R function signatures and.Call()argscrate::method_return_builder: Return value handling per class systemcrate::roxygen: Documentation extraction from Rust doc comments
§Example
ⓘ
#[miniextendr(r6)]
impl Counter {
fn new(value: i32) -> Self { Counter { value } }
fn get(&self) -> i32 { self.value }
fn increment(&mut self) { self.value += 1; }
}Generates:
- C wrappers:
C_Counter__new,C_Counter__get,C_Counter__increment - R6Class with
initialize,get,incrementmethods - Registration entries for R’s
.Call()interface
Modules§
- env_
class 🔒 - Environment-based class wrapper generator (
obj$method()dispatch). Env-class R wrapper generator. - r6_
class 🔒 - R6 class wrapper generator (
R6Classwith$new(), active bindings, private methods). R6-class R wrapper generator. - s3_
class 🔒 - S3 class wrapper generator (
structure()+generic.Classdispatch). S3-class R wrapper generator. - s4_
class 🔒 - S4 class wrapper generator (
setClass/setMethodformal OOP). S4-class R wrapper generator. - s7_
class 🔒 - S7 class wrapper generator (
new_class/new_genericmodern R OOP). S7-class R wrapper generator. - vctrs_
class 🔒 - vctrs-compatible class wrapper generator (
new_vctr/new_rcrd/new_list_of). Vctrs class R wrapper generator.
Structs§
- Impl
Attrs - Attributes parsed from
#[miniextendr(...)]on an impl block. - Method
Attrs - Per-method attributes for class system customization.
- Parsed
Impl - Fully parsed
#[miniextendr]impl block, ready for code generation. - Parsed
Method - Parsed method from an impl block.
- R6Method
Attrs - R6-specific per-method markers, separated from
MethodAttrsso ther6parser branch and R6 class generator own a self-contained bag. - S7Method
Attrs - S7-specific per-method markers, separated from
MethodAttrsso the S7 class generator has a self-contained bag of its own state (property getters/setters, generic-dispatch controls, convert() wiring) and the other class generators don’t have to look past them. - Vctrs
Attrs - Attributes for vctrs class generation.
Enums§
- Class
System - Class system flavor for wrapper generation.
- Receiver
Kind - Receiver kind for methods.
- Vctrs
Kind - Kind of vctrs class being created.
Functions§
- expand_
impl - Top-level entry point for expanding
#[miniextendr]on impl blocks. - find_
param_ 🔒type - Find a parameter’s Rust type from a stripped signature by identifier name.
- generate_
as_ coercion_ methods - Generate R S3 method wrappers for
as.<class>()coercion methods. - generate_
as_ coercion_ trait_ impls - Generate
impl As*trait impls for methods with#[miniextendr(as = "...")]. - generate_
method_ c_ wrapper - Generate a C-callable wrapper function for a single method in an impl block.
- generate_
method_ 🔒match_ arg_ helpers - Generate
__match_arg_choices__<param>helper C wrappers plus the two linkme registrations (MX_CALL_DEFSandMX_MATCH_ARG_CHOICES) per match_arg parameter. - is_
external_ 🔒ptr_ type - Check if a type is
ExternalPtr<...>(possibly fully qualified). - output_
is_ 🔒result - Check whether a function’s return type is syntactically
Result<_, _>. - replace_
self_ 🔒in_ tokens - Replace every occurrence of the
selfkeyword/ident in a TokenStream with a replacement identifier. Does NOT touchSelf(capital S). - rewrite_
external_ 🔒ptr_ receivers - Rewrite methods with ExternalPtr-based receivers so they compile on stable Rust
(which lacks
arbitrary_self_types). - strip_
miniextendr_ 🔒attrs_ from_ impl - Strip
#[miniextendr(...)]attributes and roxygen doc tags from an impl block and all of its items (functions, constants, types, macros). - ty_
is_ 🔒self_ or_ named - Recursively checks whether
tyisSelf,&Self,&mut Self,Box<Self>, the named type, orResult<(Self | NamedType), _>. - vctrs_
ctor_ 🔒returns_ self_ or_ type - Returns true if a vctrs constructor’s return type is
Self,&Self,&mut Self, the named impl type,Box<Self>,Result<Self, _>, orResult<NamedType, _>.