pyo2 0.1.2

A lightweight method to call Rust code from Python
Documentation
  • Coverage
  • 90.91%
    10 out of 11 items documented1 out of 9 items with examples
  • Size
  • Source code size: 4.39 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.37 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • david-why

PyO2: A lightweight method to call Rust code from Python

Not affiliated at all with PyO3. Is that what you were looking for?

Usage

File: Cargo.toml

[package]
name = "mylib"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
pyo2 = "0.1.0"

File: src/lib.rs

use pyo2::{PyStr, PyVec};

#[no_mangle]
pub extern "C" fn test(name: &PyStr, numbers: &mut PyVec<i64>) {
    println!("Hello, {}!", unsafe { name.as_str_unchecked() });
    println!("Sum of numbers: {}", numbers.iter().cloned().sum::<i64>());
    numbers[0] = 6;
}

File: test.py

from pyo2 import RustDLL

dll = RustDLL('./libmylib.so')

s = 'World'
lst = [1, 2, 3, 4, 5]
dll.test(s, lst)
print(lst)

Output:

Hello, World!
Sum of numbers: 15
[6, 2, 3, 4, 5]