From 96da64bc5eb05e55332fd7bb381171d4d69971bb Mon Sep 17 00:00:00 2001 From: Cheuk Ting Ho Date: Wed, 9 Oct 2024 18:30:37 +0100 Subject: [PATCH] seal PyAddToModule (#4606) * seal PyAddToModule * adding change log * fix changelog name --- newsfragments/4606.changed.md | 1 + src/impl_/pymodule.rs | 2 +- src/sealed.rs | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 newsfragments/4606.changed.md diff --git a/newsfragments/4606.changed.md b/newsfragments/4606.changed.md new file mode 100644 index 00000000000..173252992c1 --- /dev/null +++ b/newsfragments/4606.changed.md @@ -0,0 +1 @@ +Seal `PyAddToModule` trait. diff --git a/src/impl_/pymodule.rs b/src/impl_/pymodule.rs index 97eb2103dfe..da17fe4bbdc 100644 --- a/src/impl_/pymodule.rs +++ b/src/impl_/pymodule.rs @@ -144,7 +144,7 @@ impl ModuleDef { /// Trait to add an element (class, function...) to a module. /// /// Currently only implemented for classes. -pub trait PyAddToModule { +pub trait PyAddToModule: crate::sealed::Sealed { fn add_to_module(&'static self, module: &Bound<'_, PyModule>) -> PyResult<()>; } diff --git a/src/sealed.rs b/src/sealed.rs index 20f31f82e01..fef7a02aca7 100644 --- a/src/sealed.rs +++ b/src/sealed.rs @@ -5,6 +5,11 @@ use crate::types::{ }; use crate::{ffi, Bound, PyAny, PyResult}; +use crate::impl_::{ + pymethods::PyMethodDef, + pymodule::{AddClassToModule, AddTypeToModule, ModuleDef}, +}; + pub trait Sealed {} // for FfiPtrExt @@ -36,3 +41,8 @@ impl Sealed for Bound<'_, PyType> {} impl Sealed for Bound<'_, PyWeakref> {} impl Sealed for Bound<'_, PyWeakrefProxy> {} impl Sealed for Bound<'_, PyWeakrefReference> {} + +impl Sealed for AddTypeToModule {} +impl Sealed for AddClassToModule {} +impl Sealed for PyMethodDef {} +impl Sealed for ModuleDef {}