Skip to content

Commit

Permalink
switch trait sealing in impl_ module (#4817)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored Dec 23, 2024
1 parent 117d986 commit 14c5402
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
25 changes: 13 additions & 12 deletions src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,27 @@ pub fn weaklist_offset<T: PyClass>() -> ffi::Py_ssize_t {
PyClassObject::<T>::weaklist_offset()
}

mod sealed {
pub trait Sealed {}

impl Sealed for super::PyClassDummySlot {}
impl Sealed for super::PyClassDictSlot {}
impl Sealed for super::PyClassWeakRefSlot {}
impl Sealed for super::ThreadCheckerImpl {}
impl<T: Send> Sealed for super::SendablePyClass<T> {}
}

/// Represents the `__dict__` field for `#[pyclass]`.
pub trait PyClassDict {
pub trait PyClassDict: sealed::Sealed {
/// Initial form of a [PyObject](crate::ffi::PyObject) `__dict__` reference.
const INIT: Self;
/// Empties the dictionary of its key-value pairs.
#[inline]
fn clear_dict(&mut self, _py: Python<'_>) {}
private_decl! {}
}

/// Represents the `__weakref__` field for `#[pyclass]`.
pub trait PyClassWeakRef {
pub trait PyClassWeakRef: sealed::Sealed {
/// Initializes a `weakref` instance.
const INIT: Self;
/// Clears the weak references to the given object.
Expand All @@ -64,19 +73,16 @@ pub trait PyClassWeakRef {
/// - The GIL must be held.
#[inline]
unsafe fn clear_weakrefs(&mut self, _obj: *mut ffi::PyObject, _py: Python<'_>) {}
private_decl! {}
}

/// Zero-sized dummy field.
pub struct PyClassDummySlot;

impl PyClassDict for PyClassDummySlot {
private_impl! {}
const INIT: Self = PyClassDummySlot;
}

impl PyClassWeakRef for PyClassDummySlot {
private_impl! {}
const INIT: Self = PyClassDummySlot;
}

Expand All @@ -88,7 +94,6 @@ impl PyClassWeakRef for PyClassDummySlot {
pub struct PyClassDictSlot(*mut ffi::PyObject);

impl PyClassDict for PyClassDictSlot {
private_impl! {}
const INIT: Self = Self(std::ptr::null_mut());
#[inline]
fn clear_dict(&mut self, _py: Python<'_>) {
Expand All @@ -106,7 +111,6 @@ impl PyClassDict for PyClassDictSlot {
pub struct PyClassWeakRefSlot(*mut ffi::PyObject);

impl PyClassWeakRef for PyClassWeakRefSlot {
private_impl! {}
const INIT: Self = Self(std::ptr::null_mut());
#[inline]
unsafe fn clear_weakrefs(&mut self, obj: *mut ffi::PyObject, _py: Python<'_>) {
Expand Down Expand Up @@ -1034,12 +1038,11 @@ impl<T> PyClassNewTextSignature<T> for &'_ PyClassImplCollector<T> {
// Thread checkers

#[doc(hidden)]
pub trait PyClassThreadChecker<T>: Sized {
pub trait PyClassThreadChecker<T>: Sized + sealed::Sealed {
fn ensure(&self);
fn check(&self) -> bool;
fn can_drop(&self, py: Python<'_>) -> bool;
fn new() -> Self;
private_decl! {}
}

/// Default thread checker for `#[pyclass]`.
Expand All @@ -1062,7 +1065,6 @@ impl<T: Send> PyClassThreadChecker<T> for SendablePyClass<T> {
fn new() -> Self {
SendablePyClass(PhantomData)
}
private_impl! {}
}

/// Thread checker for `#[pyclass(unsendable)]` types.
Expand Down Expand Up @@ -1111,7 +1113,6 @@ impl<T> PyClassThreadChecker<T> for ThreadCheckerImpl {
fn new() -> Self {
ThreadCheckerImpl(thread::current().id())
}
private_impl! {}
}

/// Trait denoting that this class is suitable to be used as a base type for PyClass.
Expand Down
17 changes: 0 additions & 17 deletions src/internal_tricks.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
use crate::ffi::{self, Py_ssize_t, PY_SSIZE_T_MAX};
pub struct PrivateMarker;

macro_rules! private_decl {
() => {
/// This trait is private to implement; this method exists to make it
/// impossible to implement outside the crate.
fn __private__(&self) -> crate::internal_tricks::PrivateMarker;
};
}

macro_rules! private_impl {
() => {
fn __private__(&self) -> crate::internal_tricks::PrivateMarker {
crate::internal_tricks::PrivateMarker
}
};
}

macro_rules! pyo3_exception {
($doc: expr, $name: ident, $base: ty) => {
Expand Down

0 comments on commit 14c5402

Please sign in to comment.