|
3 | 3 | //! Conversions between various states of Rust and Python types and their wrappers.
|
4 | 4 | use crate::err::{self, PyDowncastError, PyResult};
|
5 | 5 | use crate::object::PyObject;
|
6 |
| -use crate::type_object::{PyDowncastImpl, PyTypeInfo}; |
| 6 | +use crate::type_object::PyTypeInfo; |
7 | 7 | use crate::types::PyTuple;
|
8 | 8 | use crate::{ffi, gil, Py, PyAny, PyCell, PyClass, PyNativeType, PyRef, PyRefMut, Python};
|
9 | 9 | use std::ptr::NonNull;
|
@@ -311,7 +311,7 @@ where
|
311 | 311 | /// If `T` implements `PyTryFrom`, we can convert `&PyAny` to `&T`.
|
312 | 312 | ///
|
313 | 313 | /// This trait is similar to `std::convert::TryFrom`
|
314 |
| -pub trait PyTryFrom<'v>: Sized + PyDowncastImpl { |
| 314 | +pub trait PyTryFrom<'v>: Sized + PyNativeType { |
315 | 315 | /// Cast from a concrete Python object type to PyObject.
|
316 | 316 | fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError>;
|
317 | 317 |
|
@@ -348,7 +348,7 @@ where
|
348 | 348 |
|
349 | 349 | impl<'v, T> PyTryFrom<'v> for T
|
350 | 350 | where
|
351 |
| - T: PyDowncastImpl + PyTypeInfo + PyNativeType, |
| 351 | + T: PyTypeInfo + PyNativeType, |
352 | 352 | {
|
353 | 353 | fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError> {
|
354 | 354 | let value = value.into();
|
@@ -460,28 +460,14 @@ where
|
460 | 460 | T: 'p + crate::PyNativeType,
|
461 | 461 | {
|
462 | 462 | unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self> {
|
463 |
| - NonNull::new(ptr).map(|p| Self::unchecked_downcast(gil::register_owned(py, p))) |
| 463 | + gil::register_owned(py, NonNull::new(ptr)?); |
| 464 | + Some(&*(ptr as *mut Self)) |
464 | 465 | }
|
465 | 466 | unsafe fn from_borrowed_ptr_or_opt(
|
466 |
| - py: Python<'p>, |
467 |
| - ptr: *mut ffi::PyObject, |
468 |
| - ) -> Option<&'p Self> { |
469 |
| - NonNull::new(ptr).map(|p| Self::unchecked_downcast(gil::register_borrowed(py, p))) |
470 |
| - } |
471 |
| -} |
472 |
| - |
473 |
| -unsafe impl<'p, T> FromPyPointer<'p> for PyCell<T> |
474 |
| -where |
475 |
| - T: PyClass, |
476 |
| -{ |
477 |
| - unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self> { |
478 |
| - NonNull::new(ptr).map(|p| &*(gil::register_owned(py, p).as_ptr() as *const PyCell<T>)) |
479 |
| - } |
480 |
| - unsafe fn from_borrowed_ptr_or_opt( |
481 |
| - py: Python<'p>, |
| 467 | + _py: Python<'p>, |
482 | 468 | ptr: *mut ffi::PyObject,
|
483 | 469 | ) -> Option<&'p Self> {
|
484 |
| - NonNull::new(ptr).map(|p| &*(gil::register_borrowed(py, p).as_ptr() as *const PyCell<T>)) |
| 470 | + NonNull::new(ptr as *mut Self).map(|p| &*p.as_ptr()) |
485 | 471 | }
|
486 | 472 | }
|
487 | 473 |
|
|
0 commit comments