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:
- Parse input proc_macro2::TokenStream into corresponding syntax tree component in syn,
- e.g. ItemStruct for
#[pyclass]
, ItemImpl for#[pymethods]
, and so on.
- e.g. ItemStruct for
- Convert syntax tree components into
*Info
struct using TryInto.- e.g. PyClassInfo is converted from ItemStruct, PyMethodsInfo is converted from ItemImpl, and so on.
- Generate token streams using implementation of quote::ToTokens trait for
*Info
structs.- quote::quote! macro uses this trait.
Modulesยง
- arg ๐
- attr ๐
- member ๐
- method ๐
- new ๐
- pyclass ๐
- pyclass_
enum ๐ - pyfunction ๐
- pymethods ๐
- signature ๐
- stub_
type ๐ - util ๐