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;
// Provided method
fn async_trait_is_send(&self) -> bool { ... }
}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 complete registration function including attributes, signature, and body.
Each backend needs different function signatures (PyO3 takes py: Python,
NAPI takes #[napi] with JS params, FFI takes extern "C" with raw pointers),
so the generator owns the full function.
Provided Methods§
Sourcefn async_trait_is_send(&self) -> bool
fn async_trait_is_send(&self) -> bool
Whether the #[async_trait] macro should require Send on its futures.
Returns true (default) for most targets. WASM is single-threaded so its
trait bounds don’t include Send; implementors should return false there.