pub struct Codegen { /* private fields */ }
Expand description
Engine for automatic generation of Rust FFI bindings to Python modules.
§Examples
Here is a simple example of how to use the Codegen
engine to generate
Rust FFI bindings for the full os
and sys
Python modules. With the
default configuration, all submodules, classes, functions, and parameters
will be recursively parsed and included in the generated bindings.
fn main() -> Result<(), Box<dyn std::error::Error>> {
Codegen::new(Config::default())
.module_name("os")?
.module_name("sys")?
.generate()?;
Ok(())
}
For more focused generation, paths to specific submodules can be provided.
In the following example, only the entities
and parser
submodules of the
html
module will be included in the generated bindings alongside their
respective submodules, classes, functions, and parameters. No direct attributes
or submodules of the html
top-level module will be included.
fn main() -> Result<(), Box<dyn std::error::Error>> {
Codegen::default()
.module_names(["html.entities", "html.parser"])?
.generate()?;
Ok(())
}
Implementations§
Source§impl Codegen
impl Codegen
Sourcepub fn module(
self,
module: &Bound<'_, PyModule>,
) -> Result<Codegen, PyBindgenError>
pub fn module( self, module: &Bound<'_, PyModule>, ) -> Result<Codegen, PyBindgenError>
Add a Python module to the list of modules for which to generate bindings.
Sourcepub fn module_name(self, module_name: &str) -> Result<Codegen, PyBindgenError>
pub fn module_name(self, module_name: &str) -> Result<Codegen, PyBindgenError>
Add a Python module by its name to the list of modules for which to generate bindings.
Sourcepub fn module_from_str(
self,
source_code: &str,
module_name: &str,
) -> Result<Codegen, PyBindgenError>
pub fn module_from_str( self, source_code: &str, module_name: &str, ) -> Result<Codegen, PyBindgenError>
Add a Python module from its source code and name to the list of modules for which to generate bindings.
§Note
When including a module in this way, the Python source code must be available also during runtime for the underlying Python interpreter.
For convenience, you can call module_name::pyo3_embed_python_source_code()
that is automatically
generated in the Rust bindings. This function must be called before attempting to use any functions
of classes from the module.
Sourcepub fn modules<'py>(
self,
modules: impl AsRef<[Bound<'py, PyModule>]>,
) -> Result<Codegen, PyBindgenError>
pub fn modules<'py>( self, modules: impl AsRef<[Bound<'py, PyModule>]>, ) -> Result<Codegen, PyBindgenError>
Add multiple Python modules to the list of modules for which to generate bindings.
Sourcepub fn module_names<'a>(
self,
module_names: impl AsRef<[&'a str]>,
) -> Result<Codegen, PyBindgenError>
pub fn module_names<'a>( self, module_names: impl AsRef<[&'a str]>, ) -> Result<Codegen, PyBindgenError>
Add multiple Python modules by their names to the list of modules for which to generate bindings.
Sourcepub fn generate(self) -> Result<TokenStream, PyBindgenError>
pub fn generate(self) -> Result<TokenStream, PyBindgenError>
Generate the Rust FFI bindings for all modules added to the engine.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Codegen
impl !RefUnwindSafe for Codegen
impl Send for Codegen
impl Sync for Codegen
impl Unpin for Codegen
impl UnwindSafe for Codegen
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more