From c60a13521c07395d7f9f51dcc495a057d4fa2a80 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Sat, 2 Mar 2024 23:37:07 +0000 Subject: [PATCH] review: Icxolu feedback --- src/instance.rs | 12 ++++-------- src/pycell/impl_.rs | 28 ++++++++++++++-------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/instance.rs b/src/instance.rs index 10ed78dc618..66c530d5f53 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -325,15 +325,11 @@ where where T: PyClass + Sync, { - // SAFETY: The class itself is frozen and `Sync`. - unsafe { &*self.get_class_object().get_ptr() } + self.1.get() } pub(crate) fn get_class_object(&self) -> &PyClassObject { - let class_object = self.as_ptr().cast::>(); - // SAFETY: Bound is known to contain an object which is laid out in memory as a - // PyClassObject. - unsafe { &*class_object } + self.1.get_class_object() } } @@ -1180,14 +1176,14 @@ where where T: PyClass + Sync, { - // SAFETY: The class itself is frozen and `Sync` + // Safety: The class itself is frozen and `Sync` unsafe { &*self.get_class_object().get_ptr() } } /// Get a view on the underlying `PyClass` contents. pub(crate) fn get_class_object(&self) -> &PyClassObject { let class_object = self.as_ptr().cast::>(); - // SAFETY: Bound is known to contain an object which is laid out in memory as a + // Safety: Bound is known to contain an object which is laid out in memory as a // PyClassObject. unsafe { &*class_object } } diff --git a/src/pycell/impl_.rs b/src/pycell/impl_.rs index a126b4dd2b6..378bec04993 100644 --- a/src/pycell/impl_.rs +++ b/src/pycell/impl_.rs @@ -161,19 +161,19 @@ impl PyClassBorrowChecker for BorrowChecker { pub trait GetBorrowChecker { fn borrow_checker( - cell: &PyClassObject, + class_object: &PyClassObject, ) -> &::Checker; } impl> GetBorrowChecker for MutableClass { fn borrow_checker(class_object: &PyClassObject) -> &BorrowChecker { - &cell.contents.borrow_checker + &class_object.contents.borrow_checker } } impl> GetBorrowChecker for ImmutableClass { - fn borrow_checker(cell: &PyClassObject) -> &EmptySlot { - &cell.contents.borrow_checker + fn borrow_checker(class_object: &PyClassObject) -> &EmptySlot { + &class_object.contents.borrow_checker } } @@ -183,8 +183,8 @@ where T::BaseType: PyClassImpl + PyClassBaseType>, ::PyClassMutability: PyClassMutability, { - fn borrow_checker(cell: &PyClassObject) -> &BorrowChecker { - <::PyClassMutability as GetBorrowChecker>::borrow_checker(&cell.ob_base) + fn borrow_checker(class_object: &PyClassObject) -> &BorrowChecker { + <::PyClassMutability as GetBorrowChecker>::borrow_checker(&class_object.ob_base) } } @@ -221,7 +221,7 @@ where let type_obj = T::type_object_raw(py); // For `#[pyclass]` types which inherit from PyAny, we can just call tp_free if type_obj == std::ptr::addr_of_mut!(ffi::PyBaseObject_Type) { - return get_tp_free(ffi::Py_TYPE(slf))(slf as _); + return get_tp_free(ffi::Py_TYPE(slf))(slf.cast()); } // More complex native types (e.g. `extends=PyDict`) require calling the base's dealloc. @@ -235,9 +235,9 @@ where if ffi::PyType_FastSubclass(type_obj, ffi::Py_TPFLAGS_BASE_EXC_SUBCLASS) == 1 { ffi::PyObject_GC_Track(slf.cast()); } - dealloc(slf as _); + dealloc(slf); } else { - get_tp_free(ffi::Py_TYPE(slf))(slf as _); + get_tp_free(ffi::Py_TYPE(slf))(slf.cast()); } } @@ -317,12 +317,12 @@ where } unsafe fn tp_dealloc(py: Python<'_>, slf: *mut ffi::PyObject) { // Safety: Python only calls tp_dealloc when no references to the object remain. - let cell = &mut *(slf as *mut PyClassObject); - if cell.contents.thread_checker.can_drop(py) { - ManuallyDrop::drop(&mut cell.contents.value); + let class_object = &mut *(slf.cast::>()); + if class_object.contents.thread_checker.can_drop(py) { + ManuallyDrop::drop(&mut class_object.contents.value); } - cell.contents.dict.clear_dict(py); - cell.contents.weakref.clear_weakrefs(slf, py); + class_object.contents.dict.clear_dict(py); + class_object.contents.weakref.clear_weakrefs(slf, py); ::LayoutAsBase::tp_dealloc(py, slf) } }