diff --git a/examples/ffi/python_calling_rust/BUILD.bazel b/examples/ffi/python_calling_rust/BUILD.bazel index 91db895ec3..78a8e70f11 100644 --- a/examples/ffi/python_calling_rust/BUILD.bazel +++ b/examples/ffi/python_calling_rust/BUILD.bazel @@ -13,17 +13,41 @@ # limitations under the License. load("@rules_python//python:defs.bzl", "py_test") +load("@rules_rust//rust:defs.bzl", "rust_library") load("@rules_rust//python:py_rust_library.bzl", "py_rust_library") +rust_library( + name = "helper", + srcs = ["helper.rs"], +) + py_rust_library( name = "hello_py", srcs = ["hello_py.rs"], edition = "2018", - deps = ["//ffi/python_calling_rust/raze:pyo3"], + deps = [ + ":helper", + "//ffi/python_calling_rust/raze:pyo3", + ], +) + +py_rust_library( + name = "hello_py_2", + srcs = ["hello_py_2.rs"], + edition = "2018", + deps = [ + ":helper", + "//ffi/python_calling_rust/raze:pyo3", + ], ) + + py_test( name = "test", srcs = ["test.py"], - deps = [":hello_py"], + deps = [ + ":hello_py", + ":hello_py_2", + ], ) diff --git a/examples/ffi/python_calling_rust/hello_py.rs b/examples/ffi/python_calling_rust/hello_py.rs index 3f3c92ced8..072a594aef 100644 --- a/examples/ffi/python_calling_rust/hello_py.rs +++ b/examples/ffi/python_calling_rust/hello_py.rs @@ -6,9 +6,15 @@ fn sum_as_string(a: usize, b: usize) -> PyResult { Ok((a + b).to_string()) } +#[pyfunction] +fn secret() -> i32 { + &helper::secret as *const u32 as usize as i32 +} + #[pymodule] fn hello_py(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + m.add_function(wrap_pyfunction!(secret, m)?)?; Ok(()) } diff --git a/examples/ffi/python_calling_rust/hello_py_2.rs b/examples/ffi/python_calling_rust/hello_py_2.rs new file mode 100644 index 0000000000..fb096bf8d8 --- /dev/null +++ b/examples/ffi/python_calling_rust/hello_py_2.rs @@ -0,0 +1,20 @@ +use pyo3::prelude::*; +use pyo3::wrap_pyfunction; + +#[pyfunction] +fn sum_as_string(a: usize, b: usize) -> PyResult { + Ok((a + b).to_string()) +} + +#[pyfunction] +fn secret() -> i32 { + &helper::secret as *const u32 as usize as i32 +} + +#[pymodule] +fn hello_py_2(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + m.add_function(wrap_pyfunction!(secret, m)?)?; + + Ok(()) +} diff --git a/examples/ffi/python_calling_rust/helper.rs b/examples/ffi/python_calling_rust/helper.rs new file mode 100644 index 0000000000..01cf6c2c96 --- /dev/null +++ b/examples/ffi/python_calling_rust/helper.rs @@ -0,0 +1 @@ +pub static secret: u32 = 42; diff --git a/examples/ffi/python_calling_rust/test.py b/examples/ffi/python_calling_rust/test.py index 7a0832665b..7e3267ab93 100644 --- a/examples/ffi/python_calling_rust/test.py +++ b/examples/ffi/python_calling_rust/test.py @@ -1,4 +1,14 @@ -from ffi.python_calling_rust.hello_py import sum_as_string +from ffi.python_calling_rust import hello_py +from ffi.python_calling_rust import hello_py_2 + + +assert hello_py.sum_as_string(1, 1) == "2" +assert hello_py_2.sum_as_string(1, 1) == "2" + + +s1 = hello_py.secret() +s2 = hello_py_2.secret() + +assert s1 == s2, (s1, s2) -assert sum_as_string(1, 1) == "2" print("Ok!")