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

Yuya Nishihara yuya at tcha.org
Tue Oct 8 17:16:59 UTC 2019


On Sun, 22 Sep 2019 15:41:47 +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya at tcha.org>
> # Date 1568553550 -32400
> #      Sun Sep 15 22:19:10 2019 +0900
> # Node ID 0d0d926ccd7f34a2125071d6e7e0f86e04026ba6
> # Parent  77d76b966a64f83fb7be893f7fc4090f24d7e83c
> rust-cpython: add safe way to map PyLeakedRef<&T> to PyLeakedRef<U>

> +    /// Converts the inner value by the given function.
> +    ///
> +    /// Typically `T` is a static reference to a container, and `U` is an
> +    /// iterator of that container.
> +    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,
> +        }
> +    }

Actually this can be unsafe since the lambda function 'f' could move the
argument "&'static X" out of the PyLeakedRef scope.

  leaked.map(py, |o| {
     out_of_scope_variable = o;
  });

Maybe I need to find a safe way to apply arbitrary function on static ref
without loosing the static-ness.

I think the patches 01-08 aren't suffered from this issue.


More information about the Mercurial-devel mailing list