How to Pickle / deepcopy Python class created with maturin?

162 views Asked by At

I've been working on integrating rust code into python using PyO3 and Maturin. I've successfully written my Rust class and gotten most of the functionality to work, however I haven't been able to successfully deepcopy the object. I've tried workarounds using wrapper classes and custom defined deepcopy and getattr/setattr to not much avail, since it seems that the rust functions aren't pickleable. The Rust class is included in a standard #[pymodule], and tagged as a standard #[pyclass]. Has anyone encountered this issue before, and is there any way around it? Or should I just avoid deepcopying in my python code.

1

There are 1 answers

0
Chayim Friedman On

You need to define a __deepcopy__() method, like:

fn __deepcopy__(&self, _memo: &PyDict) -> Self {
    self.clone()
}

As explained in the copy module documentation.

Pickling is more involved, but see PyO3 issue #100.