From 754b28c086df9a0cb7732f594e031fac3b271f68 Mon Sep 17 00:00:00 2001 From: Discord9 Date: Thu, 24 Nov 2022 12:16:45 +0800 Subject: [PATCH] refactor: use `Lazy` for static --- stdlib/src/mmap.rs | 5 +++-- vm/src/builtins/bytes.rs | 13 +++++++------ vm/src/builtins/complex.rs | 5 +++-- vm/src/builtins/dict.rs | 17 +++++++++-------- vm/src/builtins/float.rs | 5 +++-- vm/src/builtins/genericalias.rs | 6 ++++-- vm/src/builtins/int.rs | 5 +++-- vm/src/builtins/mappingproxy.rs | 14 ++++++++------ vm/src/builtins/memory.rs | 5 +++-- vm/src/builtins/range.rs | 9 +++++---- vm/src/builtins/set.rs | 10 ++++++---- vm/src/builtins/singletons.rs | 6 ++++-- vm/src/builtins/str.rs | 9 +++++---- vm/src/builtins/tuple.rs | 10 ++++++---- vm/src/builtins/union.rs | 6 ++++-- vm/src/builtins/weakproxy.rs | 6 ++++-- vm/src/stdlib/sre.rs | 17 +++++++++-------- 17 files changed, 86 insertions(+), 62 deletions(-) diff --git a/stdlib/src/mmap.rs b/stdlib/src/mmap.rs index d4cb86cb2..32f5638aa 100644 --- a/stdlib/src/mmap.rs +++ b/stdlib/src/mmap.rs @@ -446,7 +446,8 @@ mod mmap { impl AsSequence for PyMmap { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + use once_cell::sync::Lazy; + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, _vm| Ok(PyMmap::sequence_downcast(seq).len())), item: atomic_func!(|seq, i, vm| { let zelf = PyMmap::sequence_downcast(seq); @@ -462,7 +463,7 @@ mod mmap { } }), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 5e275fd38..12e968926 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -26,6 +26,7 @@ use crate::{ TryFromBorrowedObject, TryFromObject, VirtualMachine, }; use bstr::ByteSlice; +use once_cell::sync::Lazy; use std::{mem::size_of, ops::Deref}; #[pyclass(module = false, name = "bytes")] @@ -572,20 +573,20 @@ impl AsBuffer for PyBytes { impl AsMapping for PyBytes { fn as_mapping() -> &'static PyMappingMethods { - static AS_MAPPING: PyMappingMethods = PyMappingMethods { + static AS_MAPPING: Lazy = Lazy::new(|| PyMappingMethods { length: atomic_func!(|mapping, _vm| Ok(PyBytes::mapping_downcast(mapping).len())), subscript: atomic_func!( |mapping, needle, vm| PyBytes::mapping_downcast(mapping)._getitem(needle, vm) ), ..PyMappingMethods::NOT_IMPLEMENTED - }; + }); &AS_MAPPING } } impl AsSequence for PyBytes { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, _vm| Ok(PyBytes::sequence_downcast(seq).len())), concat: atomic_func!(|seq, other, vm| { PyBytes::sequence_downcast(seq) @@ -612,21 +613,21 @@ impl AsSequence for PyBytes { PyBytes::sequence_downcast(seq).contains(other, vm) }), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } impl AsNumber for PyBytes { fn as_number() -> &'static PyNumberMethods { - static AS_NUMBER: PyNumberMethods = PyNumberMethods { + static AS_NUMBER: Lazy = Lazy::new(|| PyNumberMethods { remainder: atomic_func!(|number, other, vm| { PyBytes::number_downcast(number) .mod_(other.to_owned(), vm) .to_pyresult(vm) }), ..PyNumberMethods::NOT_IMPLEMENTED - }; + }); &AS_NUMBER } } diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 6fe343e2f..8801f3f61 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -15,6 +15,7 @@ use crate::{ }; use num_complex::Complex64; use num_traits::Zero; +use once_cell::sync::Lazy; use rustpython_common::{float_ops, hash}; use std::num::Wrapping; @@ -447,7 +448,7 @@ impl Hashable for PyComplex { impl AsNumber for PyComplex { fn as_number() -> &'static PyNumberMethods { - static AS_NUMBER: PyNumberMethods = PyNumberMethods { + static AS_NUMBER: Lazy = Lazy::new(|| PyNumberMethods { add: atomic_func!(|number, other, vm| PyComplex::number_complex_op( number, other, @@ -481,7 +482,7 @@ impl AsNumber for PyComplex { PyComplex::number_general_op(number, other, inner_div, vm) }), ..PyNumberMethods::NOT_IMPLEMENTED - }; + }); &AS_NUMBER } } diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 1b09aac9c..06da56fba 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -26,6 +26,7 @@ use crate::{ vm::VirtualMachine, AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, }; +use once_cell::sync::Lazy; use rustpython_common::lock::PyMutex; use std::fmt; @@ -465,12 +466,12 @@ impl AsMapping for PyDict { impl AsSequence for PyDict { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { contains: atomic_func!(|seq, target, vm| PyDict::sequence_downcast(seq) .entries .contains(vm, target)), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } @@ -1052,7 +1053,7 @@ impl Comparable for PyDictKeys { impl AsSequence for PyDictKeys { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, _vm| Ok(PyDictKeys::sequence_downcast(seq).len())), contains: atomic_func!(|seq, target, vm| { PyDictKeys::sequence_downcast(seq) @@ -1061,7 +1062,7 @@ impl AsSequence for PyDictKeys { .contains(vm, target) }), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } @@ -1108,7 +1109,7 @@ impl Comparable for PyDictItems { impl AsSequence for PyDictItems { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, _vm| Ok(PyDictItems::sequence_downcast(seq).len())), contains: atomic_func!(|seq, target, vm| { PyDictItems::sequence_downcast(seq) @@ -1117,7 +1118,7 @@ impl AsSequence for PyDictItems { .contains(vm, target) }), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } @@ -1133,10 +1134,10 @@ impl Unconstructible for PyDictValues {} impl AsSequence for PyDictValues { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, _vm| Ok(PyDictValues::sequence_downcast(seq).len())), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 72e3c0e61..1ba512f53 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -21,6 +21,7 @@ use num_bigint::{BigInt, ToBigInt}; use num_complex::Complex64; use num_rational::Ratio; use num_traits::{Signed, ToPrimitive, Zero}; +use once_cell::sync::Lazy; #[pyclass(module = false, name = "float")] #[derive(Debug, Copy, Clone, PartialEq)] @@ -549,7 +550,7 @@ impl Hashable for PyFloat { impl AsNumber for PyFloat { fn as_number() -> &'static PyNumberMethods { - static AS_NUMBER: PyNumberMethods = PyNumberMethods { + static AS_NUMBER: Lazy = Lazy::new(|| PyNumberMethods { add: atomic_func!(|num, other, vm| PyFloat::number_float_op( num, other, @@ -602,7 +603,7 @@ impl AsNumber for PyFloat { PyFloat::number_general_op(num, other, inner_div, vm) }), ..PyNumberMethods::NOT_IMPLEMENTED - }; + }); &AS_NUMBER } } diff --git a/vm/src/builtins/genericalias.rs b/vm/src/builtins/genericalias.rs index 557a42b68..082098643 100644 --- a/vm/src/builtins/genericalias.rs +++ b/vm/src/builtins/genericalias.rs @@ -1,3 +1,5 @@ +use once_cell::sync::Lazy; + use super::type_; use crate::{ atomic_func, @@ -320,12 +322,12 @@ pub fn subs_parameters PyResult>( impl AsMapping for PyGenericAlias { fn as_mapping() -> &'static PyMappingMethods { - static AS_MAPPING: PyMappingMethods = PyMappingMethods { + static AS_MAPPING: Lazy = Lazy::new(|| PyMappingMethods { subscript: atomic_func!(|mapping, needle, vm| { PyGenericAlias::mapping_downcast(mapping).getitem(needle.to_owned(), vm) }), ..PyMappingMethods::NOT_IMPLEMENTED - }; + }); &AS_MAPPING } } diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index fb4165bfe..4f1e3c2b0 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -20,6 +20,7 @@ use num_bigint::{BigInt, BigUint, Sign}; use num_integer::Integer; use num_rational::Ratio; use num_traits::{One, Pow, PrimInt, Signed, ToPrimitive, Zero}; +use once_cell::sync::Lazy; use std::ops::{Div, Neg}; use std::{fmt, ops::Not}; @@ -728,7 +729,7 @@ impl Hashable for PyInt { impl AsNumber for PyInt { fn as_number() -> &'static PyNumberMethods { - static AS_NUMBER: PyNumberMethods = PyNumberMethods { + static AS_NUMBER: Lazy = Lazy::new(|| PyNumberMethods { add: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a + b, vm)), subtract: atomic_func!(|num, other, vm| PyInt::number_int_op( num, @@ -794,7 +795,7 @@ impl AsNumber for PyInt { }), index: atomic_func!(|num, vm| Ok(PyInt::number_int(num, vm))), ..PyNumberMethods::NOT_IMPLEMENTED - }; + }); &AS_NUMBER } } diff --git a/vm/src/builtins/mappingproxy.rs b/vm/src/builtins/mappingproxy.rs index 04436b0dd..023abdd24 100644 --- a/vm/src/builtins/mappingproxy.rs +++ b/vm/src/builtins/mappingproxy.rs @@ -1,3 +1,5 @@ +use once_cell::sync::Lazy; + use super::{PyDict, PyDictRef, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef}; use crate::{ atomic_func, @@ -205,32 +207,32 @@ impl Comparable for PyMappingProxy { impl AsMapping for PyMappingProxy { fn as_mapping() -> &'static PyMappingMethods { - static AS_MAPPING: PyMappingMethods = PyMappingMethods { + static AS_MAPPING: Lazy = Lazy::new(|| PyMappingMethods { length: atomic_func!(|mapping, vm| PyMappingProxy::mapping_downcast(mapping).len(vm)), subscript: atomic_func!(|mapping, needle, vm| { PyMappingProxy::mapping_downcast(mapping).getitem(needle.to_owned(), vm) }), ..PyMappingMethods::NOT_IMPLEMENTED - }; + }); &AS_MAPPING } } impl AsSequence for PyMappingProxy { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { contains: atomic_func!( |seq, target, vm| PyMappingProxy::sequence_downcast(seq)._contains(target, vm) ), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } impl AsNumber for PyMappingProxy { fn as_number() -> &'static PyNumberMethods { - static AS_NUMBER: PyNumberMethods = PyNumberMethods { + static AS_NUMBER: Lazy = Lazy::new(|| PyNumberMethods { or: atomic_func!(|num, args, vm| { PyMappingProxy::number_downcast(num).or(args.to_pyobject(vm), vm) }), @@ -238,7 +240,7 @@ impl AsNumber for PyMappingProxy { PyMappingProxy::number_downcast(num).ior(args.to_pyobject(vm), vm) }), ..PyNumberMethods::NOT_IMPLEMENTED - }; + }); &AS_NUMBER } } diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index 9a5fdb4bc..e7cf19d40 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -29,6 +29,7 @@ use crate::{ }; use crossbeam_utils::atomic::AtomicCell; use itertools::Itertools; +use once_cell::sync::Lazy; use rustpython_common::lock::PyMutex; use std::{cmp::Ordering, fmt::Debug, mem::ManuallyDrop, ops::Range}; @@ -996,7 +997,7 @@ impl AsMapping for PyMemoryView { impl AsSequence for PyMemoryView { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, vm| { let zelf = PyMemoryView::sequence_downcast(seq); zelf.try_not_released(vm)?; @@ -1008,7 +1009,7 @@ impl AsSequence for PyMemoryView { zelf.getitem_by_idx(i, vm) }), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } diff --git a/vm/src/builtins/range.rs b/vm/src/builtins/range.rs index 296cae025..1ff71081e 100644 --- a/vm/src/builtins/range.rs +++ b/vm/src/builtins/range.rs @@ -17,6 +17,7 @@ use crossbeam_utils::atomic::AtomicCell; use num_bigint::{BigInt, Sign}; use num_integer::Integer; use num_traits::{One, Signed, ToPrimitive, Zero}; +use once_cell::sync::Lazy; use std::cmp::max; // Search flag passed to iter_search @@ -391,7 +392,7 @@ impl PyRange { impl AsMapping for PyRange { fn as_mapping() -> &'static PyMappingMethods { - static AS_MAPPING: PyMappingMethods = PyMappingMethods { + static AS_MAPPING: Lazy = Lazy::new(|| PyMappingMethods { length: atomic_func!( |mapping, vm| PyRange::mapping_downcast(mapping).protocol_length(vm) ), @@ -399,14 +400,14 @@ impl AsMapping for PyRange { PyRange::mapping_downcast(mapping).getitem(needle.to_owned(), vm) }), ..PyMappingMethods::NOT_IMPLEMENTED - }; + }); &AS_MAPPING } } impl AsSequence for PyRange { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, vm| PyRange::sequence_downcast(seq).protocol_length(vm)), item: atomic_func!(|seq, i, vm| { PyRange::sequence_downcast(seq) @@ -418,7 +419,7 @@ impl AsSequence for PyRange { Ok(PyRange::sequence_downcast(seq).contains(needle.to_owned(), vm)) }), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } diff --git a/vm/src/builtins/set.rs b/vm/src/builtins/set.rs index e97fa4105..0fe511f58 100644 --- a/vm/src/builtins/set.rs +++ b/vm/src/builtins/set.rs @@ -1,3 +1,5 @@ +use once_cell::sync::Lazy; + /* * Builtin set type with a sequence of unique items. */ @@ -758,13 +760,13 @@ impl Initializer for PySet { impl AsSequence for PySet { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, _vm| Ok(PySet::sequence_downcast(seq).len())), contains: atomic_func!(|seq, needle, vm| PySet::sequence_downcast(seq) .inner .contains(needle, vm)), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } @@ -980,13 +982,13 @@ impl PyFrozenSet { impl AsSequence for PyFrozenSet { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, _vm| Ok(PyFrozenSet::sequence_downcast(seq).len())), contains: atomic_func!(|seq, needle, vm| PyFrozenSet::sequence_downcast(seq) .inner .contains(needle, vm)), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index 1f863e8d5..65081a0d4 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -1,3 +1,5 @@ +use once_cell::sync::Lazy; + use super::{PyType, PyTypeRef}; use crate::{ atomic_func, @@ -58,10 +60,10 @@ impl PyNone { impl AsNumber for PyNone { fn as_number() -> &'static PyNumberMethods { - static AS_NUMBER: PyNumberMethods = PyNumberMethods { + static AS_NUMBER: Lazy = Lazy::new(|| PyNumberMethods { boolean: atomic_func!(|_number, _vm| Ok(false)), ..PyNumberMethods::NOT_IMPLEMENTED - }; + }); &AS_NUMBER } } diff --git a/vm/src/builtins/str.rs b/vm/src/builtins/str.rs index ed32e92d2..0dd2921d5 100644 --- a/vm/src/builtins/str.rs +++ b/vm/src/builtins/str.rs @@ -25,6 +25,7 @@ use ascii::{AsciiStr, AsciiString}; use bstr::ByteSlice; use itertools::Itertools; use num_traits::ToPrimitive; +use once_cell::sync::Lazy; use rustpython_common::{ ascii, atomic::{self, PyAtomic, Radium}, @@ -1302,20 +1303,20 @@ impl Iterable for PyStr { impl AsMapping for PyStr { fn as_mapping() -> &'static PyMappingMethods { - static AS_MAPPING: PyMappingMethods = PyMappingMethods { + static AS_MAPPING: Lazy = Lazy::new(|| PyMappingMethods { length: atomic_func!(|mapping, _vm| Ok(PyStr::mapping_downcast(mapping).len())), subscript: atomic_func!( |mapping, needle, vm| PyStr::mapping_downcast(mapping)._getitem(needle, vm) ), ..PyMappingMethods::NOT_IMPLEMENTED - }; + }); &AS_MAPPING } } impl AsSequence for PyStr { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, _vm| Ok(PyStr::sequence_downcast(seq).len())), concat: atomic_func!(|seq, other, vm| { let zelf = PyStr::sequence_downcast(seq); @@ -1334,7 +1335,7 @@ impl AsSequence for PyStr { |seq, needle, vm| PyStr::sequence_downcast(seq)._contains(needle, vm) ), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index f6fede1eb..b5472f699 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -1,3 +1,5 @@ +use once_cell::sync::Lazy; + use super::{PositionIterInternal, PyGenericAlias, PyType, PyTypeRef}; use crate::atomic_func; use crate::common::{hash::PyHash, lock::PyMutex}; @@ -324,20 +326,20 @@ impl PyTuple { impl AsMapping for PyTuple { fn as_mapping() -> &'static PyMappingMethods { - static AS_MAPPING: PyMappingMethods = PyMappingMethods { + static AS_MAPPING: Lazy = Lazy::new(|| PyMappingMethods { length: atomic_func!(|mapping, _vm| Ok(PyTuple::mapping_downcast(mapping).len())), subscript: atomic_func!( |mapping, needle, vm| PyTuple::mapping_downcast(mapping)._getitem(needle, vm) ), ..PyMappingMethods::NOT_IMPLEMENTED - }; + }); &AS_MAPPING } } impl AsSequence for PyTuple { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, _vm| Ok(PyTuple::sequence_downcast(seq).len())), concat: atomic_func!(|seq, other, vm| { let zelf = PyTuple::sequence_downcast(seq); @@ -362,7 +364,7 @@ impl AsSequence for PyTuple { zelf._contains(needle, vm) }), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } diff --git a/vm/src/builtins/union.rs b/vm/src/builtins/union.rs index 3f8905f14..7b9a65bef 100644 --- a/vm/src/builtins/union.rs +++ b/vm/src/builtins/union.rs @@ -1,3 +1,5 @@ +use once_cell::sync::Lazy; + use super::{genericalias, type_}; use crate::{ atomic_func, @@ -243,12 +245,12 @@ impl PyUnion { impl AsMapping for PyUnion { fn as_mapping() -> &'static PyMappingMethods { - static AS_MAPPING: PyMappingMethods = PyMappingMethods { + static AS_MAPPING: Lazy = Lazy::new(|| PyMappingMethods { subscript: atomic_func!(|mapping, needle, vm| { PyUnion::mapping_downcast(mapping).getitem(needle.to_owned(), vm) }), ..PyMappingMethods::NOT_IMPLEMENTED - }; + }); &AS_MAPPING } } diff --git a/vm/src/builtins/weakproxy.rs b/vm/src/builtins/weakproxy.rs index 9b3e9a28c..3db19199b 100644 --- a/vm/src/builtins/weakproxy.rs +++ b/vm/src/builtins/weakproxy.rs @@ -1,3 +1,5 @@ +use once_cell::sync::Lazy; + use super::{PyStrRef, PyType, PyTypeRef, PyWeak}; use crate::{ atomic_func, @@ -158,13 +160,13 @@ impl Comparable for PyWeakProxy { impl AsSequence for PyWeakProxy { fn as_sequence() -> &'static PySequenceMethods { - static AS_SEQUENCE: PySequenceMethods = PySequenceMethods { + static AS_SEQUENCE: Lazy = Lazy::new(|| PySequenceMethods { length: atomic_func!(|seq, vm| PyWeakProxy::sequence_downcast(seq).len(vm)), contains: atomic_func!(|seq, needle, vm| { PyWeakProxy::sequence_downcast(seq).contains(needle.to_owned(), vm) }), ..PySequenceMethods::NOT_IMPLEMENTED - }; + }); &AS_SEQUENCE } } diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index ae322b9c3..725f4d950 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -756,14 +756,15 @@ mod _sre { impl AsMapping for Match { fn as_mapping() -> &'static PyMappingMethods { - static AS_MAPPING: PyMappingMethods = PyMappingMethods { - subscript: atomic_func!(|mapping, needle, vm| { - Match::mapping_downcast(mapping) - .getitem(needle.to_owned(), vm) - .map(|x| x.to_pyobject(vm)) - }), - ..PyMappingMethods::NOT_IMPLEMENTED - }; + static AS_MAPPING: once_cell::sync::Lazy = + once_cell::sync::Lazy::new(|| PyMappingMethods { + subscript: atomic_func!(|mapping, needle, vm| { + Match::mapping_downcast(mapping) + .getitem(needle.to_owned(), vm) + .map(|x| x.to_pyobject(vm)) + }), + ..PyMappingMethods::NOT_IMPLEMENTED + }); &AS_MAPPING } }