forked from bazelbuild/rules_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
|
||
#[pyfunction] | ||
fn sum_as_string(a: usize, b: usize) -> PyResult<String> { | ||
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub static secret: u32 = 42; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!") |