Make IntoPyObject for Bound & Borrowed more generic#5831
Make IntoPyObject for Bound & Borrowed more generic#5831davidhewitt merged 1 commit intoPyO3:mainfrom
IntoPyObject for Bound & Borrowed more generic#5831Conversation
c77f5ea to
114d4b0
Compare
114d4b0 to
091400c
Compare
davidhewitt
left a comment
There was a problem hiding this comment.
I can see it being reasonable to relax these bounds 👍
| pub fn as_unbound(&self) -> &'a Py<T> { | ||
| // Safety: NonNull<ffi::PyObject> is layout-compatible with Py<T> | ||
| unsafe { NonNull::from(&self.0).cast().as_ref() } | ||
| } |
There was a problem hiding this comment.
I think this might be unsound. I think the &Py can only live as long as the reference to the Borrowed (&self) and not for 'a, as the pointer we get from self is only valid until the borrow ends at the end of the scope.
There was a problem hiding this comment.
You're absolutely right, thanks for catching this and sorry I missed it review!
There was a problem hiding this comment.
Glad I could catch it. Opened #5832 to revert this for now.
There was a problem hiding this comment.
Sorry for my mistake, I will take a closer look tomorrow so I understand the relationship between these lifetimes better.
There was a problem hiding this comment.
No worries, I made a similar form of exactly this mistake many times when we were trying to implement the Bound API originally!
)" This reverts commit ee66c21.
Split out of #5822 because it is not needed there anymore, but might be useful for other generic code.