Skip to content

Commit 0a8c642

Browse files
committed
change as_number to static immutable
1 parent 9c3ced0 commit 0a8c642

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

Diff for: derive-impl/src/pyclass.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,9 @@ where
702702
let slot_name = slot_ident.to_string();
703703
let tokens = {
704704
const NON_ATOMIC_SLOTS: &[&str] = &["as_buffer"];
705-
const POINTER_SLOTS: &[&str] = &["as_number", "as_sequence", "as_mapping"];
705+
const POINTER_SLOTS: &[&str] = &["as_sequence", "as_mapping"];
706+
const STATIC_SLOTS: &[&str] = &["as_number"];
707+
706708
if NON_ATOMIC_SLOTS.contains(&slot_name.as_str()) {
707709
quote_spanned! { span =>
708710
slots.#slot_ident = Some(Self::#ident as _);
@@ -711,6 +713,10 @@ where
711713
quote_spanned! { span =>
712714
slots.#slot_ident.store(Some(PointerSlot::from(Self::#ident())));
713715
}
716+
} else if STATIC_SLOTS.contains(&&slot_name.as_str()) {
717+
quote_spanned! { span =>
718+
slots.#slot_ident = Self::#ident();
719+
}
714720
} else {
715721
quote_spanned! { span =>
716722
slots.#slot_ident.store(Some(Self::#ident as _));

Diff for: vm/src/protocol/number.rs

+5
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ impl PyNumberMethods {
197197
matrix_multiply: None,
198198
inplace_matrix_multiply: None,
199199
};
200+
201+
pub fn not_implemented() -> &'static PyNumberMethods {
202+
static GLOBAL_NOT_IMPLEMENTED: PyNumberMethods = PyNumberMethods::NOT_IMPLEMENTED;
203+
&GLOBAL_NOT_IMPLEMENTED
204+
}
200205
}
201206

202207
#[derive(Copy, Clone)]

Diff for: vm/src/types/slot.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ macro_rules! atomic_func {
2727

2828
// The corresponding field in CPython is `tp_` prefixed.
2929
// e.g. name -> tp_name
30-
#[derive(Default)]
3130
#[non_exhaustive]
3231
pub struct PyTypeSlots {
3332
/// # Safety
@@ -41,7 +40,10 @@ pub struct PyTypeSlots {
4140
// Methods to implement standard operations
4241

4342
// Method suites for standard classes
44-
pub as_number: AtomicCell<Option<PointerSlot<PyNumberMethods>>>,
43+
/// # Safety
44+
/// use only when initializing
45+
/// use slot number for protocol
46+
pub(crate) as_number: &'static PyNumberMethods,
4547
pub as_sequence: AtomicCell<Option<PointerSlot<PySequenceMethods>>>,
4648
pub as_mapping: AtomicCell<Option<PointerSlot<PyMappingMethods>>>,
4749

@@ -93,6 +95,36 @@ pub struct PyTypeSlots {
9395
pub number: PyNumberSlots,
9496
}
9597

98+
impl Default for PyTypeSlots {
99+
fn default() -> Self {
100+
Self {
101+
name: Default::default(),
102+
basicsize: Default::default(),
103+
as_number: PyNumberMethods::not_implemented(),
104+
as_sequence: Default::default(),
105+
as_mapping: Default::default(),
106+
hash: Default::default(),
107+
call: Default::default(),
108+
repr: Default::default(),
109+
getattro: Default::default(),
110+
setattro: Default::default(),
111+
as_buffer: Default::default(),
112+
richcompare: Default::default(),
113+
iter: Default::default(),
114+
iternext: Default::default(),
115+
flags: Default::default(),
116+
doc: Default::default(),
117+
descr_get: Default::default(),
118+
descr_set: Default::default(),
119+
init: Default::default(),
120+
new: Default::default(),
121+
del: Default::default(),
122+
member_count: Default::default(),
123+
number: Default::default(),
124+
}
125+
}
126+
}
127+
96128
impl PyTypeSlots {
97129
pub fn new(name: &'static str, flags: PyTypeFlags) -> Self {
98130
Self {

0 commit comments

Comments
 (0)