Skip to content

simple reference example #3473

Answered by adamreichold
brentp asked this question in Questions
Sep 25, 2023 · 2 comments · 6 replies
Discussion options

You must be logged in to vote

how do I get a Py inside of variant.info which accepts &mut self ?

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:

#[pymethods]
impl Variant {
    #[getter]
    fn info(slf: Py<Self>, py: Python<'_>) -> PyResult<Py<Info>> {
        let mut this: PyRefMut<Self> = slf.try_borrow_mut(py)?;
        let this: &mut Self = &mut *this;
        // Work with `this`...
        let info = Info { variant: slf };
        Py::new(py, info)
    }
}

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
2 replies
@adamreichold
Comment options

@brentp
Comment options

Comment options

You must be logged in to vote
4 replies
@adamreichold
Comment options

@brentp
Comment options

@adamreichold
Comment options

Answer selected by brentp
@brentp
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants