simple reference example #3473
-
Hi, I've seen similar questions here and perhaps here but I'm not able to grok the answer. I created a complete example here: I'm not following how to get a mutable reference so the user can the field of a struct in python. In short, given: struct Data {
x: [f64; 1000],
}
#[pyclass]
struct PyData {
data: Data
} how can I expose the ability to mutate Data from python: #[pymethods]
impl PyData {
fn set_data(&mut self, i: usize, x: f64, py: Python<'_>) {
// ???
self.data.as_ref(py).x[i] = x;
}
} As you'll see in the github repo, I have another struct that returns the PyData. I'd like it to look like this in python: outer.data[i] = 33.33 thanks for any pointers. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I found a way to make this work with |
Beta Was this translation helpful? Give feedback.
-
I added this branch which actually contains the code I want to wrap with pyo3. (I should have done this to start as it's quite simple). I want the python class ( In short, if I have: #[pyclass]
struct Variant {
inner: vcf::Record, // vcf::Record is the owned rust struct
} how can I get another python reference to Variant? With the Arc<>, I can do: clone = Variant { inner: Arc::clone(&self.inner) } |
Beta Was this translation helpful? Give feedback.
It works the other way around, you take
Py<Self>
and get a&mut Self
from that in the same way that our macros usually do automatically for you: