Skip to content

Commit

Permalink
pyo3 (#12)
Browse files Browse the repository at this point in the history
* feat: rust branch init changes

* feat: rename rustlib to crates
  • Loading branch information
evmckinney9 authored Mar 31, 2024
1 parent caeece5 commit dd8db6e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include Cargo.toml
recursive-include crates *.rs
recursive-include src *
14 changes: 14 additions & 0 deletions crates/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
14 changes: 14 additions & 0 deletions crates/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "crates"
version = "0.1.0"

[lib]
name = "_lib"
crate-type = ["cdylib"]
path = "src/lib.rs"

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
# See also PyO3 docs on writing Cargo.toml files at https://pyo3.rs
13 changes: 13 additions & 0 deletions crates/src/basic_functions/basic_math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use pyo3::prelude::*;

#[pyfunction]
#[pyo3(text_signature = "(a, b, /)")]
pub fn add_in_rust(a: i32, b: i32) -> PyResult<i32> {
Ok(a + b)
}

#[pyfunction]
#[pyo3(text_signature = "(a, b, /)")]
pub fn subtract_in_rust(a: i32, b: i32) -> PyResult<i32> {
Ok(a - b)
}
2 changes: 2 additions & 0 deletions crates/src/basic_functions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//mod.rs
pub mod basic_math;
14 changes: 14 additions & 0 deletions crates/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//src/lib.rs

pub mod basic_functions;
use crate::basic_functions::*;

use pyo3::prelude::*;

#[pymodule]
fn quippers(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(basic_math::add_in_rust, m)?)?;
m.add_function(wrap_pyfunction!(basic_math::subtract_in_rust, m)?)?;

Ok(())
}
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools", "wheel"]
requires = ["setuptools", "wheel", "setuptools_rust"]
build-backend = "setuptools.build_meta"

[project]
Expand Down Expand Up @@ -36,3 +36,12 @@ fix = true

[tool.isort]
profile = "black"


[[tool.setuptools-rust.ext-modules]]
# Private Rust extension module to be nested into the Python package
# https://github.com/PyO3/setuptools-rust?tab=readme-ov-file
target = "{{project_name}}._lib" # The last part of the name (e.g. "_lib") has to match lib.name in Cargo.toml,
# but you can add a prefix to nest it inside of a Python package.
path = "Cargo.toml" # Default value, can be omitted
binding = "PyO3" # Default value, can be omitted

0 comments on commit dd8db6e

Please sign in to comment.