Skip to content

Commit 89f4636

Browse files
committed
remove transmute by restricting trait bound
1 parent 75cd432 commit 89f4636

2 files changed

Lines changed: 9 additions & 14 deletions

File tree

src/impl_/pyclass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ pub trait PyClassBaseType: Sized {
11191119
type Initializer: PyObjectInit<Self>;
11201120
type PyClassMutability: PyClassMutability;
11211121
/// The type of object layout to use for ancestors or descendants of this type.
1122-
type Layout<T: PyClassImpl>: PyClassObjectLayout<T>;
1122+
type Layout<T: PyClassImpl>;
11231123
}
11241124

11251125
/// Implementation of tp_dealloc for pyclasses without gc

src/pycell/impl_.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ pub struct PyStaticClassObject<T: PyClassImpl> {
388388
contents: PyClassObjectContents<T>,
389389
}
390390

391-
impl<T: PyClassImpl> PyClassObjectLayout<T> for PyStaticClassObject<T> {
391+
impl<T: PyClassImpl<Layout = Self>> PyClassObjectLayout<T> for PyStaticClassObject<T> {
392392
/// Gets the offset of the contents from the start of the struct in bytes.
393393
const CONTENTS_OFFSET: PyObjectOffset = {
394394
let offset = offset_of!(Self, contents);
@@ -446,16 +446,14 @@ impl<T: PyClassImpl> PyClassObjectLayout<T> for PyStaticClassObject<T> {
446446
}
447447

448448
fn borrow_checker(&self) -> &<T::PyClassMutability as PyClassMutability>::Checker {
449-
// Safety: T::Layout must be PyStaticClassObject<T>
450-
let slf: &T::Layout = unsafe { std::mem::transmute(self) };
451-
T::PyClassMutability::borrow_checker(slf)
449+
T::PyClassMutability::borrow_checker(self)
452450
}
453451
}
454452

455453
unsafe impl<T: PyClassImpl> PyLayout<T> for PyStaticClassObject<T> {}
456454
impl<T: PyClass> PySizedLayout<T> for PyStaticClassObject<T> {}
457455

458-
impl<T: PyClassImpl> PyClassObjectBaseLayout<T> for PyStaticClassObject<T>
456+
impl<T: PyClassImpl<Layout = Self>> PyClassObjectBaseLayout<T> for PyStaticClassObject<T>
459457
where
460458
<T::BaseType as PyClassBaseType>::LayoutAsBase: PyClassObjectBaseLayout<T::BaseType>,
461459
{
@@ -486,23 +484,22 @@ pub struct PyVariableClassObject<T: PyClassImpl> {
486484
ob_base: <T::BaseType as PyClassBaseType>::LayoutAsBase,
487485
}
488486

489-
impl<T: PyClassImpl> PyVariableClassObject<T> {
490-
#[cfg(Py_3_12)]
487+
#[cfg(Py_3_12)]
488+
impl<T: PyClassImpl<Layout = Self>> PyVariableClassObject<T> {
491489
fn get_contents_of_obj(obj: *mut ffi::PyObject) -> *mut PyClassObjectContents<T> {
492490
// https://peps.python.org/pep-0697/
493491
let type_obj = unsafe { ffi::Py_TYPE(obj) };
494492
let pointer = unsafe { ffi::PyObject_GetTypeData(obj, type_obj) };
495493
pointer.cast()
496494
}
497495

498-
#[cfg(Py_3_12)]
499496
fn get_contents_ptr(&self) -> *mut PyClassObjectContents<T> {
500497
Self::get_contents_of_obj(self as *const PyVariableClassObject<T> as *mut ffi::PyObject)
501498
}
502499
}
503500

504501
#[cfg(Py_3_12)]
505-
impl<T: PyClassImpl> PyClassObjectLayout<T> for PyVariableClassObject<T> {
502+
impl<T: PyClassImpl<Layout = Self>> PyClassObjectLayout<T> for PyVariableClassObject<T> {
506503
/// Gets the offset of the contents from the start of the struct in bytes.
507504
const CONTENTS_OFFSET: PyObjectOffset = PyObjectOffset::Relative(0);
508505
const BASIC_SIZE: ffi::Py_ssize_t = {
@@ -547,16 +544,14 @@ impl<T: PyClassImpl> PyClassObjectLayout<T> for PyVariableClassObject<T> {
547544
}
548545

549546
fn borrow_checker(&self) -> &<T::PyClassMutability as PyClassMutability>::Checker {
550-
// Safety: T::Layout must be PyStaticClassObject<T>
551-
let slf: &T::Layout = unsafe { std::mem::transmute(self) };
552-
T::PyClassMutability::borrow_checker(slf)
547+
T::PyClassMutability::borrow_checker(self)
553548
}
554549
}
555550

556551
unsafe impl<T: PyClassImpl> PyLayout<T> for PyVariableClassObject<T> {}
557552

558553
#[cfg(Py_3_12)]
559-
impl<T: PyClassImpl> PyClassObjectBaseLayout<T> for PyVariableClassObject<T>
554+
impl<T: PyClassImpl<Layout = Self>> PyClassObjectBaseLayout<T> for PyVariableClassObject<T>
560555
where
561556
<T::BaseType as PyClassBaseType>::LayoutAsBase: PyClassObjectBaseLayout<T::BaseType>,
562557
{

0 commit comments

Comments
 (0)