Skip to content

Commit fb7393e

Browse files
committed
impl create PyNumberSlots from PyNumberMethods
1 parent 0a8c642 commit fb7393e

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,6 @@ where
703703
let tokens = {
704704
const NON_ATOMIC_SLOTS: &[&str] = &["as_buffer"];
705705
const POINTER_SLOTS: &[&str] = &["as_sequence", "as_mapping"];
706-
const STATIC_SLOTS: &[&str] = &["as_number"];
707706

708707
if NON_ATOMIC_SLOTS.contains(&slot_name.as_str()) {
709708
quote_spanned! { span =>
@@ -713,9 +712,10 @@ where
713712
quote_spanned! { span =>
714713
slots.#slot_ident.store(Some(PointerSlot::from(Self::#ident())));
715714
}
716-
} else if STATIC_SLOTS.contains(&&slot_name.as_str()) {
715+
} else if "as_number" == slot_name.as_str() {
717716
quote_spanned! { span =>
718717
slots.#slot_ident = Self::#ident();
718+
slots.number = Self::#ident().into();
719719
}
720720
} else {
721721
quote_spanned! { span =>

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

+58
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,64 @@ pub struct PyNumberSlots {
293293
pub inplace_matrix_multiply: AtomicCell<Option<PyNumberBinaryFunc>>,
294294
}
295295

296+
impl From<&PyNumberMethods> for PyNumberSlots {
297+
fn from(value: &PyNumberMethods) -> Self {
298+
// right_* functions will use the same left function as PyNumberMethods
299+
// allows both f(self, other) and f(other, self)
300+
Self {
301+
add: AtomicCell::new(value.add),
302+
subtract: AtomicCell::new(value.subtract),
303+
multiply: AtomicCell::new(value.multiply),
304+
remainder: AtomicCell::new(value.remainder),
305+
divmod: AtomicCell::new(value.divmod),
306+
power: AtomicCell::new(value.power),
307+
negative: AtomicCell::new(value.negative),
308+
positive: AtomicCell::new(value.positive),
309+
absolute: AtomicCell::new(value.absolute),
310+
boolean: AtomicCell::new(value.boolean),
311+
invert: AtomicCell::new(value.invert),
312+
lshift: AtomicCell::new(value.lshift),
313+
rshift: AtomicCell::new(value.rshift),
314+
and: AtomicCell::new(value.and),
315+
xor: AtomicCell::new(value.xor),
316+
or: AtomicCell::new(value.or),
317+
int: AtomicCell::new(value.int),
318+
float: AtomicCell::new(value.float),
319+
right_add: AtomicCell::new(value.add),
320+
right_subtract: AtomicCell::new(value.subtract),
321+
right_multiply: AtomicCell::new(value.multiply),
322+
right_remainder: AtomicCell::new(value.remainder),
323+
right_divmod: AtomicCell::new(value.divmod),
324+
right_power: AtomicCell::new(value.power),
325+
right_lshift: AtomicCell::new(value.lshift),
326+
right_rshift: AtomicCell::new(value.rshift),
327+
right_and: AtomicCell::new(value.and),
328+
right_xor: AtomicCell::new(value.xor),
329+
right_or: AtomicCell::new(value.or),
330+
inplace_add: AtomicCell::new(value.inplace_add),
331+
inplace_subtract: AtomicCell::new(value.inplace_subtract),
332+
inplace_multiply: AtomicCell::new(value.inplace_multiply),
333+
inplace_remainder: AtomicCell::new(value.inplace_remainder),
334+
inplace_power: AtomicCell::new(value.inplace_power),
335+
inplace_lshift: AtomicCell::new(value.inplace_lshift),
336+
inplace_rshift: AtomicCell::new(value.inplace_rshift),
337+
inplace_and: AtomicCell::new(value.inplace_and),
338+
inplace_xor: AtomicCell::new(value.inplace_xor),
339+
inplace_or: AtomicCell::new(value.inplace_or),
340+
floor_divide: AtomicCell::new(value.floor_divide),
341+
true_divide: AtomicCell::new(value.true_divide),
342+
right_floor_divide: AtomicCell::new(value.floor_divide),
343+
right_true_divide: AtomicCell::new(value.true_divide),
344+
inplace_floor_divide: AtomicCell::new(value.inplace_floor_divide),
345+
inplace_true_divide: AtomicCell::new(value.inplace_true_divide),
346+
index: AtomicCell::new(value.index),
347+
matrix_multiply: AtomicCell::new(value.matrix_multiply),
348+
right_matrix_multiply: AtomicCell::new(value.matrix_multiply),
349+
inplace_matrix_multiply: AtomicCell::new(value.inplace_matrix_multiply),
350+
}
351+
}
352+
}
353+
296354
impl PyNumberSlots {
297355
pub fn left_binary_op(&self, op_slot: PyNumberBinaryOp) -> Option<PyNumberBinaryFunc> {
298356
use PyNumberBinaryOp::*;

0 commit comments

Comments
 (0)