-
Notifications
You must be signed in to change notification settings - Fork 783
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
allow Bound<'_, T>
in #[pymethods] self
position
#3896
Changes from 1 commit
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 | ||||
---|---|---|---|---|---|---|
|
@@ -3,10 +3,12 @@ use crate::exceptions::PyStopAsyncIteration; | |||||
use crate::gil::LockGIL; | ||||||
use crate::impl_::panic::PanicTrap; | ||||||
use crate::internal_tricks::extract_c_string; | ||||||
use crate::pycell::{PyBorrowError, PyBorrowMutError}; | ||||||
use crate::pyclass::boolean_struct::False; | ||||||
use crate::types::{any::PyAnyMethods, PyModule, PyType}; | ||||||
use crate::{ | ||||||
ffi, Bound, Py, PyAny, PyCell, PyClass, PyErr, PyObject, PyResult, PyTraverseError, PyVisit, | ||||||
Python, | ||||||
ffi, Bound, DowncastError, Py, PyAny, PyCell, PyClass, PyErr, PyObject, PyRef, PyRefMut, | ||||||
PyResult, PyTraverseError, PyTypeCheck, PyVisit, Python, | ||||||
}; | ||||||
use std::borrow::Cow; | ||||||
use std::ffi::CStr; | ||||||
|
@@ -490,6 +492,12 @@ impl<'a, 'py> BoundRef<'a, 'py, PyAny> { | |||||
Bound::ref_from_ptr_or_opt(py, ptr).as_ref().map(BoundRef) | ||||||
} | ||||||
|
||||||
pub unsafe fn downcast<T: PyTypeCheck>( | ||||||
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. I'd think this one is actually safe, given it's checked?
Suggested change
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. Your completely right, one of those copy pasta errors 😆 . I'll fix it |
||||||
self, | ||||||
) -> Result<BoundRef<'a, 'py, T>, DowncastError<'a, 'py>> { | ||||||
self.0.downcast::<T>().map(BoundRef) | ||||||
} | ||||||
|
||||||
pub unsafe fn downcast_unchecked<T>(self) -> BoundRef<'a, 'py, T> { | ||||||
BoundRef(self.0.downcast_unchecked::<T>()) | ||||||
} | ||||||
|
@@ -511,6 +519,36 @@ impl<'a> From<BoundRef<'a, 'a, PyModule>> for &'a PyModule { | |||||
} | ||||||
} | ||||||
|
||||||
impl<'a, 'py, T: PyClass> From<BoundRef<'a, 'py, T>> for &'a PyCell<T> { | ||||||
#[inline] | ||||||
fn from(bound: BoundRef<'a, 'py, T>) -> Self { | ||||||
bound.0.as_gil_ref() | ||||||
} | ||||||
} | ||||||
|
||||||
impl<'a, 'py, T: PyClass> TryFrom<BoundRef<'a, 'py, T>> for PyRef<'py, T> { | ||||||
type Error = PyBorrowError; | ||||||
#[inline] | ||||||
fn try_from(value: BoundRef<'a, 'py, T>) -> Result<Self, Self::Error> { | ||||||
value.0.clone().into_gil_ref().try_into() | ||||||
} | ||||||
} | ||||||
Comment on lines
+527
to
+533
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. This |
||||||
|
||||||
impl<'a, 'py, T: PyClass<Frozen = False>> TryFrom<BoundRef<'a, 'py, T>> for PyRefMut<'py, T> { | ||||||
type Error = PyBorrowMutError; | ||||||
#[inline] | ||||||
fn try_from(value: BoundRef<'a, 'py, T>) -> Result<Self, Self::Error> { | ||||||
value.0.clone().into_gil_ref().try_into() | ||||||
} | ||||||
} | ||||||
|
||||||
impl<'a, 'py, T> From<BoundRef<'a, 'py, T>> for Bound<'py, T> { | ||||||
#[inline] | ||||||
fn from(bound: BoundRef<'a, 'py, T>) -> Self { | ||||||
bound.0.clone() | ||||||
} | ||||||
} | ||||||
|
||||||
impl<'a, 'py, T> From<BoundRef<'a, 'py, T>> for &'a Bound<'py, T> { | ||||||
#[inline] | ||||||
fn from(bound: BoundRef<'a, 'py, T>) -> Self { | ||||||
|
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.
It might be nice to rename this
TryFromPyCell
variant toTryFromBoundRef
.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.
Good idea!