Skip to content

Avoiding copies of arrays from python to rust #432

Answered by mejrs
grothesque asked this question in Q&A
Discussion options

You must be logged in to vote

But what if there was a Python array type that was truly immutable. Wouldn't it then be safe to obtain and hold multiple Rust references (even from different threads) to such an array? After all if these references from Rust are accounted for by Python's GC, they won't be destroyed. And since they are immutable, there's no way to modify them on the Python side as well.

You can make your own pyclasses that do this. See https://docs.rs/pyo3/latest/pyo3/prelude/struct.Py.html#method.get

PyString and PyBytes also have a similar method.

Replies: 2 comments 7 replies

Comment options

You must be logged in to vote
6 replies
@grothesque
Comment options

@davidhewitt
Comment options

@mejrs
Comment options

mejrs Jul 7, 2024
Collaborator

@grothesque
Comment options

@davidhewitt
Comment options

Answer selected by grothesque
Comment options

You must be logged in to vote
1 reply
@adamreichold
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants