Skip to content

Commit a6c0fdd

Browse files
authored
add Sealed to probe and pyfunction traits (#5806)
* add Sealed to probe traits * add Sealed to pyfunction traits
1 parent 5e317f0 commit a6c0fdd

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/impl_/pyclass/probes.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ use crate::{FromPyObject, Py, PyClass, PyClassInitializer};
1414
/// The true case is defined in the zero-sized type's impl block, which is
1515
/// gated on some property like trait bound or only being implemented
1616
/// for fixed concrete types.
17-
pub trait Probe {
17+
pub trait Probe: probe::Sealed {
1818
const VALUE: bool = false;
1919
}
2020

21+
/// Seals `Probe` so that types outside PyO3 cannot implement it.
22+
mod probe {
23+
pub trait Sealed {}
24+
}
25+
2126
macro_rules! probe {
2227
($name:ident) => {
2328
pub struct $name<T>(PhantomData<T>);
2429
impl<T> Probe for $name<T> {}
30+
impl<T> probe::Sealed for $name<T> {}
2531
};
2632
}
2733

@@ -93,6 +99,7 @@ impl<E> IsReturningEmptyTuple<Result<(), E>> {
9399
}
94100

95101
probe!(IsPyClass);
102+
96103
impl<T> IsPyClass<T>
97104
where
98105
T: PyClass,
@@ -108,6 +115,7 @@ where
108115
}
109116

110117
probe!(IsInitializerTuple);
118+
111119
impl<S, B> IsInitializerTuple<(S, B)>
112120
where
113121
S: PyClass<BaseType = B>,

src/impl_/pyfunction.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,22 @@ impl PyFunctionDef {
3939

4040
/// Trait to enable the use of `wrap_pyfunction` with both `Python` and `PyModule`,
4141
/// and also to infer the return type of either `&'py PyCFunction` or `Bound<'py, PyCFunction>`.
42-
pub trait WrapPyFunctionArg<'py, T> {
42+
pub trait WrapPyFunctionArg<'py, T>: wrap_pyfunctionarg::Sealed {
4343
fn wrap_pyfunction(self, function_def: &'static PyFunctionDef) -> PyResult<T>;
4444
}
4545

46+
/// Seals `WrapPyFunctionArg` so that types outside PyO3 cannot implement it.
47+
mod wrap_pyfunctionarg {
48+
use crate::{types::PyModule, Borrowed, Bound, Python};
49+
50+
pub trait Sealed {}
51+
impl<'py> Sealed for Bound<'py, PyModule> {}
52+
impl<'py> Sealed for &Bound<'py, PyModule> {}
53+
impl<'a, 'py> Sealed for Borrowed<'a, 'py, PyModule> {}
54+
impl<'a, 'py> Sealed for &Borrowed<'a, 'py, PyModule> {}
55+
impl<'py> Sealed for Python<'py> {}
56+
}
57+
4658
impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for Bound<'py, PyModule> {
4759
fn wrap_pyfunction(
4860
self,

0 commit comments

Comments
 (0)