From 9f8b7cd9bf2612ae325b68c8b618079dde715153 Mon Sep 17 00:00:00 2001 From: kngwyu Date: Tue, 12 May 2020 00:05:36 +0900 Subject: [PATCH] Revert #889 --- guide/src/class.md | 2 +- pyo3-derive-backend/src/pyclass.rs | 6 +++--- pyo3-derive-backend/src/pyimpl.rs | 4 ++-- pyo3-derive-backend/src/pyproto.rs | 4 ++-- src/class/methods.rs | 25 +++++++++++++------------ 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/guide/src/class.md b/guide/src/class.md index 460e799bcea..890b9735bf9 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -936,7 +936,7 @@ impl pyo3::class::methods::PyMethodsInventory for Pyo3MethodsInventoryForMyClass self.methods } } -impl pyo3::class::methods::PyMethodsImpl for MyClass { +impl pyo3::class::methods::HasMethodsInventory for MyClass { type Methods = Pyo3MethodsInventoryForMyClass; } pyo3::inventory::collect!(Pyo3MethodsInventoryForMyClass); diff --git a/pyo3-derive-backend/src/pyclass.rs b/pyo3-derive-backend/src/pyclass.rs index d5aad18a54b..33a45ba61c7 100644 --- a/pyo3-derive-backend/src/pyclass.rs +++ b/pyo3-derive-backend/src/pyclass.rs @@ -228,7 +228,7 @@ fn impl_methods_inventory(cls: &syn::Ident) -> TokenStream { } } - impl pyo3::class::methods::PyMethodsImpl for #cls { + impl pyo3::class::methods::HasMethodsInventory for #cls { type Methods = #inventory_cls; } @@ -455,8 +455,8 @@ fn impl_descriptors( Ok(quote! { pyo3::inventory::submit! { #![crate = pyo3] { - type ClsInventory = <#cls as pyo3::class::methods::PyMethodsImpl>::Methods; - ::new(&[#(#py_methods),*]) + type Inventory = <#cls as pyo3::class::methods::HasMethodsInventory>::Methods; + ::new(&[#(#py_methods),*]) } } }) diff --git a/pyo3-derive-backend/src/pyimpl.rs b/pyo3-derive-backend/src/pyimpl.rs index c2a68e6b0ea..3aabf47b2af 100644 --- a/pyo3-derive-backend/src/pyimpl.rs +++ b/pyo3-derive-backend/src/pyimpl.rs @@ -42,8 +42,8 @@ pub fn impl_methods(ty: &syn::Type, impls: &mut Vec) -> syn::Resu Ok(quote! { pyo3::inventory::submit! { #![crate = pyo3] { - type TyInventory = <#ty as pyo3::class::methods::PyMethodsImpl>::Methods; - ::new(&[#( + type Inventory = <#ty as pyo3::class::methods::HasMethodsInventory>::Methods; + ::new(&[#( #(#cfg_attributes)* #methods ),*]) diff --git a/pyo3-derive-backend/src/pyproto.rs b/pyo3-derive-backend/src/pyproto.rs index 78d5d6e7f9a..09459556b74 100644 --- a/pyo3-derive-backend/src/pyproto.rs +++ b/pyo3-derive-backend/src/pyproto.rs @@ -98,8 +98,8 @@ fn impl_proto_impl( let inventory_submission = quote! { pyo3::inventory::submit! { #![crate = pyo3] { - type ProtoInventory = <#ty as pyo3::class::methods::PyMethodsImpl>::Methods; - ::new(&[#(#py_methods),*]) + type Inventory = <#ty as pyo3::class::methods::HasMethodsInventory>::Methods; + ::new(&[#(#py_methods),*]) } } }; diff --git a/src/class/methods.rs b/src/class/methods.rs index 28ea68a7b5d..508db93222a 100644 --- a/src/class/methods.rs +++ b/src/class/methods.rs @@ -147,27 +147,28 @@ pub trait PyMethodsInventory: inventory::Collect { fn get(&self) -> &'static [PyMethodDefType]; } -/// Implementation detail. Only to be used through the proc macros. -/// For pyclass derived structs, this trait collects method from all impl blocks using inventory. #[doc(hidden)] #[cfg(feature = "macros")] -pub trait PyMethodsImpl { - /// Normal methods. Mainly defined by `#[pymethod]`. +pub trait HasMethodsInventory { type Methods: PyMethodsInventory; +} +/// Implementation detail. Only to be used through the proc macros. +/// For pyclass derived structs, this trait collects method from all impl blocks using inventory. +#[doc(hidden)] +pub trait PyMethodsImpl { /// Returns all methods that are defined for a class. fn py_methods() -> Vec<&'static PyMethodDefType> { - inventory::iter:: - .into_iter() - .flat_map(PyMethodsInventory::get) - .collect() + Vec::new() } } -#[doc(hidden)] -#[cfg(not(feature = "macros"))] -pub trait PyMethodsImpl { +#[cfg(feature = "macros")] +impl PyMethodsImpl for T { fn py_methods() -> Vec<&'static PyMethodDefType> { - Vec::new() + inventory::iter:: + .into_iter() + .flat_map(PyMethodsInventory::get) + .collect() } }