@@ -4,7 +4,6 @@ use crate::call::PyCallArgs;
44use crate :: conversion:: IntoPyObject ;
55use crate :: err:: { PyErr , PyResult } ;
66use crate :: impl_:: pycell:: PyClassObject ;
7- use crate :: internal_tricks:: ptr_from_ref;
87use crate :: pycell:: { PyBorrowError , PyBorrowMutError } ;
98use crate :: pyclass:: boolean_struct:: { False , True } ;
109use crate :: types:: { any:: PyAnyMethods , string:: PyStringMethods , typeobject:: PyTypeMethods } ;
@@ -444,9 +443,10 @@ impl<'py> Bound<'py, PyAny> {
444443 _py : Python < ' py > ,
445444 ptr : & ' a * mut ffi:: PyObject ,
446445 ) -> & ' a Self {
446+ let ptr = NonNull :: from ( ptr) . cast ( ) ;
447447 // SAFETY: caller has upheld the safety contract,
448448 // and `Bound<PyAny>` is layout-compatible with `*mut ffi::PyObject`.
449- unsafe { & * ptr_from_ref ( ptr) . cast :: < Bound < ' py , PyAny > > ( ) }
449+ unsafe { ptr. as_ref ( ) }
450450 }
451451
452452 /// Variant of the above which returns `None` for null pointers.
@@ -458,9 +458,10 @@ impl<'py> Bound<'py, PyAny> {
458458 _py : Python < ' py > ,
459459 ptr : & ' a * mut ffi:: PyObject ,
460460 ) -> & ' a Option < Self > {
461+ let ptr = NonNull :: from ( ptr) . cast ( ) ;
461462 // SAFETY: caller has upheld the safety contract,
462463 // and `Option<Bound<PyAny>>` is layout-compatible with `*mut ffi::PyObject`.
463- unsafe { & * ptr_from_ref ( ptr) . cast :: < Option < Bound < ' py , PyAny > > > ( ) }
464+ unsafe { ptr. as_ref ( ) }
464465 }
465466
466467 /// This slightly strange method is used to obtain `&Bound<PyAny>` from a [`NonNull`] in macro
@@ -477,9 +478,10 @@ impl<'py> Bound<'py, PyAny> {
477478 _py : Python < ' py > ,
478479 ptr : & ' a NonNull < ffi:: PyObject > ,
479480 ) -> & ' a Self {
481+ let ptr = NonNull :: from ( ptr) . cast ( ) ;
480482 // SAFETY: caller has upheld the safety contract,
481483 // and `Bound<PyAny>` is layout-compatible with `NonNull<ffi::PyObject>`.
482- unsafe { NonNull :: from ( ptr) . cast ( ) . as_ref ( ) }
484+ unsafe { ptr. as_ref ( ) }
483485 }
484486}
485487
@@ -839,9 +841,10 @@ impl<'py, T> Bound<'py, T> {
839841 /// Helper to cast to `Bound<'py, PyAny>`.
840842 #[ inline]
841843 pub fn as_any ( & self ) -> & Bound < ' py , PyAny > {
844+ let ptr = NonNull :: from ( self ) . cast ( ) ;
842845 // Safety: all Bound<T> have the same memory layout, and all Bound<T> are valid
843846 // Bound<PyAny>, so pointer casting is valid.
844- unsafe { & * ptr_from_ref ( self ) . cast :: < Bound < ' py , PyAny > > ( ) }
847+ unsafe { ptr . as_ref ( ) }
845848 }
846849
847850 /// Helper to cast to `Bound<'py, PyAny>`, transferring ownership.
@@ -1171,8 +1174,8 @@ impl<'py, T> Deref for Borrowed<'_, 'py, T> {
11711174
11721175 #[ inline]
11731176 fn deref ( & self ) -> & Bound < ' py , T > {
1174- // safety: Bound has the same layout as NonNull<ffi::PyObject>
1175- unsafe { & * ptr_from_ref ( & self . 0 ) . cast ( ) }
1177+ // SAFETY: self.0 is a valid object of type T
1178+ unsafe { Bound :: ref_from_non_null ( self . 2 , & self . 0 ) . cast_unchecked ( ) }
11761179 }
11771180}
11781181
@@ -1483,9 +1486,10 @@ impl<T> Py<T> {
14831486 /// Helper to cast to `Py<PyAny>`.
14841487 #[ inline]
14851488 pub fn as_any ( & self ) -> & Py < PyAny > {
1489+ let ptr = NonNull :: from ( self ) . cast ( ) ;
14861490 // Safety: all Py<T> have the same memory layout, and all Py<T> are valid
14871491 // Py<PyAny>, so pointer casting is valid.
1488- unsafe { & * ptr_from_ref ( self ) . cast :: < Py < PyAny > > ( ) }
1492+ unsafe { ptr . as_ref ( ) }
14891493 }
14901494
14911495 /// Helper to cast to `Py<PyAny>`, transferring ownership.
0 commit comments