pyo3_stub_gen_derive

Module gen_stub

source
Expand description

Code generation for embedding metadata for generating Python stub file.

These metadata are embedded as inventory::submit! block like:

inventory::submit!{
    PyClassInfo {
        pyclass_name: "Placeholder",
        module: Some("my_module"),
        struct_id: std::any::TypeId::of::<PyPlaceholder>,
        members: &[
            MemberInfo {
                name: "name",
                r#type: <String as ::pyo3_stub_gen::PyStubType>::type_output,
            },
            MemberInfo {
                name: "ndim",
                r#type: <usize as ::pyo3_stub_gen::PyStubType>::type_output,
            },
            MemberInfo {
                name: "description",
                r#type: <Option<String> as ::pyo3_stub_gen::PyStubType>::type_output,
            },
        ],
        doc: "",
    }
}

and this submodule responsible for generating such codes from Rust code like

#[pyclass(mapping, module = "my_module", name = "Placeholder")]
#[derive(Debug, Clone)]
pub struct PyPlaceholder {
    #[pyo3(get)]
    pub name: String,
    #[pyo3(get)]
    pub ndim: usize,
    #[pyo3(get)]
    pub description: Option<String>,
    pub custom_latex: Option<String>,
}

ยงMechanism

Code generation will take three steps:

  1. Parse input proc_macro2::TokenStream into corresponding syntax tree component in syn,
  2. Convert syntax tree components into *Info struct using TryInto.
  3. Generate token streams using implementation of quote::ToTokens trait for *Info structs.

Modulesยง

Functionsยง