@@ -8,6 +8,7 @@ use crate::{
8
8
VirtualMachine ,
9
9
} ;
10
10
use crossbeam_utils:: atomic:: AtomicCell ;
11
+ use std:: ptr;
11
12
12
13
type UnaryFunc < R = PyObjectRef > = AtomicCell < Option < fn ( PyNumber , & VirtualMachine ) -> PyResult < R > > > ;
13
14
type BinaryFunc < R = PyObjectRef > =
@@ -109,6 +110,7 @@ impl PyObject {
109
110
}
110
111
111
112
#[ derive( Default ) ]
113
+ // #[repr(C)]
112
114
pub struct PyNumberMethods {
113
115
/* Number implementations must check *both*
114
116
arguments for proper type and implement the necessary conversions
@@ -199,6 +201,46 @@ impl PyNumberMethods {
199
201
} ;
200
202
}
201
203
204
+ pub enum PyNumberMethodsOffset {
205
+ Add ,
206
+ Subtract ,
207
+ Multiply ,
208
+ Remainder ,
209
+ Divmod ,
210
+ Power ,
211
+ Negative ,
212
+ Positive ,
213
+ Absolute ,
214
+ Boolean ,
215
+ Invert ,
216
+ Lshift ,
217
+ Rshift ,
218
+ And ,
219
+ Xor ,
220
+ Or ,
221
+ Int ,
222
+ Float ,
223
+ InplaceAdd ,
224
+ InplaceSubtract ,
225
+ InplaceMultiply ,
226
+ InplaceRemainder ,
227
+ InplaceDivmod ,
228
+ InplacePower ,
229
+ InplaceLshift ,
230
+ InplaceRshift ,
231
+ InplaceAnd ,
232
+ InplaceXor ,
233
+ InplaceOr ,
234
+ FloorDivide ,
235
+ TrueDivide ,
236
+ InplaceFloorDivide ,
237
+ InplaceTrueDivide ,
238
+ Index ,
239
+ MatrixMultiply ,
240
+ InplaceMatrixMultiply ,
241
+ }
242
+
243
+ #[ derive( Copy , Clone ) ]
202
244
pub struct PyNumber < ' a > {
203
245
pub obj : & ' a PyObject ,
204
246
methods : & ' a PyNumberMethods ,
@@ -220,8 +262,12 @@ impl PyNumber<'_> {
220
262
obj. class ( ) . mro_find_map ( |x| x. slots . as_number . load ( ) )
221
263
}
222
264
223
- pub fn methods ( & self ) -> & PyNumberMethods {
224
- self . methods
265
+ pub fn methods < ' a > (
266
+ & ' a self ,
267
+ op_slot : & ' a PyNumberMethodsOffset ,
268
+ vm : & VirtualMachine ,
269
+ ) -> PyResult < & BinaryFunc > {
270
+ op_slot. method ( self . methods , vm)
225
271
}
226
272
227
273
// PyNumber_Check
@@ -238,12 +284,12 @@ impl PyNumber<'_> {
238
284
239
285
// PyIndex_Check
240
286
pub fn is_index ( & self ) -> bool {
241
- self . methods ( ) . index . load ( ) . is_some ( )
287
+ self . methods . index . load ( ) . is_some ( )
242
288
}
243
289
244
290
#[ inline]
245
291
pub fn int ( self , vm : & VirtualMachine ) -> Option < PyResult < PyIntRef > > {
246
- self . methods ( ) . int . load ( ) . map ( |f| {
292
+ self . methods . int . load ( ) . map ( |f| {
247
293
let ret = f ( self , vm) ?;
248
294
let value = if !ret. class ( ) . is ( PyInt :: class ( vm) ) {
249
295
warnings:: warn (
@@ -267,7 +313,7 @@ impl PyNumber<'_> {
267
313
268
314
#[ inline]
269
315
pub fn index ( self , vm : & VirtualMachine ) -> Option < PyResult < PyIntRef > > {
270
- self . methods ( ) . index . load ( ) . map ( |f| {
316
+ self . methods . index . load ( ) . map ( |f| {
271
317
let ret = f ( self , vm) ?;
272
318
let value = if !ret. class ( ) . is ( PyInt :: class ( vm) ) {
273
319
warnings:: warn (
@@ -291,7 +337,7 @@ impl PyNumber<'_> {
291
337
292
338
#[ inline]
293
339
pub fn float ( self , vm : & VirtualMachine ) -> Option < PyResult < PyRef < PyFloat > > > {
294
- self . methods ( ) . float . load ( ) . map ( |f| {
340
+ self . methods . float . load ( ) . map ( |f| {
295
341
let ret = f ( self , vm) ?;
296
342
let value = if !ret. class ( ) . is ( PyFloat :: class ( vm) ) {
297
343
warnings:: warn (
0 commit comments