Skip to content

Commit f820aeb

Browse files
Convert complex payload
1 parent 38c43e0 commit f820aeb

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

vm/src/obj/objcomplex.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@ use super::objfloat;
22
use super::objint;
33
use super::objtype;
44
use crate::pyobject::{
5-
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
5+
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
6+
TypeProtocol,
67
};
78
use crate::vm::VirtualMachine;
89
use num_complex::Complex64;
910
use num_traits::ToPrimitive;
1011

12+
#[derive(Debug, Copy, Clone, PartialEq)]
13+
pub struct PyComplex {
14+
value: Complex64,
15+
}
16+
17+
impl PyObjectPayload2 for PyComplex {
18+
fn required_type(ctx: &PyContext) -> PyObjectRef {
19+
ctx.complex_type()
20+
}
21+
}
22+
23+
impl From<Complex64> for PyComplex {
24+
fn from(value: Complex64) -> Self {
25+
PyComplex { value }
26+
}
27+
}
28+
1129
pub fn init(context: &PyContext) {
1230
let complex_type = &context.complex_type;
1331

@@ -45,11 +63,7 @@ pub fn init(context: &PyContext) {
4563
}
4664

4765
pub fn get_value(obj: &PyObjectRef) -> Complex64 {
48-
if let PyObjectPayload::Complex { value } = &obj.payload {
49-
*value
50-
} else {
51-
panic!("Inner error getting complex");
52-
}
66+
obj.payload::<PyComplex>().unwrap().value
5367
}
5468

5569
fn complex_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -77,7 +91,9 @@ fn complex_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7791
let value = Complex64::new(real, imag);
7892

7993
Ok(PyObject::new(
80-
PyObjectPayload::Complex { value },
94+
PyObjectPayload::AnyRustValue {
95+
value: Box::new(PyComplex { value }),
96+
},
8197
cls.clone(),
8298
))
8399
}

vm/src/pyobject.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ use std::iter;
55
use std::ops::RangeInclusive;
66
use std::rc::{Rc, Weak};
77

8+
use num_bigint::BigInt;
9+
use num_bigint::ToBigInt;
10+
use num_complex::Complex64;
11+
use num_traits::{One, Zero};
12+
813
use crate::bytecode;
914
use crate::exceptions;
1015
use crate::frame::{Frame, Scope, ScopeRef};
1116
use crate::obj::objbool;
1217
use crate::obj::objbytearray;
1318
use crate::obj::objbytes;
1419
use crate::obj::objcode;
15-
use crate::obj::objcomplex;
20+
use crate::obj::objcomplex::{self, PyComplex};
1621
use crate::obj::objdict;
1722
use crate::obj::objenumerate;
1823
use crate::obj::objfilter;
19-
use crate::obj::objfloat;
24+
use crate::obj::objfloat::{self, PyFloat};
2025
use crate::obj::objframe;
2126
use crate::obj::objfunction;
2227
use crate::obj::objgenerator;
@@ -37,10 +42,6 @@ use crate::obj::objtuple;
3742
use crate::obj::objtype;
3843
use crate::obj::objzip;
3944
use crate::vm::VirtualMachine;
40-
use num_bigint::BigInt;
41-
use num_bigint::ToBigInt;
42-
use num_complex::Complex64;
43-
use num_traits::{One, Zero};
4445

4546
/* Python objects and references.
4647
@@ -463,14 +464,19 @@ impl PyContext {
463464
pub fn new_float(&self, value: f64) -> PyObjectRef {
464465
PyObject::new(
465466
PyObjectPayload::AnyRustValue {
466-
value: Box::new(objfloat::PyFloat::from(value)),
467+
value: Box::new(PyFloat::from(value)),
467468
},
468469
self.float_type(),
469470
)
470471
}
471472

472-
pub fn new_complex(&self, i: Complex64) -> PyObjectRef {
473-
PyObject::new(PyObjectPayload::Complex { value: i }, self.complex_type())
473+
pub fn new_complex(&self, value: Complex64) -> PyObjectRef {
474+
PyObject::new(
475+
PyObjectPayload::AnyRustValue {
476+
value: Box::new(PyComplex::from(value)),
477+
},
478+
self.complex_type(),
479+
)
474480
}
475481

476482
pub fn new_str(&self, s: String) -> PyObjectRef {
@@ -1408,9 +1414,6 @@ pub enum PyObjectPayload {
14081414
Integer {
14091415
value: BigInt,
14101416
},
1411-
Complex {
1412-
value: Complex64,
1413-
},
14141417
Sequence {
14151418
elements: RefCell<Vec<PyObjectRef>>,
14161419
},
@@ -1494,7 +1497,6 @@ impl fmt::Debug for PyObjectPayload {
14941497
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14951498
match self {
14961499
PyObjectPayload::Integer { ref value } => write!(f, "int {}", value),
1497-
PyObjectPayload::Complex { ref value } => write!(f, "complex {}", value),
14981500
PyObjectPayload::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
14991501
PyObjectPayload::Sequence { .. } => write!(f, "list or tuple"),
15001502
PyObjectPayload::Dict { .. } => write!(f, "dict"),

0 commit comments

Comments
 (0)