@@ -140,7 +140,7 @@ impl Default for PyTypeFlags {
140
140
}
141
141
142
142
pub ( crate ) type GenericMethod = fn ( & PyObject , FuncArgs , & VirtualMachine ) -> PyResult ;
143
- pub ( crate ) type AsMappingFunc = fn ( & PyObject , & VirtualMachine ) -> PyMappingMethods ;
143
+ pub ( crate ) type AsMappingFunc = fn ( & PyObject , & VirtualMachine ) -> & ' static PyMappingMethods ;
144
144
pub ( crate ) type HashFunc = fn ( & PyObject , & VirtualMachine ) -> PyResult < PyHash > ;
145
145
// CallFunc = GenericMethod
146
146
pub ( crate ) type GetattroFunc = fn ( & PyObject , PyStrRef , & VirtualMachine ) -> PyResult ;
@@ -192,14 +192,14 @@ fn length_wrapper(obj: &PyObject, vm: &VirtualMachine) -> PyResult<usize> {
192
192
Ok ( len as usize )
193
193
}
194
194
195
- const fn new_as_mapping_wrapper (
195
+ const fn new_as_mapping_generic (
196
196
has_length : bool ,
197
197
has_subscript : bool ,
198
198
has_ass_subscript : bool ,
199
199
) -> PyMappingMethods {
200
200
PyMappingMethods {
201
201
length : if has_length {
202
- Some ( |mapping, vm| length_wrapper ( & mapping. obj , vm) )
202
+ Some ( |mapping, vm| length_wrapper ( mapping. obj , vm) )
203
203
} else {
204
204
None
205
205
} ,
@@ -237,34 +237,44 @@ const fn new_as_mapping_wrapper(
237
237
}
238
238
}
239
239
240
- fn as_mapping_wrapper ( zelf : & PyObject , vm : & VirtualMachine ) -> PyMappingMethods {
241
- static MAPPING_METHODS : & [ PyMappingMethods ] = & [
242
- new_as_mapping_wrapper ( false , false , false ) ,
243
- new_as_mapping_wrapper ( true , false , false ) ,
244
- new_as_mapping_wrapper ( false , true , false ) ,
245
- new_as_mapping_wrapper ( true , true , false ) ,
246
- new_as_mapping_wrapper ( false , false , true ) ,
247
- new_as_mapping_wrapper ( true , false , true ) ,
248
- new_as_mapping_wrapper ( false , true , true ) ,
249
- new_as_mapping_wrapper ( true , true , true ) ,
250
- ] ;
240
+ pub ( crate ) fn static_as_mapping_generic (
241
+ has_length : bool ,
242
+ has_subscript : bool ,
243
+ has_ass_subscript : bool ,
244
+ ) -> & ' static PyMappingMethods {
251
245
const fn bool_int ( v : bool ) -> usize {
252
246
if v {
253
247
1
254
248
} else {
255
249
0
256
250
}
257
251
}
252
+
253
+ static MAPPING_METHODS : & [ PyMappingMethods ] = & [
254
+ new_as_mapping_generic ( false , false , false ) ,
255
+ new_as_mapping_generic ( true , false , false ) ,
256
+ new_as_mapping_generic ( false , true , false ) ,
257
+ new_as_mapping_generic ( true , true , false ) ,
258
+ new_as_mapping_generic ( false , false , true ) ,
259
+ new_as_mapping_generic ( true , false , true ) ,
260
+ new_as_mapping_generic ( false , true , true ) ,
261
+ new_as_mapping_generic ( true , true , true ) ,
262
+ ] ;
263
+
264
+ let key =
265
+ bool_int ( has_length) | ( bool_int ( has_subscript) << 1 ) | ( bool_int ( has_ass_subscript) << 2 ) ;
266
+
267
+ & MAPPING_METHODS [ key]
268
+ }
269
+
270
+ pub fn as_mapping_generic ( zelf : & PyObject , vm : & VirtualMachine ) -> & ' static PyMappingMethods {
258
271
let ( has_length, has_subscript, has_ass_subscript) = (
259
272
zelf. class ( ) . has_attr ( identifier ! ( vm, __len__) ) ,
260
273
zelf. class ( ) . has_attr ( identifier ! ( vm, __getitem__) ) ,
261
274
zelf. class ( ) . has_attr ( identifier ! ( vm, __setitem__) )
262
275
| zelf. class ( ) . has_attr ( identifier ! ( vm, __delitem__) ) ,
263
276
) ;
264
- let key = ( bool_int ( has_length) << 0 )
265
- | ( bool_int ( has_subscript) << 1 )
266
- | ( bool_int ( has_ass_subscript) << 2 ) ;
267
- MAPPING_METHODS [ key] . clone ( )
277
+ static_as_mapping_generic ( has_length, has_subscript, has_ass_subscript)
268
278
}
269
279
270
280
fn as_sequence_wrapper ( zelf : & PyObject , vm : & VirtualMachine ) -> Cow < ' static , PySequenceMethods > {
@@ -275,7 +285,7 @@ fn as_sequence_wrapper(zelf: &PyObject, vm: &VirtualMachine) -> Cow<'static, PyS
275
285
Cow :: Owned ( PySequenceMethods {
276
286
length : then_some_closure ! (
277
287
zelf. class( ) . has_attr( identifier!( vm, __len__) ) ,
278
- |seq, vm| { length_wrapper( & seq. obj, vm) }
288
+ |seq, vm| { length_wrapper( seq. obj, vm) }
279
289
) ,
280
290
item : Some ( |seq, i, vm| {
281
291
vm. call_special_method (
@@ -420,7 +430,7 @@ impl PyType {
420
430
}
421
431
match name. as_str ( ) {
422
432
"__len__" | "__getitem__" | "__setitem__" | "__delitem__" => {
423
- update_slot ! ( as_mapping, as_mapping_wrapper ) ;
433
+ update_slot ! ( as_mapping, as_mapping_generic ) ;
424
434
update_slot ! ( as_sequence, as_sequence_wrapper) ;
425
435
}
426
436
"__hash__" => {
@@ -936,10 +946,11 @@ pub trait AsMapping: PyPayload {
936
946
937
947
#[ inline]
938
948
#[ pyslot]
939
- fn as_mapping ( _zelf : & PyObject , _vm : & VirtualMachine ) -> PyMappingMethods {
940
- Self :: AS_MAPPING
949
+ fn as_mapping ( _zelf : & PyObject , _vm : & VirtualMachine ) -> & ' static PyMappingMethods {
950
+ & Self :: AS_MAPPING
941
951
}
942
952
953
+ #[ inline]
943
954
fn mapping_downcast < ' a > ( mapping : & ' a PyMapping ) -> & ' a Py < Self > {
944
955
unsafe { mapping. obj . downcast_unchecked_ref ( ) }
945
956
}
0 commit comments