Skip to content

Commit

Permalink
chore: Add Python binding for Buffer struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
KuangjuX committed May 24, 2024
1 parent bf05ae2 commit a1c8bc7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
11 changes: 11 additions & 0 deletions thriller-bindings/Makefile
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)
25 changes: 25 additions & 0 deletions thriller-bindings/src/buffer.rs
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()
))
}
}
21 changes: 17 additions & 4 deletions thriller-bindings/src/lib.rs
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(())
}
9 changes: 7 additions & 2 deletions thriller-bindings/tests/test_bindings.py
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)
8 changes: 7 additions & 1 deletion thriller-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ impl Buffer {
}
}

pub(crate) fn get_name(&self) -> &String {
/// Get Buffer name.
pub fn get_name(&self) -> &String {
&self.name
}

/// Get Buffer id.
pub fn get_id(&self) -> usize {
self.id
}
}
2 changes: 1 addition & 1 deletion thriller-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! ThrillerFlow is a DataFlow Analyise and Codegen Framework written in Rust.
//! A DataFlow Analyise and Codegen Framework written in Rust.
#![deny(warnings)]
#![deny(missing_docs)]
Expand Down

0 comments on commit a1c8bc7

Please sign in to comment.