@@ -5,7 +5,7 @@ use crate::attributes::{
5
5
} ;
6
6
use crate :: deprecations:: Deprecations ;
7
7
use crate :: konst:: { ConstAttributes , ConstSpec } ;
8
- use crate :: pyimpl:: { gen_default_slot_impls , gen_py_const, PyClassMethodsType } ;
8
+ use crate :: pyimpl:: { gen_default_items , gen_py_const, PyClassMethodsType } ;
9
9
use crate :: pymethod:: { impl_py_getter_def, impl_py_setter_def, PropertyType } ;
10
10
use crate :: utils:: { self , get_pyo3_crate, unwrap_group, PythonDoc } ;
11
11
use proc_macro2:: { Span , TokenStream } ;
@@ -477,7 +477,7 @@ fn impl_enum_class(
477
477
}
478
478
} ;
479
479
480
- let default_impls = gen_default_slot_impls ( cls, vec ! [ default_repr_impl] ) ;
480
+ let default_impls = gen_default_items ( cls, vec ! [ default_repr_impl] ) ;
481
481
Ok ( quote ! {
482
482
const _: ( ) = {
483
483
use #krate as _pyo3;
@@ -511,12 +511,15 @@ fn unit_variants_as_descriptors<'a>(
511
511
. map ( |var| gen_py_const ( & cls_type, & variant_to_attribute ( var) ) ) ;
512
512
513
513
quote ! {
514
- impl _pyo3:: impl_:: pyclass:: PyClassDescriptors <#cls>
514
+ impl _pyo3:: impl_:: pyclass:: PyClassIntrinsicItems <#cls>
515
515
for _pyo3:: impl_:: pyclass:: PyClassImplCollector <#cls>
516
516
{
517
- fn py_class_descriptors( self ) -> & ' static [ _pyo3:: impl_:: pymethods:: PyMethodDefType ] {
518
- static METHODS : & [ _pyo3:: impl_:: pymethods:: PyMethodDefType ] = & [ #( #py_methods) , * ] ;
519
- METHODS
517
+ fn pyclass_intrinsic_items( self ) -> & ' static _pyo3:: impl_:: pyclass:: PyClassItems {
518
+ static ITEMS : _pyo3:: impl_:: pyclass:: PyClassItems = _pyo3:: impl_:: pyclass:: PyClassItems {
519
+ methods: & [ #( #py_methods) , * ] ,
520
+ slots: & [ ]
521
+ } ;
522
+ & ITEMS
520
523
}
521
524
}
522
525
}
@@ -574,12 +577,15 @@ fn impl_descriptors(
574
577
. collect :: < syn:: Result < _ > > ( ) ?;
575
578
576
579
Ok ( quote ! {
577
- impl _pyo3:: impl_:: pyclass:: PyClassDescriptors <#cls>
580
+ impl _pyo3:: impl_:: pyclass:: PyClassIntrinsicItems <#cls>
578
581
for _pyo3:: impl_:: pyclass:: PyClassImplCollector <#cls>
579
582
{
580
- fn py_class_descriptors( self ) -> & ' static [ _pyo3:: impl_:: pymethods:: PyMethodDefType ] {
581
- static METHODS : & [ _pyo3:: impl_:: pymethods:: PyMethodDefType ] = & [ #( #py_methods) , * ] ;
582
- METHODS
583
+ fn pyclass_intrinsic_items( self ) -> & ' static _pyo3:: impl_:: pyclass:: PyClassItems {
584
+ static ITEMS : _pyo3:: impl_:: pyclass:: PyClassItems = _pyo3:: impl_:: pyclass:: PyClassItems {
585
+ methods: & [ #( #py_methods) , * ] ,
586
+ slots: & [ ]
587
+ } ;
588
+ & ITEMS
583
589
}
584
590
}
585
591
} )
@@ -758,15 +764,10 @@ impl<'a> PyClassImplsBuilder<'a> {
758
764
quote ! { _pyo3:: impl_:: pyclass:: ThreadCheckerStub <#cls> }
759
765
} ;
760
766
761
- let ( for_each_py_method, methods_protos, inventory, inventory_class) = match self
762
- . methods_type
763
- {
764
- PyClassMethodsType :: Specialization => (
765
- quote ! { visitor( collector. py_methods( ) ) ; } ,
766
- quote ! { visitor( collector. methods_protocol_slots( ) ) ; } ,
767
- None ,
768
- None ,
769
- ) ,
767
+ let ( pymethods_items, inventory, inventory_class) = match self . methods_type {
768
+ PyClassMethodsType :: Specialization => {
769
+ ( quote ! { visitor( collector. py_methods( ) ) ; } , None , None )
770
+ }
770
771
PyClassMethodsType :: Inventory => {
771
772
// To allow multiple #[pymethods] block, we define inventory types.
772
773
let inventory_class_name = syn:: Ident :: new (
@@ -776,12 +777,7 @@ impl<'a> PyClassImplsBuilder<'a> {
776
777
(
777
778
quote ! {
778
779
for inventory in _pyo3:: inventory:: iter:: <<Self as _pyo3:: impl_:: pyclass:: PyClassImpl >:: Inventory >( ) {
779
- visitor( _pyo3:: impl_:: pyclass:: PyClassInventory :: methods( inventory) ) ;
780
- }
781
- } ,
782
- quote ! {
783
- for inventory in _pyo3:: inventory:: iter:: <<Self as _pyo3:: impl_:: pyclass:: PyClassImpl >:: Inventory >( ) {
784
- visitor( _pyo3:: impl_:: pyclass:: PyClassInventory :: slots( inventory) ) ;
780
+ visitor( _pyo3:: impl_:: pyclass:: PyClassInventory :: items( inventory) ) ;
785
781
}
786
782
} ,
787
783
Some ( quote ! { type Inventory = #inventory_class_name; } ) ,
@@ -790,29 +786,17 @@ impl<'a> PyClassImplsBuilder<'a> {
790
786
}
791
787
} ;
792
788
793
- let pyproto_method_visitors = if cfg ! ( feature = "pyproto" ) {
789
+ let pyproto_items = if cfg ! ( feature = "pyproto" ) {
794
790
Some ( quote ! {
795
- visitor( collector. object_protocol_methods( ) ) ;
796
- visitor( collector. async_protocol_methods( ) ) ;
797
- visitor( collector. descr_protocol_methods( ) ) ;
798
- visitor( collector. mapping_protocol_methods( ) ) ;
799
- visitor( collector. number_protocol_methods( ) ) ;
800
- } )
801
- } else {
802
- None
803
- } ;
804
-
805
- let pyproto_proto_visitors = if cfg ! ( feature = "pyproto" ) {
806
- Some ( quote ! {
807
- visitor( collector. object_protocol_slots( ) ) ;
808
- visitor( collector. number_protocol_slots( ) ) ;
809
- visitor( collector. iter_protocol_slots( ) ) ;
810
- visitor( collector. gc_protocol_slots( ) ) ;
811
- visitor( collector. descr_protocol_slots( ) ) ;
812
- visitor( collector. mapping_protocol_slots( ) ) ;
813
- visitor( collector. sequence_protocol_slots( ) ) ;
814
- visitor( collector. async_protocol_slots( ) ) ;
815
- visitor( collector. buffer_protocol_slots( ) ) ;
791
+ visitor( collector. object_protocol_items( ) ) ;
792
+ visitor( collector. number_protocol_items( ) ) ;
793
+ visitor( collector. iter_protocol_items( ) ) ;
794
+ visitor( collector. gc_protocol_items( ) ) ;
795
+ visitor( collector. descr_protocol_items( ) ) ;
796
+ visitor( collector. mapping_protocol_items( ) ) ;
797
+ visitor( collector. sequence_protocol_items( ) ) ;
798
+ visitor( collector. async_protocol_items( ) ) ;
799
+ visitor( collector. buffer_protocol_items( ) ) ;
816
800
} )
817
801
} else {
818
802
None
@@ -830,12 +814,15 @@ impl<'a> PyClassImplsBuilder<'a> {
830
814
type ThreadChecker = #thread_checker;
831
815
#inventory
832
816
833
- fn for_each_method_def ( visitor: & mut dyn :: std:: ops:: FnMut ( & [ _pyo3:: class :: PyMethodDefType ] ) ) {
817
+ fn for_all_items ( visitor: & mut dyn :: std:: ops:: FnMut ( & _pyo3:: impl_ :: pyclass :: PyClassItems ) ) {
834
818
use _pyo3:: impl_:: pyclass:: * ;
835
819
let collector = PyClassImplCollector :: <Self >:: new( ) ;
836
- #for_each_py_method;
837
- visitor( collector. py_class_descriptors( ) ) ;
838
- #pyproto_method_visitors
820
+ visitor( collector. pyclass_intrinsic_items( ) ) ;
821
+ // This depends on Python implementation detail;
822
+ // an old slot entry will be overriden by newer ones.
823
+ visitor( collector. pyclass_default_items( ) ) ;
824
+ #pymethods_items
825
+ #pyproto_items
839
826
}
840
827
fn get_new( ) -> :: std:: option:: Option <_pyo3:: ffi:: newfunc> {
841
828
use _pyo3:: impl_:: pyclass:: * ;
@@ -853,17 +840,6 @@ impl<'a> PyClassImplsBuilder<'a> {
853
840
collector. free_impl( )
854
841
}
855
842
856
- fn for_each_proto_slot( visitor: & mut dyn :: std:: ops:: FnMut ( & [ _pyo3:: ffi:: PyType_Slot ] ) ) {
857
- // Implementation which uses dtolnay specialization to load all slots.
858
- use _pyo3:: impl_:: pyclass:: * ;
859
- let collector = PyClassImplCollector :: <Self >:: new( ) ;
860
- // This depends on Python implementation detail;
861
- // an old slot entry will be overriden by newer ones.
862
- visitor( collector. py_class_default_slots( ) ) ;
863
- #pyproto_proto_visitors
864
- #methods_protos
865
- }
866
-
867
843
#dict_offset
868
844
869
845
#weaklist_offset
@@ -933,31 +909,20 @@ fn define_inventory_class(inventory_class_name: &syn::Ident) -> TokenStream {
933
909
quote ! {
934
910
#[ doc( hidden) ]
935
911
pub struct #inventory_class_name {
936
- methods: & ' static [ _pyo3:: class:: PyMethodDefType ] ,
937
- slots: & ' static [ _pyo3:: ffi:: PyType_Slot ] ,
912
+ items: _pyo3:: impl_:: pyclass:: PyClassItems ,
938
913
}
939
914
impl #inventory_class_name {
940
- const fn new(
941
- methods: & ' static [ _pyo3:: class:: PyMethodDefType ] ,
942
- slots: & ' static [ _pyo3:: ffi:: PyType_Slot ] ,
943
- ) -> Self {
944
- Self { methods, slots }
915
+ const fn new( items: _pyo3:: impl_:: pyclass:: PyClassItems ) -> Self {
916
+ Self { items }
945
917
}
946
918
}
947
919
948
920
impl _pyo3:: impl_:: pyclass:: PyClassInventory for #inventory_class_name {
949
- fn methods( & ' static self ) -> & ' static [ _pyo3:: class:: PyMethodDefType ] {
950
- self . methods
951
- }
952
- fn slots( & ' static self ) -> & ' static [ _pyo3:: ffi:: PyType_Slot ] {
953
- self . slots
921
+ fn items( & self ) -> & _pyo3:: impl_:: pyclass:: PyClassItems {
922
+ & self . items
954
923
}
955
924
}
956
925
957
- // inventory requires these bounds
958
- unsafe impl :: std:: marker:: Send for #inventory_class_name { }
959
- unsafe impl :: std:: marker:: Sync for #inventory_class_name { }
960
-
961
926
_pyo3:: inventory:: collect!( #inventory_class_name) ;
962
927
}
963
928
}
0 commit comments