Skip to content

Commit

Permalink
Add PyO3 bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbayley committed May 2, 2024
1 parent 15a6a73 commit ea317a6
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 5 deletions.
288 changes: 288 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ version = "0.1.0"
edition = "2021"

[dependencies]
pyo3 = { version = "0.21.2", features = ["extension-module"] }

[lib]
name = "gazellekit"
crate-type = ["cdylib"]

5 changes: 4 additions & 1 deletion bindings/python/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
print('Welcome to Gazelle! 🦌')
from gazellekit import sum_as_string

print("Welcome to Gazelle! 🦌")
print(sum_as_string(10, 30))
9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ packages = [{ include = "bindings/python" }]
python = ">=3.11"

[tool.poetry.group.dev.dependencies]
pytest = "^8.0.1"
ruff = "^0.2.2"
maturin = "^1.5.1"
pytest = "^8.2.0"
ruff = "^0.4.2"

[tool.ruff]
indent-width = 2

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
requires = ["maturin>=1,<2"]
build-backend = "maturin"
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use pyo3::prelude::*;

/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}

/// A Python module implemented in Rust. The name of this function must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
fn gazellekit(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}

0 comments on commit ea317a6

Please sign in to comment.