Skip to content

Commit

Permalink
Add the equivalent code from the previous post
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrankel committed Oct 18, 2023
1 parent 1da2e41 commit 7ad74a5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
59 changes: 43 additions & 16 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ name = "rust_over_pyo3"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.19.0"
pyo3 = { version = "0.20.0" , features = ["num-complex"]}
num-complex = "0.4.4"
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use num_complex::Complex;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;

/// Formats the sum of two numbers as string.
Expand All @@ -10,5 +12,16 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
#[pymodule]
fn rust_over_pyo3(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
m.add_function(wrap_pyfunction!(compute, m)?)?;
Ok(())
}
}

#[pyfunction]
fn compute(command: &str, a: Complex<f64>, b: Complex<f64>) -> PyResult<Complex<f64>> {
match command {
"add" => Ok(a + b),
"sub" => Ok(a - b),
"mul" => Ok(a * b),
_ => Err(PyValueError::new_err("Unknown command")),
}
}

0 comments on commit 7ad74a5

Please sign in to comment.