Skip to main content

Module miniextendr_impl

Module miniextendr_impl 

Source
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

SystemSyntaxR PatternUse Case
Env#[miniextendr]obj$method()Simple, environment-based dispatch
R6#[miniextendr(r6)]R6Class with $new()OOP with encapsulation
S3#[miniextendr(s3)]generic(obj) dispatchIdiomatic R generics
S4#[miniextendr(s4)]setClass/setMethodFormal OOP, multiple dispatch
S7#[miniextendr(s7)]new_class/new_genericModern R OOP
vctrs#[miniextendr(vctrs)]new_vctr/new_rcrd/new_list_ofvctrs-compatible vectors

§Method Categorization

Methods are categorized by their receiver type:

ReceiverReceiverKindGenerated as
&selfRefInstance method (immutable)
&mut selfRefMutInstance method (mutable, chainable)
self: &ExternalPtr<Self>ExternalPtrRefInstance method (immutable, full ExternalPtr access)
self: &mut ExternalPtr<Self>ExternalPtrRefMutInstance method (mutable, full ExternalPtr access)
self: ExternalPtr<Self>ExternalPtrValueInstance method (owned ExternalPtr, full access)
selfValueConsuming method (not supported in v1)
(none)NoneStatic method or constructor

Special methods:

  • Constructor: Returns Self, marked with #[miniextendr(constructor)] or named new
  • Finalizer: R6 only, marked with #[miniextendr(r6(finalize))]
  • Private: R6 only, marked with #[miniextendr(r6(private))]

§Shared Builders

This module uses shared infrastructure from:

§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, increment methods
  • 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 (R6Class with $new(), active bindings, private methods). R6-class R wrapper generator.
s3_class 🔒
S3 class wrapper generator (structure() + generic.Class dispatch). S3-class R wrapper generator.
s4_class 🔒
S4 class wrapper generator (setClass / setMethod formal OOP). S4-class R wrapper generator.
s7_class 🔒
S7 class wrapper generator (new_class / new_generic modern 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§

ImplAttrs
Attributes parsed from #[miniextendr(...)] on an impl block.
MethodAttrs
Per-method attributes for class system customization.
ParsedImpl
Fully parsed #[miniextendr] impl block, ready for code generation.
ParsedMethod
Parsed method from an impl block.
R6MethodAttrs
R6-specific per-method markers, separated from MethodAttrs so the r6 parser branch and R6 class generator own a self-contained bag.
S7MethodAttrs
S7-specific per-method markers, separated from MethodAttrs so 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.
VctrsAttrs
Attributes for vctrs class generation.

Enums§

ClassSystem
Class system flavor for wrapper generation.
ReceiverKind
Receiver kind for methods.
VctrsKind
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_DEFS and MX_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 self keyword/ident in a TokenStream with a replacement identifier. Does NOT touch Self (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 ty is Self, &Self, &mut Self, Box<Self>, the named type, or Result<(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, _>, or Result<NamedType, _>.