Skip to content

feature: support pyo3 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[workspace]
members = ["node-binding", "algo"]
members = ["algo", "node-binding", "python-binding"]
resolver = "2"

[workspace.dependencies]
algo = { path = "algo" }
[workspace.dependencies]
algo = { path = "algo" }

[profile.release]
lto = true
6 changes: 3 additions & 3 deletions algo/src/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Algo {
Self { r#type }
}

pub fn hash(&self, v: String) -> String {
pub fn hash(&self, v: &str) -> String {
match self.r#type {
AlgoType::Blake3 => {
let hash = blake3::hash(v.as_bytes());
Expand Down Expand Up @@ -47,11 +47,11 @@ mod tests {
fn hash_should_work() {
let algo = Algo::new(AlgoType::Blake3);
assert_eq!(
algo.hash("hello".to_string()),
algo.hash("hello"),
"ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f"
);

let algo = Algo::new(AlgoType::Default);
assert_eq!(algo.hash("hello".to_string()), "16156531084128653017");
assert_eq!(algo.hash("hello"), "16156531084128653017");
}
}
1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ unlicensed = "allow"
allow = [
"MIT",
"Apache-2.0",
"Apache-2.0 WITH LLVM-exception",
"Unicode-DFS-2016",
"MPL-2.0",
"BSD-2-Clause",
Expand Down
2 changes: 1 addition & 1 deletion node-binding/src/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl JsAlgo {

#[napi]
pub fn hash(&self, v: String) -> String {
self.inner.hash(v)
self.inner.hash(&v)
}

#[napi]
Expand Down
13 changes: 13 additions & 0 deletions python-binding/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# python generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info
# Rust
target/

# venv
.venv
*.so
1 change: 1 addition & 0 deletions python-binding/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.3
13 changes: 13 additions & 0 deletions python-binding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "python-binding"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "python_binding"
crate-type = ["cdylib"]

[dependencies]
algo = { workspace = true }
pyo3 = { version = "0.22.2", features = ["extension-module"] }
3 changes: 3 additions & 0 deletions python-binding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# python-binding

Describe your project here.
21 changes: 21 additions & 0 deletions python-binding/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[project]
name = "algo"
version = "0.1.0"
description = "Add your description here"
authors = [{ name = "Tyr Chen", email = "[email protected]" }]
dependencies = []
readme = "README.md"
requires-python = ">= 3.8"

[build-system]
requires = ["maturin>=1.2,<2.0"]
build-backend = "maturin"

[tool.rye]
managed = true
dev-dependencies = ["pip>=24.2", "ipython>=8.26.0"]

[tool.maturin]
python-source = "python"
module-name = "algo._lowlevel"
features = ["pyo3/extension-module"]
4 changes: 4 additions & 0 deletions python-binding/python/algo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from algo._lowlevel import hello
from algo._lowlevel import Algo

__all__ = ["hello", "Algo"]
43 changes: 43 additions & 0 deletions python-binding/requirements-dev.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# generated by rye
# use `rye lock` or `rye sync` to update this lockfile
#
# last locked with the following flags:
# pre: false
# features: []
# all-features: false
# with-sources: false

-e file:.
asttokens==2.4.1
# via stack-data
decorator==5.1.1
# via ipython
executing==2.0.1
# via stack-data
ipython==8.26.0
jedi==0.19.1
# via ipython
matplotlib-inline==0.1.7
# via ipython
parso==0.8.4
# via jedi
pexpect==4.9.0
# via ipython
pip==24.2
prompt-toolkit==3.0.47
# via ipython
ptyprocess==0.7.0
# via pexpect
pure-eval==0.2.3
# via stack-data
pygments==2.18.0
# via ipython
six==1.16.0
# via asttokens
stack-data==0.6.3
# via ipython
traitlets==5.14.3
# via ipython
# via matplotlib-inline
wcwidth==0.2.13
# via prompt-toolkit
10 changes: 10 additions & 0 deletions python-binding/requirements.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# generated by rye
# use `rye lock` or `rye sync` to update this lockfile
#
# last locked with the following flags:
# pre: false
# features: []
# all-features: false
# with-sources: false

-e file:.
44 changes: 44 additions & 0 deletions python-binding/src/hasher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::fmt;

use algo::{Algo, AlgoType};
use pyo3::prelude::*;

#[pyclass(name = "Algo")]
pub struct PyAlgo {
inner: Algo,
}

#[pymethods]
impl PyAlgo {
#[new]
#[pyo3(signature = (name=""))]
pub fn new(name: &str) -> Self {
let algo = match name {
"blake3" => Algo::new(AlgoType::Blake3),
_ => Algo::new(AlgoType::Default),
};
Self { inner: algo }
}

pub fn hash(&self, v: &str) -> String {
self.inner.hash(v)
}

pub fn get_name(&self) -> &str {
self.inner.get_name()
}

pub fn __repr__(&self) -> String {
self.to_string()
}

pub fn __str__(&self) -> String {
self.to_string()
}
}

impl fmt::Display for PyAlgo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<Algo: {}>", self.inner.get_name())
}
}
18 changes: 18 additions & 0 deletions python-binding/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mod hasher;

use hasher::PyAlgo;
use pyo3::prelude::*;

/// Prints a message.
#[pyfunction]
fn hello() -> PyResult<String> {
Ok("Hello from python-binding!".into())
}

/// A Python module implemented in Rust.
#[pymodule]
fn _lowlevel(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hello, m)?)?;
m.add_class::<PyAlgo>()?;
Ok(())
}