-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from konstin/capybara
Relax return types and add functions
- Loading branch information
Showing
14 changed files
with
433 additions
and
290 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,60 @@ | ||
# Python Function | ||
|
||
TODO | ||
Pyo3 supports two ways to define a function in python. Both require registering | ||
the function to a [module](./module.md) | ||
|
||
One way is defining the function in the module definition. | ||
|
||
```rust | ||
#![feature(proc_macro)] | ||
|
||
extern crate pyo3; | ||
use pyo3::{py, PyResult, Python, PyModule}; | ||
use pyo3::py::modinit as pymodinit; | ||
|
||
#[pymodinit(rust2py)] | ||
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> { | ||
|
||
// Note that the `#[pyfn()]` annotation automatically converts the arguments from | ||
// Python objects to Rust values; and the Rust return value back into a Python object. | ||
#[pyfn(m, "sum_as_string")] | ||
fn sum_as_string_py(_py: Python, a:i64, b:i64) -> PyResult<String> { | ||
Ok(format!("{}", a + b).to_string()) | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
# fn main() {} | ||
``` | ||
|
||
The other is annotating a function with `#[py::function]` and then adding it | ||
to the module using the `add_function_to_module!` macro, which takes the module | ||
as first parameter, the function name as second and an instance of `Python` | ||
as third. | ||
|
||
```rust | ||
#![feature(proc_macro, concat_idents)] | ||
|
||
#[macro_use] | ||
extern crate pyo3; | ||
use pyo3::{py, PyResult, Python, PyModule}; | ||
|
||
use pyo3::py::function as pyfunction; | ||
use pyo3::py::modinit as pymodinit; | ||
|
||
#[pyfunction] | ||
fn double(x: usize) -> usize { | ||
x * 2 | ||
} | ||
|
||
#[pymodinit(module_with_functions)] | ||
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_function(wrap_function!(double)).unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
# fn main() {} | ||
``` | ||
|
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
Oops, something went wrong.