Skip to content

Commit 3c4ac0e

Browse files
committed
clean up PyNumber interfaces
1 parent 9822749 commit 3c4ac0e

File tree

3 files changed

+37
-52
lines changed

3 files changed

+37
-52
lines changed

vm/src/protocol/number.rs

+31-46
Original file line numberDiff line numberDiff line change
@@ -194,41 +194,37 @@ impl PyNumberMethods {
194194
inplace_matrix_multiply: None,
195195
};
196196

197-
pub fn get_binary_op(
198-
&self,
199-
op_slot: PyNumberBinaryOp,
200-
) -> PyResult<&Option<PyNumberBinaryFunc>> {
197+
pub fn binary_op(&self, op_slot: PyNumberBinaryOp) -> Option<PyNumberBinaryFunc> {
201198
use PyNumberBinaryOp::*;
202-
let binary_op = match op_slot {
203-
Add => &self.add,
204-
Subtract => &self.subtract,
205-
Multiply => &self.multiply,
206-
Remainder => &self.remainder,
207-
Divmod => &self.divmod,
208-
Power => &self.power,
209-
Lshift => &self.lshift,
210-
Rshift => &self.rshift,
211-
And => &self.and,
212-
Xor => &self.xor,
213-
Or => &self.or,
214-
InplaceAdd => &self.inplace_add,
215-
InplaceSubtract => &self.inplace_subtract,
216-
InplaceMultiply => &self.inplace_multiply,
217-
InplaceRemainder => &self.inplace_remainder,
218-
InplacePower => &self.inplace_power,
219-
InplaceLshift => &self.inplace_lshift,
220-
InplaceRshift => &self.inplace_rshift,
221-
InplaceAnd => &self.inplace_and,
222-
InplaceXor => &self.inplace_xor,
223-
InplaceOr => &self.inplace_or,
224-
FloorDivide => &self.floor_divide,
225-
TrueDivide => &self.true_divide,
226-
InplaceFloorDivide => &self.inplace_floor_divide,
227-
InplaceTrueDivide => &self.inplace_true_divide,
228-
MatrixMultiply => &self.matrix_multiply,
229-
InplaceMatrixMultiply => &self.inplace_matrix_multiply,
230-
};
231-
Ok(binary_op)
199+
match op_slot {
200+
Add => self.add,
201+
Subtract => self.subtract,
202+
Multiply => self.multiply,
203+
Remainder => self.remainder,
204+
Divmod => self.divmod,
205+
Power => self.power,
206+
Lshift => self.lshift,
207+
Rshift => self.rshift,
208+
And => self.and,
209+
Xor => self.xor,
210+
Or => self.or,
211+
InplaceAdd => self.inplace_add,
212+
InplaceSubtract => self.inplace_subtract,
213+
InplaceMultiply => self.inplace_multiply,
214+
InplaceRemainder => self.inplace_remainder,
215+
InplacePower => self.inplace_power,
216+
InplaceLshift => self.inplace_lshift,
217+
InplaceRshift => self.inplace_rshift,
218+
InplaceAnd => self.inplace_and,
219+
InplaceXor => self.inplace_xor,
220+
InplaceOr => self.inplace_or,
221+
FloorDivide => self.floor_divide,
222+
TrueDivide => self.true_divide,
223+
InplaceFloorDivide => self.inplace_floor_divide,
224+
InplaceTrueDivide => self.inplace_true_divide,
225+
MatrixMultiply => self.matrix_multiply,
226+
InplaceMatrixMultiply => self.inplace_matrix_multiply,
227+
}
232228
}
233229
}
234230

@@ -266,7 +262,7 @@ pub enum PyNumberBinaryOp {
266262
#[derive(Copy, Clone)]
267263
pub struct PyNumber<'a> {
268264
pub obj: &'a PyObject,
269-
methods: &'a PyNumberMethods,
265+
pub(crate) methods: &'a PyNumberMethods,
270266
}
271267

272268
impl<'a> From<&'a PyObject> for PyNumber<'a> {
@@ -285,17 +281,6 @@ impl PyNumber<'_> {
285281
obj.class().mro_find_map(|x| x.slots.as_number.load())
286282
}
287283

288-
pub fn methods(&self) -> &PyNumberMethods {
289-
self.methods
290-
}
291-
292-
pub fn get_binary_op(
293-
&self,
294-
op_slot: PyNumberBinaryOp,
295-
) -> PyResult<&Option<PyNumberBinaryFunc>> {
296-
self.methods().get_binary_op(op_slot)
297-
}
298-
299284
// PyNumber_Check
300285
pub fn check(obj: &PyObject) -> bool {
301286
let methods = &obj.class().slots.number;

vm/src/types/slot.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub struct PyNumberSlots {
161161
}
162162

163163
impl PyNumberSlots {
164-
pub fn get_left_binary_op(
164+
pub fn left_binary_op(
165165
&self,
166166
op_slot: PyNumberBinaryOp,
167167
) -> PyResult<Option<PyNumberBinaryFunc>> {
@@ -198,7 +198,7 @@ impl PyNumberSlots {
198198
Ok(binary_op)
199199
}
200200

201-
pub fn get_right_binary_op(
201+
pub fn right_binary_op(
202202
&self,
203203
op_slot: PyNumberBinaryOp,
204204
) -> PyResult<Option<PyNumberBinaryFunc>> {
@@ -1281,7 +1281,7 @@ macro_rules! extend_number_slot {
12811281
if $methods.$method.is_some() {
12821282
$slots.number.$method.store($methods.$method);
12831283
$slots.number.$right_method.store(Some(|num, other, vm| {
1284-
num.get_binary_op(PyNumberBinaryOp::$op_slot)?.unwrap()(
1284+
num.methods.binary_op(PyNumberBinaryOp::$op_slot).unwrap()(
12851285
other.to_number(),
12861286
num.obj,
12871287
vm,

vm/src/vm/vm_ops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ impl VirtualMachine {
132132
///
133133
/// [*] only when Py_TYPE(a) != Py_TYPE(b) && Py_TYPE(b) is a subclass of Py_TYPE(a)
134134
pub fn binary_op1(&self, a: &PyObject, b: &PyObject, op_slot: PyNumberBinaryOp) -> PyResult {
135-
let slot_a = a.class().slots.number.get_left_binary_op(op_slot)?;
135+
let slot_a = a.class().slots.number.left_binary_op(op_slot)?;
136136
let mut slot_b = if b.class().is(a.class()) {
137137
None
138138
} else {
139-
match b.class().slots.number.get_right_binary_op(op_slot)? {
139+
match b.class().slots.number.right_binary_op(op_slot)? {
140140
Some(slot_b)
141141
if slot_b as usize == slot_a.map(|s| s as usize).unwrap_or_default() =>
142142
{
@@ -206,7 +206,7 @@ impl VirtualMachine {
206206
iop_slot: PyNumberBinaryOp,
207207
op_slot: PyNumberBinaryOp,
208208
) -> PyResult {
209-
if let Some(slot) = a.class().slots.number.get_left_binary_op(iop_slot)? {
209+
if let Some(slot) = a.class().slots.number.left_binary_op(iop_slot)? {
210210
let x = slot(a.to_number(), b, self)?;
211211
if !x.is(&self.ctx.not_implemented) {
212212
return Ok(x);

0 commit comments

Comments
 (0)