PyO3 / PyO3/pyo3

Safely passing a mutable reference to Python?

Open
#1,180 17 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

I have a feeling I'm not alone in wanting to pass a mutable Rust reference to Python code. In my case, I want Python code to draw into a Piet RenderContext. There is basically no reasonable way to do this without a mutable reference, for example by copying data.

Having read through the code and some blogs on the issue, I believe I may have a workable answer. Probably easiest to post my code:

https://gist.github.com/raphlinus/479df97a7ba715b494b87af9071e9b87

The short explanation is that the PyCell holds a RefCell to an optional mutable reference. There's also a guard object which clears that reference when the guard is dropped. There is a small amount of unsafe, but it's only to adjust lifetimes; I'm not mucking with raw pointers at all (as I had done in a previous iteration). In particular, I'm relying on RefCell for mutability guarantees, so I'm not particularly worried about that.

I post this issue mostly to ask for review that the approach is sound. It's also possible there's some easier way to do this I'm missing. If it is a good approach, likely it should be documented, as it seems like something a lot of people want to do.

Many thanks for your consideration!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked gist and the issue's description of PyCell, RefCell, the optional mutable reference, and the guard object. Check whether the proposed lifetime adjustment and mutability guarantees are sound, and determine whether an easier approach exists. Done means reaching a decision on the approach and documenting it if accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.