Skip to main content

generate_concrete_vtable_shims

Function generate_concrete_vtable_shims 

Source
fn generate_concrete_vtable_shims(
    methods: &[TraitMethod],
    type_ident: &Ident,
    trait_name: &Ident,
    trait_path: &Path,
    concrete_type: &Type,
) -> TokenStream
Expand description

Generate concrete vtable shims for a generic trait impl.

For generic traits (e.g., RExtend<T>), shims and the vtable builder function cannot be generated at the trait definition site because where clauses like Vec<T>: TryFromSexp cause recursive trait resolution overflow. Instead, we generate fully monomorphized shims at the impl site where T is known (e.g., T = i32).

Each instance method gets a concrete unsafe extern "C" shim named __vtshim_{Type}__{Trait}__{method} that:

  1. Checks argument arity
  2. Wraps everything in with_r_unwind_protect
  3. Extracts SEXP arguments to concrete Rust types
  4. Calls the method via fully-qualified syntax <Type as Trait>::method()
  5. Converts the result back to SEXP

Static methods are skipped (not part of the vtable).