[PATCH 10 of 10] rust-cpython: add safe way to map PyLeakedRef<&T> to PyLeakedRef<U>

Yuya Nishihara yuya at tcha.org
Thu Oct 10 10:34:17 EDT 2019


On Thu, 10 Oct 2019 13:53:54 +0200, Raphaël Gomès wrote:
> Is it not possible to just take a `fn` type? It forces a non-capturing fn.

Interesting idea, but still fn() would allow mutating a static variable.
IIRC, mutation of static variable can be safely written by using lazy_static
and Mutex.

> On 10/8/19 7:16 PM, Yuya Nishihara wrote:
> > +    pub fn map<U>(
> > +        mut self,
> > +        py: Python,
> > +        f: impl FnOnce(T) -> U,
> > +    ) -> PyLeakedRef<U> {
> > +        PyLeakedRef {
> > +            inner: self.inner.clone_ref(py),
> > +            data: Some(f(self.data.take().unwrap())),
> > +            py_shared_state: self.py_shared_state,
> > +        }
> > +    }

I'm feeling that it's good to leave leaked_ref.map() unsafe. The new API
should be less error-prone, and its safety requirement, "don't leak the
static reference", can be easily guaranteed.


More information about the Mercurial-devel mailing list