Struct Codegen

Source
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

Source

pub fn new(cfg: Config) -> Codegen

Create a new Codegen engine with the given configuration.

Source

pub fn module( self, module: &Bound<'_, PyModule>, ) -> Result<Codegen, PyBindgenError>

Add a Python module to the list of modules for which to generate bindings.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn generate(self) -> Result<TokenStream, PyBindgenError>

Generate the Rust FFI bindings for all modules added to the engine.

Source

pub fn build(self, output_path: impl AsRef<Path>) -> Result<(), PyBindgenError>

Generate the Rust FFI bindings for all modules added to the engine and write them to the given file. This is a convenience method that combines generate and std::fs::write.

Trait Implementations§

Source§

impl Clone for Codegen

Source§

fn clone(&self) -> Codegen

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Codegen

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Codegen

Source§

fn default() -> Codegen

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Ungil for T
where T: Send,