diff --git a/README.md b/README.md new file mode 100644 index 0000000..00869cd --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Python + Rust Playground + +Playing with Pyo3 and Maturin to build Python libraries with Rust \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 62c6a0b..7db7aaa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ use pyo3::prelude::*; +mod types; + /// Formats the sum of two numbers as string. #[pyfunction] fn sum_as_string(a: usize, b: usize) -> PyResult { @@ -17,9 +19,15 @@ 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 /// import the module. #[pymodule] +#[pyo3(name = "py_rust_playground")] // python package name fn py_rust_playground(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + register_child_module(_py, m)?; Ok(()) } \ No newline at end of file diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..bf5ccd4 --- /dev/null +++ b/src/types.rs @@ -0,0 +1,25 @@ +use pyo3::prelude::*; + +#[pyclass] +pub struct Integer { + inner: i32, +} + +// 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. +} \ No newline at end of file