refactor: use Lazy for static

This commit is contained in:
Discord9 committed 2022-11-24 12:17:07 +08:00
1 parent f885db8c61
commit 754b28c086
17 files changed
+86 -62

No files matched your search

+3 -2
View File
@@ -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<PySequenceMethods> = 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
}
}
+7 -6
View File
@@ -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<PyMappingMethods> = 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<PySequenceMethods> = 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<PyNumberMethods> = 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
}
}
+3 -2
View File
@@ -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<PyNumberMethods> = 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
}
}
+9 -8
View File
@@ -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<PySequenceMethods> = 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<PySequenceMethods> = 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<PySequenceMethods> = 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<PySequenceMethods> = Lazy::new(|| PySequenceMethods {
length: atomic_func!(|seq, _vm| Ok(PyDictValues::sequence_downcast(seq).len())),
..PySequenceMethods::NOT_IMPLEMENTED
};
});
&AS_SEQUENCE
}
}
+3 -2
View File
@@ -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<PyNumberMethods> = 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
}
}
+4 -2
View File
@@ -1,3 +1,5 @@
use once_cell::sync::Lazy;
use super::type_;
use crate::{
atomic_func,
@@ -320,12 +322,12 @@ pub fn subs_parameters<F: Fn(&VirtualMachine) -> PyResult<String>>(
impl AsMapping for PyGenericAlias {
fn as_mapping() -> &'static PyMappingMethods {
static AS_MAPPING: PyMappingMethods = PyMappingMethods {
static AS_MAPPING: Lazy<PyMappingMethods> = Lazy::new(|| PyMappingMethods {
subscript: atomic_func!(|mapping, needle, vm| {
PyGenericAlias::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
..PyMappingMethods::NOT_IMPLEMENTED
};
});
&AS_MAPPING
}
}
+3 -2
View File
@@ -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<PyNumberMethods> = 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
}
}
+8 -6
View File
@@ -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<PyMappingMethods> = 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<PySequenceMethods> = 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<PyNumberMethods> = 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
}
}
+3 -2
View File
@@ -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<PySequenceMethods> = 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
}
}
+5 -4
View File
@@ -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<PyMappingMethods> = 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<PySequenceMethods> = 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
}
}
+6 -4
View File
@@ -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<PySequenceMethods> = 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<PySequenceMethods> = 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
}
}
+4 -2
View File
@@ -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<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
boolean: atomic_func!(|_number, _vm| Ok(false)),
..PyNumberMethods::NOT_IMPLEMENTED
};
});
&AS_NUMBER
}
}
+5 -4
View File
@@ -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<PyMappingMethods> = 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<PySequenceMethods> = 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
}
}
+6 -4
View File
@@ -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<PyMappingMethods> = 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<PySequenceMethods> = 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
}
}
+4 -2
View File
@@ -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<PyMappingMethods> = Lazy::new(|| PyMappingMethods {
subscript: atomic_func!(|mapping, needle, vm| {
PyUnion::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
..PyMappingMethods::NOT_IMPLEMENTED
};
});
&AS_MAPPING
}
}
+4 -2
View File
@@ -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<PySequenceMethods> = 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
}
}
+9 -8
View File
@@ -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<PyMappingMethods> =
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
}
}