Compare commits
No commits in common. "debf43dff9f7b1302a73e7f4d7217ff618042269" and "bfafbc6e9f33fcb43adc0a6e34187acdc79170d3" have entirely different histories.
debf43dff9
...
bfafbc6e9f
@ -1,3 +0,0 @@
|
|||||||
# Python + Rust Playground
|
|
||||||
|
|
||||||
Playing with Pyo3 and Maturin to build Python libraries with Rust
|
|
@ -1,7 +1,5 @@
|
|||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
|
||||||
mod types;
|
|
||||||
|
|
||||||
/// Formats the sum of two numbers as string.
|
/// Formats the sum of two numbers as string.
|
||||||
#[pyfunction]
|
#[pyfunction]
|
||||||
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
|
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
|
||||||
@ -19,15 +17,9 @@ fn register_child_module(py: Python<'_>, parent_module: &PyModule) -> PyResult<(
|
|||||||
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
|
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
|
||||||
/// import the module.
|
/// import the module.
|
||||||
#[pymodule]
|
#[pymodule]
|
||||||
#[pyo3(name = "py_rust_playground")] // python package name
|
|
||||||
fn py_rust_playground(_py: Python, m: &PyModule) -> PyResult<()> {
|
fn py_rust_playground(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||||
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
|
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
|
||||||
|
|
||||||
m.add_class::<types::Integer>()?;
|
|
||||||
m.add_class::<types::Number>()?;
|
|
||||||
m.add_class::<types::HttpResponse>()?;
|
|
||||||
m.add_class::<types::MyEnum>()?;
|
|
||||||
|
|
||||||
register_child_module(_py, m)?;
|
register_child_module(_py, m)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
34
src/types.rs
34
src/types.rs
@ -1,34 +0,0 @@
|
|||||||
use pyo3::prelude::*;
|
|
||||||
|
|
||||||
#[pyclass]
|
|
||||||
pub struct Integer {
|
|
||||||
#[pyo3(get, set)]
|
|
||||||
inner: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pymethods]
|
|
||||||
impl Integer {
|
|
||||||
#[new]
|
|
||||||
fn new(value: i32) -> Integer {
|
|
||||||
Integer { inner: value }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A "tuple" struct
|
|
||||||
#[pyclass]
|
|
||||||
pub struct Number(i32);
|
|
||||||
|
|
||||||
// PyO3 supports custom discriminants in enums
|
|
||||||
#[pyclass]
|
|
||||||
pub enum HttpResponse {
|
|
||||||
Ok = 200,
|
|
||||||
NotFound = 404,
|
|
||||||
Teapot = 418,
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pyclass]
|
|
||||||
pub enum MyEnum {
|
|
||||||
Variant,
|
|
||||||
OtherVariant = 30, // PyO3 supports custom discriminants.
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user