From 69efb0980f575341611247ab1e7569ca79ac59a3 Mon Sep 17 00:00:00 2001 From: kngwyu Date: Wed, 3 Jun 2020 13:01:37 +0900 Subject: [PATCH] Rename PyMethodsImpl -> PyMethods --- src/class/methods.rs | 25 +++++++++++++------------ src/pyclass.rs | 10 ++++------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/class/methods.rs b/src/class/methods.rs index 508db93222a..a34260d6c5c 100644 --- a/src/class/methods.rs +++ b/src/class/methods.rs @@ -134,7 +134,16 @@ impl PySetterDef { } } -/// Implementation detail. Only to be used through the proc macros. +/// Indicates that the type `T` has some Python methods. +pub trait PyMethods { + /// Returns all methods that are defined for a class. + fn py_methods() -> Vec<&'static PyMethodDefType> { + Vec::new() + } +} + +/// Implementation detail. Only to be used through our proc macro code. +/// Method storage for `#[pyclass]`. /// Allows arbitrary `#[pymethod]/#[pyproto]` blocks to submit their methods, /// which are eventually collected by `#[pyclass]`. #[doc(hidden)] @@ -147,24 +156,16 @@ pub trait PyMethodsInventory: inventory::Collect { fn get(&self) -> &'static [PyMethodDefType]; } +/// Implemented for `#[pyclass]` in our proc macro code. +/// Indicates that the pyclass has its own method storage. #[doc(hidden)] #[cfg(feature = "macros")] 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> { - Vec::new() - } -} - #[cfg(feature = "macros")] -impl PyMethodsImpl for T { +impl PyMethods for T { fn py_methods() -> Vec<&'static PyMethodDefType> { inventory::iter:: .into_iter() diff --git a/src/pyclass.rs b/src/pyclass.rs index 665e8c980d0..5e0b05e3ae8 100644 --- a/src/pyclass.rs +++ b/src/pyclass.rs @@ -1,5 +1,5 @@ //! `PyClass` trait -use crate::class::methods::{PyClassAttributeDef, PyMethodDefType, PyMethodsImpl}; +use crate::class::methods::{PyClassAttributeDef, PyMethodDefType, PyMethods}; use crate::conversion::{IntoPyPointer, ToPyObject}; use crate::pyclass_slots::{PyClassDict, PyClassWeakRef}; use crate::type_object::{type_flags, PyLayout}; @@ -72,9 +72,7 @@ pub unsafe fn tp_free_fallback(obj: *mut ffi::PyObject) { /// /// The `#[pyclass]` attribute automatically implements this trait for your Rust struct, /// so you don't have to use this trait directly. -pub trait PyClass: - PyTypeInfo> + Sized + PyClassAlloc + PyMethodsImpl -{ +pub trait PyClass: PyTypeInfo> + Sized + PyClassAlloc + PyMethods { /// Specify this class has `#[pyclass(dict)]` or not. type Dict: PyClassDict; /// Specify this class has `#[pyclass(weakref)]` or not. @@ -227,7 +225,7 @@ fn py_class_flags(type_object: &mut ffi::PyTypeObject) { } } -fn py_class_method_defs() -> ( +fn py_class_method_defs() -> ( Option, Option, Vec, @@ -267,7 +265,7 @@ fn py_class_method_defs() -> ( (new, call, defs, attrs) } -fn py_class_properties() -> Vec { +fn py_class_properties() -> Vec { let mut defs = std::collections::HashMap::new(); for def in T::py_methods() {