pub trait TraitBridgeGenerator {
// Required methods
fn foreign_object_type(&self) -> &str;
fn bridge_imports(&self) -> Vec<String>;
fn gen_sync_method_body(
&self,
method: &MethodDef,
spec: &TraitBridgeSpec<'_>,
) -> String;
fn gen_async_method_body(
&self,
method: &MethodDef,
spec: &TraitBridgeSpec<'_>,
) -> String;
fn gen_constructor(&self, spec: &TraitBridgeSpec<'_>) -> String;
fn gen_registration_fn(&self, spec: &TraitBridgeSpec<'_>) -> String;
fn registration_fn_attr(&self) -> &str;
}Expand description
Backend-specific trait bridge generation.
Each binding backend (PyO3, NAPI-RS, wasm-bindgen, etc.) implements this trait to provide the language-specific parts of bridge codegen. The shared functions in this module call these methods to fill in the backend-dependent pieces.
Required Methods§
Sourcefn foreign_object_type(&self) -> &str
fn foreign_object_type(&self) -> &str
The type of the wrapped foreign object (e.g., "Py<PyAny>", "ThreadsafeFunction").
Sourcefn bridge_imports(&self) -> Vec<String>
fn bridge_imports(&self) -> Vec<String>
Additional use imports needed for the bridge code.
Sourcefn gen_sync_method_body(
&self,
method: &MethodDef,
spec: &TraitBridgeSpec<'_>,
) -> String
fn gen_sync_method_body( &self, method: &MethodDef, spec: &TraitBridgeSpec<'_>, ) -> String
Generate the body of a synchronous method bridge.
The returned string is inserted inside the trait impl method. It should call through to the foreign object and convert the result.
Sourcefn gen_async_method_body(
&self,
method: &MethodDef,
spec: &TraitBridgeSpec<'_>,
) -> String
fn gen_async_method_body( &self, method: &MethodDef, spec: &TraitBridgeSpec<'_>, ) -> String
Generate the body of an async method bridge.
The returned string is the body of a Box::pin(async move { ... }) block.
Sourcefn gen_constructor(&self, spec: &TraitBridgeSpec<'_>) -> String
fn gen_constructor(&self, spec: &TraitBridgeSpec<'_>) -> String
Generate the constructor body that validates and wraps the foreign object.
Should check that the foreign object provides all required methods and
return Self { ... } on success.
Sourcefn gen_registration_fn(&self, spec: &TraitBridgeSpec<'_>) -> String
fn gen_registration_fn(&self, spec: &TraitBridgeSpec<'_>) -> String
Generate the body of the registration function.
This is the code inside the register_xxx() function that wraps the
foreign object, builds the bridge, and inserts it into the registry.
Sourcefn registration_fn_attr(&self) -> &str
fn registration_fn_attr(&self) -> &str
The attribute placed on the registration function (e.g., "#[pyfunction]", "#[napi]").