Skip to content

Commit

Permalink
add basic rust integration with setuptools-rust
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Aug 31, 2024
1 parent 00fd35d commit c2a4cb6
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ experiments
logprep.log
/charts/logprep/charts
examples/k8s/charts
*.so
target
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2021"

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

[dependencies]
pyo3 = "0.22.0"
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include Cargo.toml
recursive-include rust *
16 changes: 14 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
[build-system]
requires = ["setuptools>=68.0.0", "setuptools-scm>=8.0", "wheel"]
requires = [
"setuptools>=68.0.0",
"setuptools-scm>=8.0",
"wheel",
"setuptools-rust",
]
build-backend = "setuptools.build_meta"

[tool.setuptools]
packages = ["logprep"]


[tool.setuptools_scm]
fallback_version = "unset"

[[tool.setuptools-rust.ext-modules]]
# Private Rust extension module to be nested into the Python package
target = "rust" # 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

[project]
name = "logprep"
description = "Logprep allows to collect, process and forward log messages from various data sources."
Expand Down Expand Up @@ -98,6 +109,7 @@ dev = [
"pytest-cov",
"responses",
"jinja2",
"maturin",
]

doc = [
Expand Down
72 changes: 72 additions & 0 deletions rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/target

# Byte-compiled / optimized / DLL files
__pycache__/
.pytest_cache/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
.venv/
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
include/
man/
venv/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
pip-selfcheck.json

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

.DS_Store

# Sphinx documentation
docs/_build/

# PyCharm
.idea/

# VSCode
.vscode/

# Pyenv
.python-version
22 changes: 22 additions & 0 deletions rust/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use pyo3::prelude::*;

/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}

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

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!("4", "4");
}
}
1 change: 0 additions & 1 deletion tests/unit/framework/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import multiprocessing
from copy import deepcopy
from logging import DEBUG
from multiprocessing import Lock
from unittest import mock

import pytest
Expand Down
Empty file added tests/unit/rust/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions tests/unit/rust/test_rust.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import rust


def test_example():
assert rust.sum_as_string(1, 2) == "3"

0 comments on commit c2a4cb6

Please sign in to comment.