Skip to content

Commit 2416311

Browse files
committed
move Layout<T> GAT from PyTypeInfo to PyClassBaseType
1 parent d137d52 commit 2416311

11 files changed

Lines changed: 16 additions & 43 deletions

File tree

guide/src/class.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,6 @@ unsafe impl pyo3::type_object::PyTypeInfo for MyClass {
15021502
const NAME: &'static str = "MyClass";
15031503
const MODULE: ::std::option::Option<&'static str> = ::std::option::Option::None;
15041504

1505-
type Layout<T: pyo3::impl_::pyclass::PyClassImpl> = pyo3::impl_::pycell::PyStaticClassObject<T>;
1506-
15071505
#[inline]
15081506
fn type_object_raw(py: pyo3::Python<'_>) -> *mut pyo3::ffi::PyTypeObject {
15091507
<Self as pyo3::impl_::pyclass::PyClassImpl>::lazy_type_object()
@@ -1528,7 +1526,7 @@ impl pyo3::impl_::pyclass::PyClassImpl for MyClass {
15281526
const IS_SUBCLASS: bool = false;
15291527
const IS_MAPPING: bool = false;
15301528
const IS_SEQUENCE: bool = false;
1531-
type Layout = <MyClass as pyo3::type_object::PyTypeInfo>::Layout<MyClass>;
1529+
type Layout = <Self::BaseNativeType as pyo3::impl_::pyclass::PyClassBaseType>::Layout<Self>;
15321530
type BaseType = PyAny;
15331531
type ThreadChecker = pyo3::impl_::pyclass::SendablePyClass<MyClass>;
15341532
type PyClassMutability = <<pyo3::PyAny as pyo3::impl_::pyclass::PyClassBaseType>::PyClassMutability as pyo3::impl_::pycell::PyClassMutability>::MutableChild;

pyo3-macros-backend/src/pyclass.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,8 +2016,6 @@ fn impl_pytypeinfo(cls: &syn::Ident, attr: &PyClassArgs, ctx: &Ctx) -> TokenStre
20162016

20172017
#type_hint
20182018

2019-
type Layout<T: #pyo3_path::impl_::pyclass::PyClassImpl> = <<#cls as #pyo3_path::impl_::pyclass::PyClassImpl>::BaseNativeType as #pyo3_path::type_object::PyTypeInfo>::Layout<T>;
2020-
20212019
#[inline]
20222020
fn type_object_raw(py: #pyo3_path::Python<'_>) -> *mut #pyo3_path::ffi::PyTypeObject {
20232021
use #pyo3_path::prelude::PyTypeMethods;
@@ -2544,6 +2542,7 @@ impl<'a> PyClassImplsBuilder<'a> {
25442542
type BaseNativeType = <Self as #pyo3_path::impl_::pyclass::PyClassImpl>::BaseNativeType;
25452543
type Initializer = #pyo3_path::pyclass_init::PyClassInitializer<Self>;
25462544
type PyClassMutability = <Self as #pyo3_path::impl_::pyclass::PyClassImpl>::PyClassMutability;
2545+
type Layout<T: #pyo3_path::impl_::pyclass::PyClassImpl> = <Self::BaseNativeType as #pyo3_path::impl_::pyclass::PyClassBaseType>::Layout<T>;
25472546
}
25482547
}
25492548
});
@@ -2614,7 +2613,7 @@ impl<'a> PyClassImplsBuilder<'a> {
26142613
const IS_SEQUENCE: bool = #is_sequence;
26152614
const IS_IMMUTABLE_TYPE: bool = #is_immutable_type;
26162615

2617-
type Layout = <#cls as #pyo3_path::PyTypeInfo>::Layout<Self>;
2616+
type Layout = <Self::BaseNativeType as #pyo3_path::impl_::pyclass::PyClassBaseType>::Layout<Self>;
26182617
type BaseType = #base;
26192618
type ThreadChecker = #thread_checker;
26202619
#inventory

src/impl_/pyclass.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,8 @@ pub trait PyClassBaseType: Sized {
11181118
type BaseNativeType;
11191119
type Initializer: PyObjectInit<Self>;
11201120
type PyClassMutability: PyClassMutability;
1121+
/// The type of object layout to use for ancestors or descendants of this type.
1122+
type Layout<T: PyClassImpl>: PyClassObjectLayout<T>;
11211123
}
11221124

11231125
/// Implementation of tp_dealloc for pyclasses without gc

src/type_object.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
//! Python type object information
22
33
use crate::ffi_ptr_ext::FfiPtrExt;
4-
use crate::impl_::pyclass::PyClassImpl;
54
#[cfg(feature = "experimental-inspect")]
65
use crate::inspect::TypeHint;
7-
use crate::pycell::impl_::PyClassObjectLayout;
86
use crate::types::{PyAny, PyType};
97
use crate::{ffi, Bound, Python};
108
use std::ptr;
@@ -62,9 +60,6 @@ pub unsafe trait PyTypeInfo: Sized {
6260
#[cfg(feature = "experimental-inspect")]
6361
const TYPE_HINT: TypeHint = TypeHint::module_attr("typing", "Any");
6462

65-
/// The type of object layout to use for ancestors or descendants of this type.
66-
type Layout<T: PyClassImpl>: PyClassObjectLayout<T>;
67-
6863
/// Returns the PyTypeObject instance for this type.
6964
fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject;
7065

src/types/any.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pyobject_native_type_info!(
4747
"typing",
4848
"Any",
4949
Some("builtins"),
50-
PyStaticClassObject<T>,
5150
#checkfunction=PyObject_Check
5251
);
5352

@@ -58,6 +57,7 @@ impl crate::impl_::pyclass::PyClassBaseType for PyAny {
5857
type BaseNativeType = PyAny;
5958
type Initializer = crate::impl_::pyclass_init::PyNativeTypeInitializer<Self>;
6059
type PyClassMutability = crate::pycell::impl_::ImmutableClass;
60+
type Layout<T: crate::impl_::pyclass::PyClassImpl> = PyStaticClassObject<T>;
6161
}
6262

6363
/// This trait represents the Python APIs which are usable on all Python objects.

src/types/ellipsis.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
#[cfg(feature = "experimental-inspect")]
22
use crate::inspect::TypeHint;
33
use crate::{
4-
ffi,
5-
ffi_ptr_ext::FfiPtrExt,
6-
impl_::{pycell::PyStaticClassObject, pyclass::PyClassImpl},
7-
types::any::PyAnyMethods,
8-
Borrowed, Bound, PyAny, PyTypeInfo, Python,
4+
ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
5+
Python,
96
};
107

118
/// Represents the Python `Ellipsis` object.
@@ -37,8 +34,6 @@ unsafe impl PyTypeInfo for PyEllipsis {
3734
#[cfg(feature = "experimental-inspect")]
3835
const TYPE_HINT: TypeHint = TypeHint::module_attr("types", "EllipsisType");
3936

40-
type Layout<T: PyClassImpl> = PyStaticClassObject<T>;
41-
4237
fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
4338
unsafe { ffi::Py_TYPE(ffi::Py_Ellipsis()) }
4439
}

src/types/mapping.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ unsafe impl PyTypeInfo for PyMapping {
3030
#[cfg(feature = "experimental-inspect")]
3131
const TYPE_HINT: TypeHint = TypeHint::module_attr("collections.abc", "Mapping");
3232

33-
type Layout<T: crate::impl_::pyclass::PyClassImpl> =
34-
crate::impl_::pycell::PyStaticClassObject<T>;
35-
3633
#[inline]
3734
#[allow(clippy::redundant_closure_call)]
3835
fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject {

src/types/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,13 @@ macro_rules! pyobject_type_info_type_hint(
166166
#[doc(hidden)]
167167
#[macro_export]
168168
macro_rules! pyobject_native_type_info(
169-
($name:ty, $typeobject:expr, $type_hint_module:expr, $type_hint_name:expr, $module:expr, $layout:path $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
169+
($name:ty, $typeobject:expr, $type_hint_module:expr, $type_hint_name:expr, $module:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
170170
// SAFETY: macro caller has upheld the safety contracts
171171
unsafe impl<$($generics,)*> $crate::type_object::PyTypeInfo for $name {
172172
const NAME: &'static str = stringify!($name);
173173
const MODULE: ::std::option::Option<&'static str> = $module;
174174
$crate::pyobject_type_info_type_hint!($type_hint_module, $type_hint_name);
175175

176-
type Layout<T: $crate::impl_::pyclass::PyClassImpl> = $layout;
177-
178176
#[inline]
179177
#[allow(clippy::redundant_closure_call)]
180178
fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
@@ -206,15 +204,15 @@ macro_rules! pyobject_native_type_info(
206204
#[doc(hidden)]
207205
#[macro_export]
208206
macro_rules! pyobject_native_type_core {
209-
($name:ty, $typeobject:expr, $type_hint_module:expr, $type_hint_name:expr, #module=$module:expr, #layout=$layout:path $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
207+
($name:ty, $typeobject:expr, $type_hint_module:expr, $type_hint_name:expr, #module=$module:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
210208
$crate::pyobject_native_type_named!($name $(;$generics)*);
211-
$crate::pyobject_native_type_info!($name, $typeobject, $type_hint_module, $type_hint_name, $module, $layout $(, #checkfunction=$checkfunction)? $(;$generics)*);
209+
$crate::pyobject_native_type_info!($name, $typeobject, $type_hint_module, $type_hint_name, $module $(, #checkfunction=$checkfunction)? $(;$generics)*);
212210
};
213211
($name:ty, $typeobject:expr, $type_hint_module:expr, $type_hint_name:expr, #module=$module:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
214-
$crate::pyobject_native_type_core!($name, $typeobject, $type_hint_module, $type_hint_name, #module=$module, #layout=$crate::impl_::pycell::PyStaticClassObject<T> $(, #checkfunction=$checkfunction)? $(;$generics)*);
212+
$crate::pyobject_native_type_core!($name, $typeobject, $type_hint_module, $type_hint_name, #module=$module $(, #checkfunction=$checkfunction)? $(;$generics)*);
215213
};
216214
($name:ty, $typeobject:expr, $type_hint_module:expr, $type_hint_name:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
217-
$crate::pyobject_native_type_core!($name, $typeobject, $type_hint_module, $type_hint_name, #module=::std::option::Option::Some("builtins"), #layout=$crate::impl_::pycell::PyStaticClassObject<T> $(, #checkfunction=$checkfunction)? $(;$generics)*);
215+
$crate::pyobject_native_type_core!($name, $typeobject, $type_hint_module, $type_hint_name, #module=::std::option::Option::Some("builtins") $(, #checkfunction=$checkfunction)? $(;$generics)*);
218216
};
219217
}
220218

@@ -228,6 +226,7 @@ macro_rules! pyobject_subclassable_native_type {
228226
type BaseNativeType = $name;
229227
type Initializer = $crate::impl_::pyclass_init::PyNativeTypeInitializer<Self>;
230228
type PyClassMutability = $crate::pycell::impl_::ImmutableClass;
229+
type Layout<T: $crate::impl_::pyclass::PyClassImpl> = $crate::impl_::pycell::PyStaticClassObject<T>;
231230
}
232231
}
233232
}

src/types/none.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::ffi_ptr_ext::FfiPtrExt;
2-
use crate::impl_::pycell::PyStaticClassObject;
3-
use crate::impl_::pyclass::PyClassImpl;
42
#[cfg(feature = "experimental-inspect")]
53
use crate::inspect::TypeHint;
64
use crate::{ffi, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo, Python};
@@ -34,8 +32,6 @@ unsafe impl PyTypeInfo for PyNone {
3432
#[cfg(feature = "experimental-inspect")]
3533
const TYPE_HINT: TypeHint = TypeHint::builtin("None");
3634

37-
type Layout<T: PyClassImpl> = PyStaticClassObject<T>;
38-
3935
fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
4036
unsafe { ffi::Py_TYPE(ffi::Py_None()) }
4137
}

src/types/notimplemented.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
#[cfg(feature = "experimental-inspect")]
22
use crate::inspect::TypeHint;
33
use crate::{
4-
ffi,
5-
ffi_ptr_ext::FfiPtrExt,
6-
impl_::{pycell::PyStaticClassObject, pyclass::PyClassImpl},
7-
types::any::PyAnyMethods,
8-
Borrowed, Bound, PyAny, PyTypeInfo, Python,
4+
ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
5+
Python,
96
};
107

118
/// Represents the Python `NotImplemented` object.
@@ -37,8 +34,6 @@ unsafe impl PyTypeInfo for PyNotImplemented {
3734
#[cfg(feature = "experimental-inspect")]
3835
const TYPE_HINT: TypeHint = TypeHint::module_attr("types", "NotImplementedType");
3936

40-
type Layout<T: PyClassImpl> = PyStaticClassObject<T>;
41-
4237
fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
4338
unsafe { ffi::Py_TYPE(ffi::Py_NotImplemented()) }
4439
}

0 commit comments

Comments
 (0)