I'm trying to build a Python package from Rust using PyO3 (version: 0.13.2). Right now I'm stuck trying to get the conversion working for enums. I have a simple enum like so:
#[derive(FromPyObject)]
#[derive(Copy,Clone,PartialEq,Eq)]
enum Direction…
I'm trying to pass a list of lists from Python to Rust using Py03. The function I'm trying to pass it to has this signature:
pub fn k_nearest_neighbours(k: usize, x: &[[f32; 2]], y: &[[f32; 3]]) -> Vec
I'm making a Python module, written in Rust, using pyo3, that will:
Run its own thread
Read an input pin on a Raspberry Pi, counting how many times the state changes
Let Python query the counter
So far, my code looks like this:
use…
I am trying to iterate by rows over a Numpy Array. The array is accessed thru PyO3 and I think the library acess to the underlying C object just fine but I can't seem to find the reference for a more complex SingleIteratorBuilder that helps me…
I've created a Rust library for python using pyo3. This contains a pyclass struct that implements several of PyNumberProtocol methods like __add__, __sub__, etc... to allow python operators like + and - to work on the class. I'm using PyAny as the…
I have a library in Rust and I want to be able to use it from Python. So I can do the bindings using PyO3.
Let's suppose that I have a following library in Rust:
pub trait Animal {
fn make_sound();
}
pub struct Dog {}
impl Animal for Dog {
…
Using PyO3, I am able to pass &str and String types from Rust to Python:
#[pyfunction]
fn test_str(py: Python) -> &str {
"this is a &str"
}
#[pyfunction]
fn test_string(py: Python) -> String {
"this is a String".to_string()
}
And Python is…
I'm wondering if anyone has experience with cross-compiling to windows with pyo3 and maturin.
The pyo3 docs say:
Cross compiling PyO3 modules is relatively straightforward and
requires a few pieces of software:
A toolchain for your target. The…
I am trying to pass a Python object into rust and perform operations using the fields of the Python object.
Python:
class myclass(object):
def __init__(self):
self.a = 3
b = myclass()
print(b.a)
// 3
Rust:
#[pyfn(m, "rust_obj")]
fn…
I have 2 pyclasses Block and BlockGroup.
#[pyclass]
struct Block {
start: i32,
stop: i32,
}
#[pyclass]
struct BlockGroup {
blocks: Vec
}
I'm new to PyO3 and I have read the documentation about garbage collection but I don't…
I have read this answer(How to call Rust async method from Python?) and what I want to do is the opposite of this.
I want to call a Python async function and await it in tokio's runtime. Is it possible?
I have given it some tries but I am facing an…
When we write vanilla rust and we have to pass in an async function as an argument to another function we do the following:
pub f(
test: &dyn Fn(&'a mut String, String, String, TcpStream) -> F,
) where
F: Future
The closer option I have found is pyo3, but it isn't clear to me if it adds any extra overhead when compared to the traditional C-extensions.
From here it seems like such C-extension behavior is possible through borrowed objects (I still have to…
I am using the numpy crate in Rust to work with 2D arrays that come from python. PyArray (https://docs.rs/numpy/0.11.0/numpy/array/struct.PyArray.html) implements a from_vec2() function, which converts a Vec> into a PyArray (2D…