Skip to content

Commit 183e8da

Browse files
committed
refactor: use Lazy for PyNumberMethod static
1 parent 0158ed1 commit 183e8da

File tree

7 files changed

+48
-41
lines changed

7 files changed

+48
-41
lines changed

Cargo.lock

Lines changed: 28 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/src/builtins/bytes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::{
2626
TryFromBorrowedObject, TryFromObject, VirtualMachine,
2727
};
2828
use bstr::ByteSlice;
29+
use once_cell::sync::Lazy;
2930
use std::{mem::size_of, ops::Deref};
3031

3132
#[pyclass(module = false, name = "bytes")]
@@ -610,14 +611,14 @@ impl AsSequence for PyBytes {
610611

611612
impl AsNumber for PyBytes {
612613
fn as_number() -> &'static PyNumberMethods {
613-
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
614+
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
614615
remainder: atomic_func!(|number, other, vm| {
615616
PyBytes::number_downcast(number)
616617
.mod_(other.to_owned(), vm)
617618
.to_pyresult(vm)
618619
}),
619620
..PyNumberMethods::NOT_IMPLEMENTED
620-
};
621+
});
621622
&AS_NUMBER
622623
}
623624
}

vm/src/builtins/complex.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
};
1616
use num_complex::Complex64;
1717
use num_traits::Zero;
18+
use once_cell::sync::Lazy;
1819
use rustpython_common::{float_ops, hash};
1920
use std::num::Wrapping;
2021

@@ -447,7 +448,7 @@ impl Hashable for PyComplex {
447448

448449
impl AsNumber for PyComplex {
449450
fn as_number() -> &'static PyNumberMethods {
450-
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
451+
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
451452
add: atomic_func!(|number, other, vm| PyComplex::number_complex_op(
452453
number,
453454
other,
@@ -481,7 +482,7 @@ impl AsNumber for PyComplex {
481482
PyComplex::number_general_op(number, other, inner_div, vm)
482483
}),
483484
..PyNumberMethods::NOT_IMPLEMENTED
484-
};
485+
});
485486
&AS_NUMBER
486487
}
487488
}

vm/src/builtins/float.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use num_bigint::{BigInt, ToBigInt};
2121
use num_complex::Complex64;
2222
use num_rational::Ratio;
2323
use num_traits::{Signed, ToPrimitive, Zero};
24+
use once_cell::sync::Lazy;
2425

2526
/// Convert a string or number to a floating point number, if possible.
2627
#[pyclass(module = false, name = "float")]
@@ -549,7 +550,7 @@ impl Hashable for PyFloat {
549550

550551
impl AsNumber for PyFloat {
551552
fn as_number() -> &'static PyNumberMethods {
552-
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
553+
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
553554
add: atomic_func!(|num, other, vm| PyFloat::number_float_op(
554555
num,
555556
other,
@@ -602,7 +603,7 @@ impl AsNumber for PyFloat {
602603
PyFloat::number_general_op(num, other, inner_div, vm)
603604
}),
604605
..PyNumberMethods::NOT_IMPLEMENTED
605-
};
606+
});
606607
&AS_NUMBER
607608
}
608609
}

vm/src/builtins/int.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use bstr::ByteSlice;
1919
use num_bigint::{BigInt, BigUint, Sign};
2020
use num_integer::Integer;
2121
use num_traits::{One, Pow, PrimInt, Signed, ToPrimitive, Zero};
22+
use once_cell::sync::Lazy;
2223
use std::ops::Neg;
2324
use std::{fmt, ops::Not};
2425

@@ -756,7 +757,7 @@ impl Hashable for PyInt {
756757

757758
impl AsNumber for PyInt {
758759
fn as_number() -> &'static PyNumberMethods {
759-
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
760+
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
760761
add: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a + b, vm)),
761762
subtract: atomic_func!(|num, other, vm| PyInt::number_int_op(
762763
num,
@@ -822,7 +823,7 @@ impl AsNumber for PyInt {
822823
}),
823824
index: atomic_func!(|num, vm| Ok(PyInt::number_int(num, vm))),
824825
..PyNumberMethods::NOT_IMPLEMENTED
825-
};
826+
});
826827
&AS_NUMBER
827828
}
828829
}

vm/src/builtins/mappingproxy.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use once_cell::sync::Lazy;
2+
13
use super::{PyDict, PyDictRef, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef};
24
use crate::{
35
atomic_func,
@@ -219,15 +221,15 @@ impl AsSequence for PyMappingProxy {
219221

220222
impl AsNumber for PyMappingProxy {
221223
fn as_number() -> &'static PyNumberMethods {
222-
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
224+
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
223225
or: atomic_func!(|num, args, vm| {
224226
PyMappingProxy::number_downcast(num).or(args.to_pyobject(vm), vm)
225227
}),
226228
inplace_or: atomic_func!(|num, args, vm| {
227229
PyMappingProxy::number_downcast(num).ior(args.to_pyobject(vm), vm)
228230
}),
229231
..PyNumberMethods::NOT_IMPLEMENTED
230-
};
232+
});
231233
&AS_NUMBER
232234
}
233235
}

vm/src/builtins/singletons.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use once_cell::sync::Lazy;
2+
13
use super::{PyType, PyTypeRef};
24
use crate::{
35
atomic_func,
@@ -58,10 +60,10 @@ impl PyNone {
5860

5961
impl AsNumber for PyNone {
6062
fn as_number() -> &'static PyNumberMethods {
61-
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
63+
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
6264
boolean: atomic_func!(|_number, _vm| Ok(false)),
6365
..PyNumberMethods::NOT_IMPLEMENTED
64-
};
66+
});
6567
&AS_NUMBER
6668
}
6769
}

0 commit comments

Comments
 (0)