Safely passing a mutable reference to Python?
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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