Skip to main content

Module trait_bridge

Module trait_bridge 

Source
Expand description

Shared trait bridge code generation.

Generates wrapper structs that allow foreign language objects (Python, JS, etc.) to implement Rust traits via FFI. Each backend implements TraitBridgeGenerator to provide language-specific dispatch logic; the shared functions in this module handle the structural boilerplate.

Structs§

BridgeFieldMatch
Match info for a trait bridge whose handle lives as a struct field (bind_via = "options_field").
BridgeOutput
Result of trait bridge generation: imports (to be added via builder.add_import) and the code body (to be added via builder.add_item).
TraitBridgeSpec
Everything needed to generate a trait bridge for one trait.

Traits§

TraitBridgeGenerator
Backend-specific trait bridge generation.

Functions§

bridge_param_type
Map a TypeRef to its Rust source type string for use in trait bridge method signatures. ci is the core import path (e.g. "kreuzberg"), tp maps type names to fully-qualified paths.
find_bridge_field
Find the first function parameter whose IR type carries a bridge field (bind_via = "options_field").
find_bridge_param
Find the first function parameter that matches a trait bridge configuration (by type alias or parameter name).
format_param_type
Format a parameter type, respecting is_ref, is_mut, and optional from the IR.
format_return_type
Format a return type, wrapping in Result when an error type is present.
format_type_ref
Format a TypeRef as a Rust type string for use in trait method signatures.
gen_bridge_all
Generate the complete trait bridge code block: struct, impls, and optionally a registration function.
gen_bridge_clear_fn
Generate the clear_xxx() function that removes all registered plugins of this type.
gen_bridge_plugin_impl
Generate impl SuperTrait for Wrapper when the bridge config specifies a super-trait.
gen_bridge_registration_fn
Generate the register_xxx() function that wraps a foreign object and inserts it into the plugin registry.
gen_bridge_trait_impl
Generate impl Trait for Wrapper dispatching each method through the generator.
gen_bridge_unregistration_fn
Generate the unregister_xxx(name) function that removes a previously registered plugin from the registry.
gen_bridge_wrapper_struct
Generate the wrapper struct holding the foreign object and cached fields.
host_function_path
Resolve the FQN of a host-crate plugin function (e.g. kreuzberg::plugins::ocr::unregister_ocr_backend) given the bridge’s registry_getter path. The convention used by host crates is:
prim
Map a Rust primitive to its type string.
to_camel_case
Convert a snake_case string to camelCase.
visitor_param_type
Map a visitor method parameter type to the correct Rust type string, handling IR quirks: