-
Notifications
You must be signed in to change notification settings - Fork 740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move PyTypeInfo::AsRefTarget onto new trait HasPyGilBoundRef #2956
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Move `PyTypeInfo::AsRefTarget` onto new trait `HasPyGilBoundRef`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ use crate::pycell::{PyBorrowError, PyBorrowMutError, PyCell}; | |
use crate::types::{PyDict, PyString, PyTuple}; | ||
use crate::{ | ||
ffi, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, PyAny, PyClass, PyClassInitializer, | ||
PyRef, PyRefMut, PyTypeInfo, Python, ToPyObject, | ||
PyRef, PyRefMut, Python, ToPyObject, | ||
}; | ||
use std::marker::PhantomData; | ||
use std::mem; | ||
|
@@ -39,6 +39,16 @@ pub unsafe trait PyNativeType: Sized { | |
} | ||
} | ||
|
||
/// Trait to denote types which have a GIL-bound reference, e.g. PyAny has &'a PyAny. | ||
/// | ||
/// # Safety | ||
/// `Py<T>` will hand out references to `Self::AsRefTarget`. This _must_ have the | ||
/// same layout as `*mut ffi::PyObject`. | ||
pub unsafe trait HasPyGilBoundRef { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO the name could use some work. I don't have a name that is obviously good, but what about something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing. I think we can sit on this and think for a bit. I'm a bit lost for a good name too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to add completely different colour for the bikeshed: |
||
/// Type resulting from Py::as_ref or Py::into_ref | ||
type AsRefTarget: PyNativeType; | ||
} | ||
|
||
/// A GIL-independent reference to an object allocated on the Python heap. | ||
/// | ||
/// This type does not auto-dereference to the inner object because you must prove you hold the GIL to access it. | ||
|
@@ -262,7 +272,7 @@ where | |
|
||
impl<T> Py<T> | ||
where | ||
T: PyTypeInfo, | ||
T: HasPyGilBoundRef, | ||
{ | ||
/// Borrows a GIL-bound reference to the contained `T`. | ||
/// | ||
|
@@ -949,7 +959,7 @@ impl<T> Drop for Py<T> { | |
|
||
impl<'a, T> FromPyObject<'a> for Py<T> | ||
where | ||
T: PyTypeInfo, | ||
T: HasPyGilBoundRef, | ||
&'a T::AsRefTarget: FromPyObject<'a>, | ||
T::AsRefTarget: 'a + AsPyPointer, | ||
{ | ||
|
@@ -968,14 +978,14 @@ where | |
/// Use .as_ref() to get the GIL-scoped error if you need to inspect the cause. | ||
impl<T> std::error::Error for Py<T> | ||
where | ||
T: std::error::Error + PyTypeInfo, | ||
T: std::error::Error + HasPyGilBoundRef, | ||
T::AsRefTarget: std::fmt::Display, | ||
{ | ||
} | ||
|
||
impl<T> std::fmt::Display for Py<T> | ||
where | ||
T: PyTypeInfo, | ||
T: HasPyGilBoundRef, | ||
T::AsRefTarget: std::fmt::Display, | ||
{ | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the layout part guaranteed by
PyNativeType
? Unless "this" refers toSelf
rather than the target.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think so. Probably still worth repeating this constraint here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure 👍