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 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…
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 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 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…
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 =…
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…
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 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'm trying to implement a vector class in rust for my math library.
#[pyclass]
struct Vec2d {
#[pyo3(get, set)]
x: f64,
#[pyo3(get, set)]
y: f64
}
But I can't figure out how I can overload the standard operators (+, -, *, /)
I…
Based on this I can create a homogeneous Python dictionary.
How can I create a dictionary with mixed-type values, e.g. make this work:
let dict = [ ("num", 8), ("str", "asd") ].into_py_dict(py);
?
How do I correctly raise an Exception?
I've tried the following:
#[pymethods]
impl Foo {
#[new]
fn __new__(arg1: u8, ...) -> Self {
if arg1 > LIMIT {
let gil = Python::acquire_gil();
let py = gil.python();
…
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…