add `Py::as_any` and `Py::into_any`
This commit is contained in:
parent
cbc97f8ea9
commit
4437e8f616
|
@ -0,0 +1 @@
|
|||
Add `Py::as_any` and `Py::into_any`.
|
|
@ -199,10 +199,7 @@ impl<'py, T> Bound<'py, T> {
|
|||
/// Helper to cast to `Bound<'py, PyAny>`, transferring ownership.
|
||||
pub fn into_any(self) -> Bound<'py, PyAny> {
|
||||
// Safety: all Bound<T> are valid Bound<PyAny>
|
||||
Bound(
|
||||
self.0,
|
||||
ManuallyDrop::new(unsafe { Py::from_non_null(self.into_non_null()) }),
|
||||
)
|
||||
Bound(self.0, ManuallyDrop::new(self.unbind().into_any()))
|
||||
}
|
||||
|
||||
/// Casts this `Bound<T>` to a `Borrowed<T>` smart pointer.
|
||||
|
@ -721,6 +718,19 @@ impl<T> Py<T> {
|
|||
std::mem::forget(self);
|
||||
ptr
|
||||
}
|
||||
|
||||
/// Helper to cast to `Py<PyAny>`.
|
||||
pub fn as_any(&self) -> &Py<PyAny> {
|
||||
// Safety: all Py<T> have the same memory layout, and all Py<T> are valid
|
||||
// Py<PyAny>, so pointer casting is valid.
|
||||
unsafe { &*(self as *const Self).cast::<Py<PyAny>>() }
|
||||
}
|
||||
|
||||
/// Helper to cast to `Py<PyAny>`, transferring ownership.
|
||||
pub fn into_any(self) -> Py<PyAny> {
|
||||
// Safety: all Py<T> are valid Py<PyAny>
|
||||
unsafe { Py::from_non_null(self.into_non_null()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Py<T>
|
||||
|
|
Loading…
Reference in New Issue