@@ -140,7 +140,7 @@ impl Default for PyTypeFlags {
140140}
141141
142142pub ( 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 ;
144144pub ( crate ) type HashFunc = fn ( & PyObject , & VirtualMachine ) -> PyResult < PyHash > ;
145145// CallFunc = GenericMethod
146146pub ( crate ) type GetattroFunc = fn ( & PyObject , PyStrRef , & VirtualMachine ) -> PyResult ;
@@ -192,14 +192,14 @@ fn length_wrapper(obj: &PyObject, vm: &VirtualMachine) -> PyResult<usize> {
192192 Ok ( len as usize )
193193}
194194
195- const fn new_as_mapping_wrapper (
195+ const fn new_as_mapping_generic (
196196 has_length : bool ,
197197 has_subscript : bool ,
198198 has_ass_subscript : bool ,
199199) -> PyMappingMethods {
200200 PyMappingMethods {
201201 length : if has_length {
202- Some ( |mapping, vm| length_wrapper ( & mapping. obj , vm) )
202+ Some ( |mapping, vm| length_wrapper ( mapping. obj , vm) )
203203 } else {
204204 None
205205 } ,
@@ -237,34 +237,44 @@ const fn new_as_mapping_wrapper(
237237 }
238238}
239239
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 {
251245 const fn bool_int ( v : bool ) -> usize {
252246 if v {
253247 1
254248 } else {
255249 0
256250 }
257251 }
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 {
258271 let ( has_length, has_subscript, has_ass_subscript) = (
259272 zelf. class ( ) . has_attr ( identifier ! ( vm, __len__) ) ,
260273 zelf. class ( ) . has_attr ( identifier ! ( vm, __getitem__) ) ,
261274 zelf. class ( ) . has_attr ( identifier ! ( vm, __setitem__) )
262275 | zelf. class ( ) . has_attr ( identifier ! ( vm, __delitem__) ) ,
263276 ) ;
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)
268278}
269279
270280fn 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
275285 Cow :: Owned ( PySequenceMethods {
276286 length : then_some_closure ! (
277287 zelf. class( ) . has_attr( identifier!( vm, __len__) ) ,
278- |seq, vm| { length_wrapper( & seq. obj, vm) }
288+ |seq, vm| { length_wrapper( seq. obj, vm) }
279289 ) ,
280290 item : Some ( |seq, i, vm| {
281291 vm. call_special_method (
@@ -420,7 +430,7 @@ impl PyType {
420430 }
421431 match name. as_str ( ) {
422432 "__len__" | "__getitem__" | "__setitem__" | "__delitem__" => {
423- update_slot ! ( as_mapping, as_mapping_wrapper ) ;
433+ update_slot ! ( as_mapping, as_mapping_generic ) ;
424434 update_slot ! ( as_sequence, as_sequence_wrapper) ;
425435 }
426436 "__hash__" => {
@@ -936,10 +946,11 @@ pub trait AsMapping: PyPayload {
936946
937947 #[ inline]
938948 #[ 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
941951 }
942952
953+ #[ inline]
943954 fn mapping_downcast < ' a > ( mapping : & ' a PyMapping ) -> & ' a Py < Self > {
944955 unsafe { mapping. obj . downcast_unchecked_ref ( ) }
945956 }
0 commit comments