mixed/
lib.rs

1use pyo3::prelude::*;
2use pyo3_stub_gen::{define_stub_info_gatherer, derive::*};
3
4// Specify the module name explicitly
5#[gen_stub_pyclass]
6#[pyclass(module = "mixed.main_mod")]
7#[derive(Debug)]
8struct A {
9    x: usize,
10}
11
12#[gen_stub_pymethods]
13#[pymethods]
14impl A {
15    fn show_x(&self) {
16        println!("x = {}", self.x);
17    }
18}
19
20#[gen_stub_pyfunction(module = "mixed.main_mod")]
21#[pyfunction]
22fn create_a(x: usize) -> A {
23    A { x }
24}
25
26// Do not specify the module name explicitly
27// This will be placed in the main module
28#[gen_stub_pyclass]
29#[pyclass]
30#[derive(Debug)]
31struct B {
32    x: usize,
33}
34
35#[gen_stub_pymethods]
36#[pymethods]
37impl B {
38    fn show_x(&self) {
39        println!("x = {}", self.x);
40    }
41}
42
43#[gen_stub_pyfunction]
44#[pyfunction]
45fn create_b(x: usize) -> B {
46    B { x }
47}
48
49#[pymodule]
50fn main_mod(m: &Bound<PyModule>) -> PyResult<()> {
51    m.add_class::<A>()?;
52    m.add_class::<B>()?;
53    m.add_function(wrap_pyfunction!(create_a, m)?)?;
54    m.add_function(wrap_pyfunction!(create_b, m)?)?;
55    Ok(())
56}
57
58define_stub_info_gatherer!(stub_info);
59
60/// Test of unit test for testing link problem
61#[cfg(test)]
62mod test {
63    #[test]
64    fn test() {
65        assert_eq!(2 + 2, 4);
66    }
67}