Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I've been looking at
ManagedPyRef<T>
to understand how we can fix it for specialization.I think that a concept like this is nice, but in its current form I don't see a way that we can make this work well on stable Rust. It uses
AsPyPointer
trait to specialize on.min_specialization
feature doesn't allow specializing on traits so I think it could yet be many years before the current design is sound / stable.As well as this, there is a lifetime safety issue with using
ManagedPyRef<T>
withPyObject
(orPy<T>
) that leads to dangling objects. The following code compiles in currentmaster
:The snippet above prints
refcount: 0
, so clearly if I tried to pass this object to any other Python FFI I'll get a crash.I think there's ways to resolve this lifetime issue, but it still leaves me concerned that
ManagedPyRef
as implemented is probably not quite the right design.Also, we have the
ToBorrowedObject
trait which serves a very similar function but does not have this lifetime issue.Because it's a blocker for us getting onto stable Rust, I would like to suggest that for now we remove it. I would like to one day perhaps re-introduce this as
PyHandle<T>
, but only when we can get a safe design which compiles on stable.