I'm working on a videogame in which I'll need to set Rust objects (like, add a button with texture:"", coords:"", text:"", action:"") from Python files.
I'm using the pyo3 crate to link Python and Rust. I succeed calling Python scripts from my Rust…
I'm quite new with Rust, and my first 'serious' project has involved writing a Python wrapper for a small Rust library using PyO3. This has mostly been quite painless, but I'm struggling to work out how to expose lazy iterators over Rust Vecs to…
I am writing a Rust project with several libraries. Some of the libraries export types that are consumed by other libraries in the workspace. In addition to the Rust crates, I would also like to expose some of the libraries to Python, using the pyo3…
Returning Vector of document objects from rust to python fails.
I have a struct and method implementation in rust as follows.
use mongodb::{
bson::{Bson, Document},
error::Error,
sync::Client,
};
use pyo3::prelude::*;
#[pyclass]
pub…
I'm new to Rust and PyO3 (coming from Python) so this might be obvious to more experienced people.
I declared a pyclass struct in PyO3.
#[pyclass]
struct Block {
start: i32,
stop: i32,
}
Then I use Block in a rust function that takes a…
I am using cargo, maturin and pytest to build a mixed Python/Rust project. During development, I frequently cycle through the commands:
$ cargo test -p mypkg --release
$ maturin develop --release
$ python -m pytest --failed-first my_pkg
It seemed…
I have a Python library written in Rust with PyO3, and it involves some expensive calculations (up to 10 minutes for a single function call). How can I abort the execution when calling from Python ?
Ctrl+C seems to only be handled after the end of…
I have a struct + implementation in Rust that I return to Python. This object can also be passed back to Rust for further work. (In my actual code, I'm using a HashMap, but even just using a struct directly seems to cause the same…
I want to write a Python extension using Rust with Ctypes or Pyo3 to get better performance than native Python. But how to exchange data such as Polars DataFrame or ndarray type between Rust and Python?
I'm looking for Info on how to structure a Python package that wraps an extension module written in Rust, where both languages are mixed. I'm using pyO3 for FFI but can't seem to find an example on how to do this.
To be specific: my rust library…
I am looking to write a Rust backend for my library, and I need to implement the equivalent of the following function in pyo3:
def f(x):
return x
This should return the same object as the input, and the function getting the return value should…
Here's my main.rs file:
use pyo3::prelude::*;
use pyo3::types::{PyDateTime, PyDate};
fn main() -> () {
Python::with_gil(|py| {
let datetime = py.import("datetime").unwrap();
let my_datetime = datetime.call_method1("datetime",…
I'm using the latest version of pyo3 (main branch) and it's not clear how I can store an instance of a class, say Store in the below example, on another class. For example, the following code composes two classes, Store and MyClass and will work to…
I want to use a Rust async method in Python.
I'm trying to use PyO3 or rust-cpython.
For example, for sync Rust functions, I can use,
#[pyfunction]
fn myfunc(a: String) -> PyResult {
let mut contents = String::new();
contents =…