Skip to content

Commit

Permalink
Trying with linkme
Browse files Browse the repository at this point in the history
  • Loading branch information
termoshtt committed Jan 4, 2024
1 parent fcd875c commit 9ce876b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion pyo3-stub-gen-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ crate-type = ["cdylib", "rlib"]
inventory.workspace = true
pyo3-stub-gen = { workspace = true, optional = true }
pyo3.workspace = true
linkme = { workspace = true, optional = true }

[features]
stub_gen = ["pyo3-stub-gen"]
default = ["stub_gen"]
stub_gen = ["pyo3-stub-gen", "linkme"]

[[bin]]
name = "stub_gen"
Expand Down
6 changes: 4 additions & 2 deletions pyo3-stub-gen-testing/src/bin/stub_gen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use pyo3_stub_gen::*;

fn main() {
let modules = generate::gather().unwrap();
dbg!(modules);
dbg!(type_info::PYFUNCTIONS.len());
for info in type_info::PYFUNCTIONS {
dbg!(info);
}
}
16 changes: 12 additions & 4 deletions pyo3-stub-gen-testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use linkme::distributed_slice;
use pyo3::prelude::*;

#[cfg(feature = "stub_gen")]
use pyo3_stub_gen::derive::*;
use pyo3_stub_gen::type_info::*;

/// Returns the sum of two numbers as a string.
///
Expand All @@ -10,12 +9,21 @@ use pyo3_stub_gen::derive::*;
/// ```rust
/// assert_eq!(2 + 2, 4);
/// ```
#[cfg_attr(feature = "stub_gen", gen_stub_pyfunction)]
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}

#[distributed_slice(PYFUNCTIONS)]
static PYFUNCTION_SUM_AS_STRING: PyFunctionInfo = PyFunctionInfo {
name: "sum_as_string",
r#return: no_return_type_output,
args: &[/* dummy */],
doc: "",
signature: None,
module: None,
};

#[pymodule]
fn pyo3_stub_gen_testing(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Expand Down
4 changes: 4 additions & 0 deletions pyo3-stub-gen/src/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//! This process is done at runtime in [gen_stub](../../gen_stub) executable.
//!
use linkme::distributed_slice;
use pyo3::inspect::types::TypeInfo;
use std::any::TypeId;

Expand Down Expand Up @@ -128,6 +129,9 @@ pub struct PyFunctionInfo {
pub module: Option<&'static str>,
}

#[distributed_slice]
pub static PYFUNCTIONS: [PyFunctionInfo];

inventory::collect!(PyFunctionInfo);

#[derive(Debug)]
Expand Down

0 comments on commit 9ce876b

Please sign in to comment.