Pyclasses and structs with mutable references #2624
Unanswered
entropidelic
asked this question in
Questions
Replies: 1 comment 2 replies
-
Hey 👋 When you do... #[pymethods]
impl Outer {
#[new]
fn new() -> Outer {
Outer {
inner: &mut Inner::new(),
}
}
} ...you're creating a new NB: looking up things in |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello guys, I am working on a project that requires both accessing a Rust type from Python and allowing that Rust type to invoke Python code. I've put together the smallest possible example in this repo to illustrate.
Outer
, a struct that wraps a mutable reference toInner
,We understand that to create variables accessible by the Python interpreter in
py.run()
scope, this is done via alocals
PyDict. To give it access to a Rust struct, the definition of this struct should be annotated with a#[pyclass]
attribute. Finally, an instance of it should be wrapped in a PyCell to insert it to the locals dictionary.At this point our confusion appears:
As the
Outer
struct is wrapping a mutable reference toInner
, a lifetime parameter should be specified. But when the lifetime is set along with the#[pyclass]
attribute to the struct, we get an error complaining thatpyclass
cannot have generic parameters.We know by design that
Outer
will not be mutated while it is accesible by the running python code. How can we convince pyo3 of this and pass in a pyclass-wrapped struct that references a rust-allocated data structure?Beta Was this translation helpful? Give feedback.
All reactions