Skip to content

Commit 2bfbb23

Browse files
committed
WIP: Initial rust build support.
This is just ensure that tests can invoke the built rust code to make sure that the test/build tooling works.
1 parent 20ea5c6 commit 2bfbb23

6 files changed

Lines changed: 36 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.egg-info
2+
*.so
23
*.sqlite3
34
.DS_Store
45
/*.lock
@@ -7,6 +8,8 @@
78
/.jj/
89
/.tox/
910
/.venv/
11+
/build/
1012
/dist/
1113
/site/
14+
/target/
1215
__pycache__/

Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
edition = "2021"
3+
name = "htpy"
4+
version = "0.0.0"
5+
6+
[dependencies]
7+
pyo3 = "0.25"
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
name = "_rust_impl"
12+
path = "src/lib.rs"

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ html2htpy = "htpy.html2htpy:main"
6262

6363

6464
[build-system]
65-
requires = ["setuptools>=64", "setuptools-scm>=8"]
65+
requires = ["setuptools>=64", "setuptools-scm>=8", "setuptools-rust>=1.11.1"]
6666
build-backend = "setuptools.build_meta"
6767

6868
[tool.setuptools_scm]
6969

70+
[[tool.setuptools-rust.ext-modules]]
71+
target = "htpy._rust_impl"
7072

7173
[tool.mypy]
7274
strict = true

src/htpy/_rust_impl.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def sum_as_string(a: int, b: int) -> str: ...

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use pyo3::prelude::*;
2+
3+
#[pyfunction]
4+
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
5+
Ok((a + b).to_string())
6+
}
7+
8+
#[pymodule]
9+
fn _rust_impl(m: &Bound<'_, PyModule>) -> PyResult<()> {
10+
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
11+
Ok(())
12+
}

tests/test_rust_build.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from htpy._rust_impl import sum_as_string
2+
3+
4+
def test_sum_as_string() -> None:
5+
assert sum_as_string(21, 21) == "42"

0 commit comments

Comments
 (0)