-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add Python binding for Buffer struct.
- Loading branch information
Showing
6 changed files
with
68 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
TEST ?= test_bindings.py | ||
TEST_DIR := tests | ||
|
||
.PHONY: build test | ||
|
||
build: | ||
@cargo build | ||
@maturin develop | ||
|
||
test: build | ||
@python3 $(TEST_DIR)/$(TEST) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use pyo3::prelude::*; | ||
use thriller_core::Buffer; | ||
|
||
#[pyclass] | ||
pub struct PyBuffer(pub Buffer); | ||
|
||
#[pymethods] | ||
impl PyBuffer { | ||
#[new] | ||
fn new(name: String) -> Self { | ||
Self(Buffer::new(name.as_str())) | ||
} | ||
|
||
fn __str__(&self) -> PyResult<String> { | ||
Ok(format!("name: {}", self.0.get_name())) | ||
} | ||
|
||
fn __repr__(&self) -> PyResult<String> { | ||
Ok(format!( | ||
"id: {}, name: {}", | ||
self.0.get_id(), | ||
self.0.get_name() | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
use pyo3::prelude::*; | ||
|
||
/// Formats the sum of two numbers as string. | ||
use buffer::PyBuffer; | ||
use thriller_core::initialize; | ||
|
||
mod buffer; | ||
|
||
#[pyfunction] | ||
fn sum_as_string(a: usize, b: usize) -> PyResult<String> { | ||
Ok((a + b).to_string()) | ||
fn initialize_thriller_flow() -> PyResult<()> { | ||
initialize(); | ||
Ok(()) | ||
} | ||
|
||
// #[pyfunction] | ||
// fn create_buffer(name: String) -> PyResult<PyBuffer> { | ||
// let buffer = PyBuffer(Buffer::new(name.as_str())); | ||
// Ok(buffer) | ||
// } | ||
|
||
/// 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 thriller_flow(m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; | ||
m.add_function(wrap_pyfunction!(initialize_thriller_flow, m)?)?; | ||
// m.add_function(wrap_pyfunction!(create_buffer, m)?)?; | ||
m.add_class::<PyBuffer>()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import thriller_flow | ||
from thriller_flow import PyBuffer | ||
|
||
|
||
if __name__ == '__main__': | ||
a = thriller_flow.sum_as_string(5, 10) | ||
print(a) | ||
thriller_flow.initialize_thriller_flow() | ||
g_a = PyBuffer("g_a") | ||
g_b = PyBuffer("g_b") | ||
g_c = PyBuffer("g_c") | ||
|
||
print(g_a, g_b, g_c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters