Skip to content

Commit f85b22c

Browse files
committed
switch trait sealing in impl_ module
1 parent 54cfaf2 commit f85b22c

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

src/impl_/pyclass.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,27 @@ pub fn weaklist_offset<T: PyClass>() -> ffi::Py_ssize_t {
4343
PyClassObject::<T>::weaklist_offset()
4444
}
4545

46+
mod sealed {
47+
pub trait Sealed {}
48+
49+
impl Sealed for super::PyClassDummySlot {}
50+
impl Sealed for super::PyClassDictSlot {}
51+
impl Sealed for super::PyClassWeakRefSlot {}
52+
impl Sealed for super::ThreadCheckerImpl {}
53+
impl<T: Send> Sealed for super::SendablePyClass<T> {}
54+
}
55+
4656
/// Represents the `__dict__` field for `#[pyclass]`.
47-
pub trait PyClassDict {
57+
pub trait PyClassDict: sealed::Sealed {
4858
/// Initial form of a [PyObject](crate::ffi::PyObject) `__dict__` reference.
4959
const INIT: Self;
5060
/// Empties the dictionary of its key-value pairs.
5161
#[inline]
5262
fn clear_dict(&mut self, _py: Python<'_>) {}
53-
private_decl! {}
5463
}
5564

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

7078
/// Zero-sized dummy field.
7179
pub struct PyClassDummySlot;
7280

7381
impl PyClassDict for PyClassDummySlot {
74-
private_impl! {}
7582
const INIT: Self = PyClassDummySlot;
7683
}
7784

7885
impl PyClassWeakRef for PyClassDummySlot {
79-
private_impl! {}
8086
const INIT: Self = PyClassDummySlot;
8187
}
8288

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

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

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

10361040
#[doc(hidden)]
1037-
pub trait PyClassThreadChecker<T>: Sized {
1041+
pub trait PyClassThreadChecker<T>: Sized + sealed::Sealed {
10381042
fn ensure(&self);
10391043
fn check(&self) -> bool;
10401044
fn can_drop(&self, py: Python<'_>) -> bool;
10411045
fn new() -> Self;
1042-
private_decl! {}
10431046
}
10441047

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

10681070
/// Thread checker for `#[pyclass(unsendable)]` types.
@@ -1111,7 +1113,6 @@ impl<T> PyClassThreadChecker<T> for ThreadCheckerImpl {
11111113
fn new() -> Self {
11121114
ThreadCheckerImpl(thread::current().id())
11131115
}
1114-
private_impl! {}
11151116
}
11161117

11171118
/// Trait denoting that this class is suitable to be used as a base type for PyClass.

src/internal_tricks.rs

-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
11
use crate::ffi::{Py_ssize_t, PY_SSIZE_T_MAX};
2-
pub struct PrivateMarker;
3-
4-
macro_rules! private_decl {
5-
() => {
6-
/// This trait is private to implement; this method exists to make it
7-
/// impossible to implement outside the crate.
8-
fn __private__(&self) -> crate::internal_tricks::PrivateMarker;
9-
};
10-
}
11-
12-
macro_rules! private_impl {
13-
() => {
14-
fn __private__(&self) -> crate::internal_tricks::PrivateMarker {
15-
crate::internal_tricks::PrivateMarker
16-
}
17-
};
18-
}
192

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

0 commit comments

Comments
 (0)