Skip to content

Commit 17cf97d

Browse files
authored
Merge pull request #889 from kngwyu/refactor-pymethods-inventory
Unify PyMethodsInventoryDispatch and PyMethodsProtocol
2 parents e40f022 + b6befcf commit 17cf97d

File tree

6 files changed

+29
-36
lines changed

6 files changed

+29
-36
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2424
* `&'static Py~` being allowed as arguments. [#869](https://github.com/PyO3/pyo3/pull/869)
2525
* `#[pyo3(get)]` for `Py<T>`. [#880](https://github.com/PyO3/pyo3/pull/880)
2626

27+
### Removed
28+
* `PyMethodsProtocol` is now renamed to `PyMethodsImpl` and hidden. [#889](https://github.com/PyO3/pyo3/pull/889)
29+
2730

2831
## [0.9.2]
2932

guide/src/class.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ impl pyo3::IntoPy<PyObject> for MyClass {
6363
}
6464
}
6565

66-
pub struct MyClassGeneratedPyo3Inventory {
66+
pub struct Pyo3MethodsInventoryForMyClass {
6767
methods: &'static [pyo3::class::PyMethodDefType],
6868
}
6969

70-
impl pyo3::class::methods::PyMethodsInventory for MyClassGeneratedPyo3Inventory {
70+
impl pyo3::class::methods::PyMethodsInventory for Pyo3MethodsInventoryForMyClass {
7171
fn new(methods: &'static [pyo3::class::PyMethodDefType]) -> Self {
7272
Self { methods }
7373
}
@@ -77,11 +77,11 @@ impl pyo3::class::methods::PyMethodsInventory for MyClassGeneratedPyo3Inventory
7777
}
7878
}
7979

80-
impl pyo3::class::methods::PyMethodsInventoryDispatch for MyClass {
81-
type InventoryType = MyClassGeneratedPyo3Inventory;
80+
impl pyo3::class::methods::PyMethodsImpl for MyClass {
81+
type Methods = Pyo3MethodsInventoryForMyClass;
8282
}
8383

84-
pyo3::inventory::collect!(MyClassGeneratedPyo3Inventory);
84+
pyo3::inventory::collect!(Pyo3MethodsInventoryForMyClass);
8585
# let gil = Python::acquire_gil();
8686
# let py = gil.python();
8787
# let cls = py.get_type::<MyClass>();

pyo3-derive-backend/src/pyclass.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ fn parse_descriptors(item: &mut syn::Field) -> syn::Result<Vec<FnType>> {
213213
fn impl_inventory(cls: &syn::Ident) -> TokenStream {
214214
// Try to build a unique type that gives a hint about it's function when
215215
// it comes up in error messages
216-
let name = cls.to_string() + "GeneratedPyo3Inventory";
216+
let name = format!("Pyo3MethodsInventoryFor{}", cls);
217217
let inventory_cls = syn::Ident::new(&name, Span::call_site());
218218

219219
quote! {
@@ -234,8 +234,8 @@ fn impl_inventory(cls: &syn::Ident) -> TokenStream {
234234
}
235235
}
236236

237-
impl pyo3::class::methods::PyMethodsInventoryDispatch for #cls {
238-
type InventoryType = #inventory_cls;
237+
impl pyo3::class::methods::PyMethodsImpl for #cls {
238+
type Methods = #inventory_cls;
239239
}
240240

241241
pyo3::inventory::collect!(#inventory_cls);
@@ -461,7 +461,7 @@ fn impl_descriptors(
461461
Ok(quote! {
462462
pyo3::inventory::submit! {
463463
#![crate = pyo3] {
464-
type ClsInventory = <#cls as pyo3::class::methods::PyMethodsInventoryDispatch>::InventoryType;
464+
type ClsInventory = <#cls as pyo3::class::methods::PyMethodsImpl>::Methods;
465465
<ClsInventory as pyo3::class::methods::PyMethodsInventory>::new(&[#(#py_methods),*])
466466
}
467467
}

pyo3-derive-backend/src/pyimpl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn impl_methods(ty: &syn::Type, impls: &mut Vec<syn::ImplItem>) -> syn::Resu
3333
Ok(quote! {
3434
pyo3::inventory::submit! {
3535
#![crate = pyo3] {
36-
type TyInventory = <#ty as pyo3::class::methods::PyMethodsInventoryDispatch>::InventoryType;
36+
type TyInventory = <#ty as pyo3::class::methods::PyMethodsImpl>::Methods;
3737
<TyInventory as pyo3::class::methods::PyMethodsInventory>::new(&[#(
3838
#(#cfg_attributes)*
3939
#methods

src/class/methods.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,10 @@ impl PySetterDef {
115115
}
116116
}
117117

118-
#[doc(hidden)] // Only to be used through the proc macros, use PyMethodsProtocol in custom code
119-
/// This trait is implemented for all pyclass so to implement the [PyMethodsProtocol]
120-
/// through inventory
121-
pub trait PyMethodsInventoryDispatch {
122-
/// This allows us to get the inventory type when only the pyclass is in scope
123-
type InventoryType: PyMethodsInventory;
124-
}
125-
126-
#[doc(hidden)] // Only to be used through the proc macros, use PyMethodsProtocol in custom code
127-
/// Allows arbitrary pymethod blocks to submit their methods, which are eventually collected by pyclass
118+
/// Implementation detail. Only to be used through the proc macros.
119+
/// Allows arbitrary pymethod blocks to submit their methods, which are eventually
120+
/// collected by pyclass.
121+
#[doc(hidden)]
128122
pub trait PyMethodsInventory: inventory::Collect {
129123
/// Create a new instance
130124
fn new(methods: &'static [PyMethodDefType]) -> Self;
@@ -133,20 +127,16 @@ pub trait PyMethodsInventory: inventory::Collect {
133127
fn get_methods(&self) -> &'static [PyMethodDefType];
134128
}
135129

136-
/// The implementation of this trait defines which methods a Python type has.
137-
///
138-
/// For pyclass derived structs this is implemented by collecting all impl blocks through inventory
139-
pub trait PyMethodsProtocol {
140-
/// Returns all methods that are defined for a class
141-
fn py_methods() -> Vec<&'static PyMethodDefType>;
142-
}
130+
/// Implementation detail. Only to be used through the proc macros.
131+
/// For pyclass derived structs, this trait collects method from all impl blocks using inventory.
132+
#[doc(hidden)]
133+
pub trait PyMethodsImpl {
134+
/// Normal methods. Mainly defined by `#[pymethod]`.
135+
type Methods: PyMethodsInventory;
143136

144-
impl<T> PyMethodsProtocol for T
145-
where
146-
T: PyMethodsInventoryDispatch,
147-
{
137+
/// Returns all methods that are defined for a class.
148138
fn py_methods() -> Vec<&'static PyMethodDefType> {
149-
inventory::iter::<T::InventoryType>
139+
inventory::iter::<Self::Methods>
150140
.into_iter()
151141
.flat_map(PyMethodsInventory::get_methods)
152142
.collect()

src/pyclass.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! `PyClass` trait
2-
use crate::class::methods::{PyMethodDefType, PyMethodsProtocol};
2+
use crate::class::methods::{PyMethodDefType, PyMethodsImpl};
33
use crate::pyclass_slots::{PyClassDict, PyClassWeakRef};
44
use crate::type_object::{type_flags, PyLayout};
55
use crate::{class, ffi, PyCell, PyErr, PyNativeType, PyResult, PyTypeInfo, Python};
@@ -71,7 +71,7 @@ pub unsafe fn tp_free_fallback(obj: *mut ffi::PyObject) {
7171
/// The `#[pyclass]` attribute automatically implements this trait for your Rust struct,
7272
/// so you don't have to use this trait directly.
7373
pub trait PyClass:
74-
PyTypeInfo<Layout = PyCell<Self>> + Sized + PyClassAlloc + PyMethodsProtocol
74+
PyTypeInfo<Layout = PyCell<Self>> + Sized + PyClassAlloc + PyMethodsImpl
7575
{
7676
/// Specify this class has `#[pyclass(dict)]` or not.
7777
type Dict: PyClassDict;
@@ -215,7 +215,7 @@ fn py_class_flags<T: PyTypeInfo>(type_object: &mut ffi::PyTypeObject) {
215215
}
216216
}
217217

218-
fn py_class_method_defs<T: PyMethodsProtocol>() -> (
218+
fn py_class_method_defs<T: PyMethodsImpl>() -> (
219219
Option<ffi::newfunc>,
220220
Option<ffi::PyCFunctionWithKeywords>,
221221
Vec<ffi::PyMethodDef>,
@@ -274,7 +274,7 @@ fn py_class_async_methods<T>(defs: &mut Vec<ffi::PyMethodDef>) {
274274
}
275275
}
276276

277-
fn py_class_properties<T: PyMethodsProtocol>() -> Vec<ffi::PyGetSetDef> {
277+
fn py_class_properties<T: PyMethodsImpl>() -> Vec<ffi::PyGetSetDef> {
278278
let mut defs = std::collections::HashMap::new();
279279

280280
for def in T::py_methods() {

0 commit comments

Comments
 (0)