Skip to content

Commit

Permalink
Merge pull request #951 from PyO3/pymethod-refactor
Browse files Browse the repository at this point in the history
Rename PyMethodsImpl -> PyMethods
  • Loading branch information
kngwyu authored Jun 3, 2020
2 parents 4355360 + 69efb09 commit 75b2b62
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
25 changes: 13 additions & 12 deletions src/class/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<T: HasMethodsInventory> PyMethodsImpl for T {
impl<T: HasMethodsInventory> PyMethods for T {
fn py_methods() -> Vec<&'static PyMethodDefType> {
inventory::iter::<T::Methods>
.into_iter()
Expand Down
10 changes: 4 additions & 6 deletions src/pyclass.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<Layout = PyCell<Self>> + Sized + PyClassAlloc + PyMethodsImpl
{
pub trait PyClass: PyTypeInfo<Layout = PyCell<Self>> + Sized + PyClassAlloc + PyMethods {
/// Specify this class has `#[pyclass(dict)]` or not.
type Dict: PyClassDict;
/// Specify this class has `#[pyclass(weakref)]` or not.
Expand Down Expand Up @@ -227,7 +225,7 @@ fn py_class_flags<T: PyTypeInfo>(type_object: &mut ffi::PyTypeObject) {
}
}

fn py_class_method_defs<T: PyMethodsImpl>() -> (
fn py_class_method_defs<T: PyMethods>() -> (
Option<ffi::newfunc>,
Option<ffi::PyCFunctionWithKeywords>,
Vec<ffi::PyMethodDef>,
Expand Down Expand Up @@ -267,7 +265,7 @@ fn py_class_method_defs<T: PyMethodsImpl>() -> (
(new, call, defs, attrs)
}

fn py_class_properties<T: PyMethodsImpl>() -> Vec<ffi::PyGetSetDef> {
fn py_class_properties<T: PyMethods>() -> Vec<ffi::PyGetSetDef> {
let mut defs = std::collections::HashMap::new();

for def in T::py_methods() {
Expand Down

0 comments on commit 75b2b62

Please sign in to comment.