diff --git a/vm/src/compile.rs b/vm/src/compile.rs index 21ce3a99f..5a3fe6b1d 100644 --- a/vm/src/compile.rs +++ b/vm/src/compile.rs @@ -8,7 +8,7 @@ use crate::bytecode::{self, CallType, CodeObject, Instruction}; use crate::error::CompileError; use crate::obj::objcode; -use crate::pyobject::{PyObject, PyObjectPayload, PyObjectRef}; +use crate::pyobject::{PyObject, PyObjectRef}; use num_complex::Complex64; use rustpython_parser::{ast, parser}; @@ -50,9 +50,7 @@ pub fn compile( let code = compiler.pop_code_object(); trace!("Compilation completed: {:?}", code); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objcode::PyCode::new(code)), - }, + Box::new(objcode::PyCode::new(code)), code_type, )) } diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 0d8788637..2e100f791 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -22,8 +22,8 @@ use crate::obj::objslice::PySlice; use crate::obj::objstr; use crate::obj::objtype; use crate::pyobject::{ - DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyResult, TryFromObject, TypeProtocol, + DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, + PyResult, TryFromObject, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -407,12 +407,8 @@ impl Frame { let stop = out[1].take(); let step = if out.len() == 3 { out[2].take() } else { None }; - let obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySlice { start, stop, step }), - }, - vm.ctx.slice_type(), - ); + let obj = + PyObject::new(Box::new(PySlice { start, stop, step }), vm.ctx.slice_type()); self.push_value(obj); Ok(None) } @@ -706,11 +702,9 @@ impl Frame { } bytecode::Instruction::LoadBuildClass => { let rustfunc = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyBuiltinFunction::new(Box::new( - builtins::builtin_build_class_, - ))), - }, + Box::new(PyBuiltinFunction::new(Box::new( + builtins::builtin_build_class_, + ))), vm.ctx.type_type(), ); self.push_value(rustfunc); diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index 77b1c14eb..d2a098edf 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -5,8 +5,7 @@ use std::fmt::Write; use std::ops::{Deref, DerefMut}; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use super::objint; @@ -174,9 +173,7 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { vec![] }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyByteArray::new(value)), - }, + Box::new(PyByteArray::new(value)), cls.clone(), )) } diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs index 2aa0006dd..31fe95e14 100644 --- a/vm/src/obj/objbytes.rs +++ b/vm/src/obj/objbytes.rs @@ -5,8 +5,8 @@ use std::ops::Deref; use super::objint; use super::objtype; use crate::pyobject::{ - PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, PyResult, + TypeProtocol, }; use crate::vm::VirtualMachine; use num_traits::ToPrimitive; @@ -95,12 +95,7 @@ fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { vec![] }; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyBytes::new(value)), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyBytes::new(value)), cls.clone())) } fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -209,12 +204,10 @@ fn bytes_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytes_type()))]); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: obj.clone(), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: obj.clone(), + }), vm.ctx.iter_type(), ); diff --git a/vm/src/obj/objcomplex.rs b/vm/src/obj/objcomplex.rs index 66205559c..afafe48ed 100644 --- a/vm/src/obj/objcomplex.rs +++ b/vm/src/obj/objcomplex.rs @@ -2,8 +2,7 @@ use super::objfloat; use super::objint; use super::objtype; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use num_complex::Complex64; @@ -90,12 +89,7 @@ fn complex_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let value = Complex64::new(real, imag); - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyComplex { value }), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyComplex { value }), cls.clone())) } fn complex_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/obj/objdict.rs b/vm/src/obj/objdict.rs index 84391cdd8..7e3cef048 100644 --- a/vm/src/obj/objdict.rs +++ b/vm/src/obj/objdict.rs @@ -3,8 +3,8 @@ use std::collections::HashMap; use std::ops::{Deref, DerefMut}; use crate::pyobject::{ - PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, + PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, + PyRef, PyResult, TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; @@ -251,12 +251,10 @@ fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let key_list = vm.ctx.new_list(keys); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: key_list, - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: key_list, + }), vm.ctx.iter_type(), ); @@ -273,12 +271,10 @@ fn dict_values(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let values_list = vm.ctx.new_list(values); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: values_list, - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: values_list, + }), vm.ctx.iter_type(), ); @@ -295,12 +291,10 @@ fn dict_items(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let items_list = vm.ctx.new_list(items); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: items_list, - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: items_list, + }), vm.ctx.iter_type(), ); @@ -348,12 +342,10 @@ pub fn create_type(type_type: PyObjectRef, object_type: PyObjectRef, dict_type: // this is not ideal let ptr = PyObjectRef::into_raw(dict_type.clone()) as *mut PyObject; unsafe { - (*ptr).payload = PyObjectPayload::AnyRustValue { - value: Box::new(objtype::PyClass { - name: String::from("dict"), - mro: vec![object_type], - }), - }; + (*ptr).payload = Box::new(objtype::PyClass { + name: String::from("dict"), + mro: vec![object_type], + }); (*ptr).dict = Some(RefCell::new(HashMap::new())); (*ptr).typ = Some(type_type.clone()); } diff --git a/vm/src/obj/objenumerate.rs b/vm/src/obj/objenumerate.rs index aa573ad6f..d02fc2086 100644 --- a/vm/src/obj/objenumerate.rs +++ b/vm/src/obj/objenumerate.rs @@ -4,8 +4,7 @@ use std::ops::AddAssign; use super::objint; use super::objiter; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::BigInt; @@ -37,12 +36,10 @@ fn enumerate_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { }; let iterator = objiter::get_iter(vm, iterable)?; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyEnumerate { - counter: RefCell::new(counter), - iterator, - }), - }, + Box::new(PyEnumerate { + counter: RefCell::new(counter), + iterator, + }), cls.clone(), )) } diff --git a/vm/src/obj/objfilter.rs b/vm/src/obj/objfilter.rs index 94703be57..009c7da90 100644 --- a/vm/src/obj/objfilter.rs +++ b/vm/src/obj/objfilter.rs @@ -1,6 +1,6 @@ use crate::pyobject::{ - IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, - PyResult, TypeProtocol, + IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, + TypeProtocol, }; use crate::vm::VirtualMachine; // Required for arg_check! to use isinstance @@ -27,12 +27,10 @@ fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { ); let iterator = objiter::get_iter(vm, iterable)?; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyFilter { - predicate: function.clone(), - iterator, - }), - }, + Box::new(PyFilter { + predicate: function.clone(), + iterator, + }), cls.clone(), )) } diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index 6f1790d68..efbd4fed4 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -3,8 +3,7 @@ use super::objint; use super::objstr; use super::objtype; use crate::pyobject::{ - IntoPyObject, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyRef, - PyResult, TypeProtocol, + IntoPyObject, PyContext, PyObject, PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::ToBigInt; @@ -189,12 +188,7 @@ impl PyFloatRef { let type_name = objtype::get_type_name(&arg.typ()); return Err(vm.new_type_error(format!("can't convert {} to float", type_name))); }; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyFloat { value }), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyFloat { value }), cls.clone())) } fn mod_(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult { diff --git a/vm/src/obj/objgenerator.rs b/vm/src/obj/objgenerator.rs index 1babd5c9d..4e2f40a12 100644 --- a/vm/src/obj/objgenerator.rs +++ b/vm/src/obj/objgenerator.rs @@ -4,8 +4,7 @@ use crate::frame::{ExecutionResult, Frame}; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -41,9 +40,7 @@ pub fn init(context: &PyContext) { pub fn new_generator(vm: &mut VirtualMachine, frame: PyObjectRef) -> PyResult { Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyGenerator { frame }), - }, + Box::new(PyGenerator { frame }), vm.ctx.generator_type.clone(), )) } diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 5b751faa0..4352e97c6 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -6,8 +6,8 @@ use num_traits::{Pow, Signed, ToPrimitive, Zero}; use crate::format::FormatSpec; use crate::pyobject::{ - FromPyObjectRef, IntoPyObject, PyContext, PyFuncArgs, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, TryFromObject, TypeProtocol, + FromPyObjectRef, IntoPyObject, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, + PyRef, PyResult, TryFromObject, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -105,12 +105,7 @@ fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Some(val) => to_int(vm, val, base)?, None => Zero::zero(), }; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyInt::new(val)), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyInt::new(val)), cls.clone())) } // Casting function: diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index ecabb009a..29d8dc386 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -9,8 +9,8 @@ use super::objsequence::{ use super::objstr; use super::objtype; use crate::pyobject::{ - IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, + IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, + PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; use num_traits::ToPrimitive; @@ -111,12 +111,10 @@ impl PyListRef { fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: self.into_object(), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: self.into_object(), + }), vm.ctx.iter_type(), ) } @@ -305,9 +303,7 @@ fn list_new( }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyList::from(elements)), - }, + Box::new(PyList::from(elements)), cls.into_object(), )) } diff --git a/vm/src/obj/objmap.rs b/vm/src/obj/objmap.rs index 4cbb8d3ee..eb748bc01 100644 --- a/vm/src/obj/objmap.rs +++ b/vm/src/obj/objmap.rs @@ -1,6 +1,5 @@ use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -31,12 +30,10 @@ fn map_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { .map(|iterable| objiter::get_iter(vm, iterable)) .collect::, _>>()?; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyMap { - mapper: function.clone(), - iterators, - }), - }, + Box::new(PyMap { + mapper: function.clone(), + iterators, + }), cls.clone(), )) } diff --git a/vm/src/obj/objmemory.rs b/vm/src/obj/objmemory.rs index 988abbed9..c5669532c 100644 --- a/vm/src/obj/objmemory.rs +++ b/vm/src/obj/objmemory.rs @@ -1,6 +1,5 @@ use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -19,11 +18,9 @@ pub fn new_memory_view(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(cls, None), (bytes_object, None)]); vm.ctx.set_attr(&cls, "obj", bytes_object.clone()); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyMemoryView { - obj: bytes_object.clone(), - }), - }, + Box::new(PyMemoryView { + obj: bytes_object.clone(), + }), cls.clone(), )) } diff --git a/vm/src/obj/objobject.rs b/vm/src/obj/objobject.rs index 54fcd1b6e..7a560399c 100644 --- a/vm/src/obj/objobject.rs +++ b/vm/src/obj/objobject.rs @@ -3,7 +3,7 @@ use super::objtype; use crate::obj::objproperty::PropertyBuilder; use crate::pyobject::{ AttributeProtocol, DictProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, - PyObjectPayload, PyObjectRef, PyRef, PyResult, TypeProtocol, + PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use std::cell::RefCell; @@ -25,12 +25,10 @@ pub fn create_object(type_type: PyObjectRef, object_type: PyObjectRef, _dict_typ // this is not ideal let ptr = PyObjectRef::into_raw(object_type.clone()) as *mut PyObject; unsafe { - (*ptr).payload = PyObjectPayload::AnyRustValue { - value: Box::new(objtype::PyClass { - name: String::from("object"), - mro: vec![], - }), - }; + (*ptr).payload = Box::new(objtype::PyClass { + name: String::from("object"), + mro: vec![], + }); (*ptr).dict = Some(RefCell::new(HashMap::new())); (*ptr).typ = Some(type_type.clone()); } diff --git a/vm/src/obj/objproperty.rs b/vm/src/obj/objproperty.rs index 9aaea31cd..03c012ad5 100644 --- a/vm/src/obj/objproperty.rs +++ b/vm/src/obj/objproperty.rs @@ -7,8 +7,8 @@ use std::marker::PhantomData; use crate::obj::objstr::PyStringRef; use crate::obj::objtype::PyClassRef; use crate::pyobject::{ - IntoPyNativeFunc, OptionalArg, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyRef, PyResult, + IntoPyNativeFunc, OptionalArg, PyContext, PyObject, PyObjectPayload2, PyObjectRef, PyRef, + PyResult, }; use crate::VirtualMachine; @@ -138,12 +138,7 @@ impl<'a, T> PropertyBuilder<'a, T> { deleter: None, }; - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - self.ctx.property_type(), - ) + PyObject::new(Box::new(payload), self.ctx.property_type()) } else { let payload = PyReadOnlyProperty { getter: self.getter.expect( @@ -151,12 +146,7 @@ impl<'a, T> PropertyBuilder<'a, T> { ), }; - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - self.ctx.readonly_property_type(), - ) + PyObject::new(Box::new(payload), self.ctx.readonly_property_type()) } } } diff --git a/vm/src/obj/objrange.rs b/vm/src/obj/objrange.rs index f249a1f48..3100d2351 100644 --- a/vm/src/obj/objrange.rs +++ b/vm/src/obj/objrange.rs @@ -6,8 +6,8 @@ use num_integer::Integer; use num_traits::{One, Signed, ToPrimitive, Zero}; use crate::pyobject::{ - PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, PyResult, + TypeProtocol, }; use crate::vm::VirtualMachine; @@ -229,9 +229,7 @@ fn range_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Err(vm.new_value_error("range with 0 step size".to_string())) } else { Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyRange { start, end, step }), - }, + Box::new(PyRange { start, end, step }), cls.clone(), )) } @@ -241,12 +239,10 @@ fn range_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(range, Some(vm.ctx.range_type()))]); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: range.clone(), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: range.clone(), + }), vm.ctx.iter_type(), )) } @@ -257,17 +253,10 @@ fn range_reversed(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let range = get_value(zelf).reversed(); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(range), - }, - vm.ctx.range_type(), - ), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: PyObject::new(Box::new(range), vm.ctx.range_type()), + }), vm.ctx.iter_type(), )) } @@ -330,13 +319,11 @@ fn range_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyRange { - start: new_start, - end: new_end, - step: new_step, - }), - }, + Box::new(PyRange { + start: new_start, + end: new_end, + step: new_step, + }), vm.ctx.range_type(), )) } else { diff --git a/vm/src/obj/objsequence.rs b/vm/src/obj/objsequence.rs index 5d7ddaf32..d9961b3ac 100644 --- a/vm/src/obj/objsequence.rs +++ b/vm/src/obj/objsequence.rs @@ -5,7 +5,7 @@ use std::ops::{Deref, DerefMut, Range}; use num_bigint::BigInt; use num_traits::{One, Signed, ToPrimitive, Zero}; -use crate::pyobject::{IdProtocol, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol}; +use crate::pyobject::{IdProtocol, PyObject, PyObjectRef, PyResult, TypeProtocol}; use crate::vm::VirtualMachine; use super::objbool; @@ -163,29 +163,29 @@ pub fn get_item( } if subscript.payload::().is_some() { - let payload = if sequence.payload::().is_some() { - PyObjectPayload::AnyRustValue { - value: Box::new(PyList::from( + if sequence.payload::().is_some() { + Ok(PyObject::new( + Box::new(PyList::from( elements.to_vec().get_slice_items(vm, &subscript)?, )), - } + sequence.typ(), + )) } else if sequence.payload::().is_some() { - PyObjectPayload::AnyRustValue { - value: Box::new(PyTuple::from( + Ok(PyObject::new( + Box::new(PyTuple::from( elements.to_vec().get_slice_items(vm, &subscript)?, )), - } + sequence.typ(), + )) } else { panic!("sequence get_item called for non-sequence") - }; - - return Ok(PyObject::new(payload, sequence.typ())); + } + } else { + Err(vm.new_type_error(format!( + "TypeError: indexing type {:?} with index {:?} is not supported (yet?)", + sequence, subscript + ))) } - - Err(vm.new_type_error(format!( - "TypeError: indexing type {:?} with index {:?} is not supported (yet?)", - sequence, subscript - ))) } pub fn seq_equal( diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 76eca5ec2..3f5c306a8 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -13,8 +13,8 @@ use super::objiter; use super::objstr; use super::objtype; use crate::pyobject::{ - PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, PyResult, + TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; @@ -169,11 +169,9 @@ fn set_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), cls.clone(), )) } @@ -190,11 +188,9 @@ fn set_copy(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(s, Some(vm.ctx.set_type()))]); let elements = get_elements(s); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), vm.ctx.set_type(), )) } @@ -346,11 +342,9 @@ fn set_union(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { elements.extend(get_elements(other).clone()); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), vm.ctx.set_type(), )) } @@ -390,11 +384,9 @@ fn set_symmetric_difference(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResu } Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), vm.ctx.set_type(), )) } @@ -432,11 +424,9 @@ fn set_combine_inner( } Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), vm.ctx.set_type(), )) } @@ -566,12 +556,10 @@ fn set_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let items = get_elements(zelf).values().cloned().collect(); let set_list = vm.ctx.new_list(items); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: set_list, - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: set_list, + }), vm.ctx.iter_type(), ); diff --git a/vm/src/obj/objslice.rs b/vm/src/obj/objslice.rs index 45b92b5af..f146bb062 100644 --- a/vm/src/obj/objslice.rs +++ b/vm/src/obj/objslice.rs @@ -1,7 +1,6 @@ use super::objint; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::BigInt; @@ -55,13 +54,11 @@ fn slice_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } }?; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySlice { - start: start.map(|x| objint::get_value(x)), - stop: stop.map(|x| objint::get_value(x)), - step: step.map(|x| objint::get_value(x)), - }), - }, + Box::new(PySlice { + start: start.map(|x| objint::get_value(x)), + stop: stop.map(|x| objint::get_value(x)), + step: step.map(|x| objint::get_value(x)), + }), cls.clone(), )) } diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index 95c1b12f9..9d120d271 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -2,8 +2,8 @@ use std::cell::{Cell, RefCell}; use std::hash::{Hash, Hasher}; use crate::pyobject::{ - IdProtocol, OptionalArg, PyContext, PyIteratorValue, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, + IdProtocol, OptionalArg, PyContext, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, + PyRef, PyResult, }; use crate::vm::{ReprGuard, VirtualMachine}; @@ -126,12 +126,10 @@ impl PyTupleRef { fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: self.into_object(), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: self.into_object(), + }), vm.ctx.iter_type(), ) } @@ -216,9 +214,7 @@ fn tuple_new( }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyTuple::from(elements)), - }, + Box::new(PyTuple::from(elements)), cls.into_object(), )) } diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index 44bb9bc62..a9d5e5c4f 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -2,8 +2,8 @@ use std::cell::RefCell; use std::collections::HashMap; use crate::pyobject::{ - AttributeProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, + AttributeProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, + PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -32,12 +32,10 @@ pub fn create_type(type_type: PyObjectRef, object_type: PyObjectRef, _dict_type: // this is not ideal let ptr = PyObjectRef::into_raw(type_type.clone()) as *mut PyObject; unsafe { - (*ptr).payload = PyObjectPayload::AnyRustValue { - value: Box::new(PyClass { - name: String::from("type"), - mro: vec![object_type], - }), - }; + (*ptr).payload = Box::new(PyClass { + name: String::from("type"), + mro: vec![object_type], + }); (*ptr).dict = Some(RefCell::new(PyAttributes::new())); (*ptr).typ = Some(type_type); } @@ -320,12 +318,10 @@ pub fn new( let mros = bases.into_iter().map(|x| _mro(x).unwrap()).collect(); let mro = linearise_mro(mros).unwrap(); Ok(PyObject { - payload: PyObjectPayload::AnyRustValue { - value: Box::new(PyClass { - name: String::from(name), - mro, - }), - }, + payload: Box::new(PyClass { + name: String::from(name), + mro, + }), dict: Some(RefCell::new(dict)), typ: Some(typ), } diff --git a/vm/src/obj/objzip.rs b/vm/src/obj/objzip.rs index 70cded6b2..25714fd91 100644 --- a/vm/src/obj/objzip.rs +++ b/vm/src/obj/objzip.rs @@ -1,6 +1,5 @@ use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -25,12 +24,7 @@ fn zip_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { .iter() .map(|iterable| objiter::get_iter(vm, iterable)) .collect::, _>>()?; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyZip { iterators }), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyZip { iterators }), cls.clone())) } fn zip_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index c7c99723c..c213fcda7 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1,3 +1,4 @@ +use std::any::Any; use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::fmt; @@ -213,38 +214,19 @@ impl PyContext { let exceptions = exceptions::ExceptionZoo::new(&type_type, &object_type, &dict_type); let none = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objnone::PyNone), - }, + Box::new(objnone::PyNone), create_type("NoneType", &type_type, &object_type, &dict_type), ); - let ellipsis = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(()), - }, - ellipsis_type.clone(), - ); + let ellipsis = PyObject::new(Box::new(()), ellipsis_type.clone()); let not_implemented = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(()), - }, + Box::new(()), create_type("NotImplementedType", &type_type, &object_type, &dict_type), ); - let true_value = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyInt::new(BigInt::one())), - }, - bool_type.clone(), - ); - let false_value = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyInt::new(BigInt::zero())), - }, - bool_type.clone(), - ); + let true_value = PyObject::new(Box::new(PyInt::new(BigInt::one())), bool_type.clone()); + let false_value = PyObject::new(Box::new(PyInt::new(BigInt::zero())), bool_type.clone()); let context = PyContext { bool_type, memoryview_type, @@ -482,55 +464,28 @@ impl PyContext { } pub fn new_int(&self, i: T) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyInt::new(i)), - }, - self.int_type(), - ) + PyObject::new(Box::new(PyInt::new(i)), self.int_type()) } pub fn new_float(&self, value: f64) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyFloat::from(value)), - }, - self.float_type(), - ) + PyObject::new(Box::new(PyFloat::from(value)), self.float_type()) } pub fn new_complex(&self, value: Complex64) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyComplex::from(value)), - }, - self.complex_type(), - ) + PyObject::new(Box::new(PyComplex::from(value)), self.complex_type()) } pub fn new_str(&self, s: String) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objstr::PyString { value: s }), - }, - self.str_type(), - ) + PyObject::new(Box::new(objstr::PyString { value: s }), self.str_type()) } pub fn new_bytes(&self, data: Vec) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objbytes::PyBytes::new(data)), - }, - self.bytes_type(), - ) + PyObject::new(Box::new(objbytes::PyBytes::new(data)), self.bytes_type()) } pub fn new_bytearray(&self, data: Vec) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objbytearray::PyByteArray::new(data)), - }, + Box::new(objbytearray::PyByteArray::new(data)), self.bytearray_type(), ) } @@ -544,41 +499,21 @@ impl PyContext { } pub fn new_tuple(&self, elements: Vec) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyTuple::from(elements)), - }, - self.tuple_type(), - ) + PyObject::new(Box::new(PyTuple::from(elements)), self.tuple_type()) } pub fn new_list(&self, elements: Vec) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyList::from(elements)), - }, - self.list_type(), - ) + PyObject::new(Box::new(PyList::from(elements)), self.list_type()) } pub fn new_set(&self) -> PyObjectRef { // Initialized empty, as calling __hash__ is required for adding each object to the set // which requires a VM context - this is done in the objset code itself. - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet::default()), - }, - self.set_type(), - ) + PyObject::new(Box::new(PySet::default()), self.set_type()) } pub fn new_dict(&self) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyDict::default()), - }, - self.dict_type(), - ) + PyObject::new(Box::new(PyDict::default()), self.dict_type()) } pub fn new_class(&self, name: &str, base: PyObjectRef) -> PyObjectRef { @@ -591,12 +526,10 @@ impl PyContext { pub fn new_module(&self, name: &str, dict: PyObjectRef) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyModule { - name: name.to_string(), - dict, - }), - }, + Box::new(PyModule { + name: name.to_string(), + dict, + }), self.module_type.clone(), ) } @@ -606,20 +539,13 @@ impl PyContext { F: IntoPyNativeFunc, { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyBuiltinFunction::new(f.into_func())), - }, + Box::new(PyBuiltinFunction::new(f.into_func())), self.builtin_function_or_method_type(), ) } pub fn new_frame(&self, code: PyObjectRef, scope: Scope) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(Frame::new(code, scope)), - }, - self.frame_type(), - ) + PyObject::new(Box::new(Frame::new(code, scope)), self.frame_type()) } pub fn new_property(&self, f: F) -> PyObjectRef @@ -630,12 +556,7 @@ impl PyContext { } pub fn new_code_object(&self, code: bytecode::CodeObject) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objcode::PyCode::new(code)), - }, - self.code_type(), - ) + PyObject::new(Box::new(objcode::PyCode::new(code)), self.code_type()) } pub fn new_function( @@ -645,18 +566,14 @@ impl PyContext { defaults: PyObjectRef, ) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyFunction::new(code_obj, scope, defaults)), - }, + Box::new(PyFunction::new(code_obj, scope, defaults)), self.function_type(), ) } pub fn new_bound_method(&self, function: PyObjectRef, object: PyObjectRef) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyMethod::new(object, function)), - }, + Box::new(PyMethod::new(object, function)), self.bound_method_type(), ) } @@ -668,11 +585,9 @@ impl PyContext { PyAttributes::new() }; PyObject { - payload: PyObjectPayload::AnyRustValue { - value: Box::new(objobject::PyInstance), - }, typ: Some(class), dict: Some(RefCell::new(dict)), + payload: Box::new(objobject::PyInstance), } .into_ref() } @@ -735,11 +650,20 @@ impl Default for PyContext { /// This is an actual python object. It consists of a `typ` which is the /// python class, and carries some rust payload optionally. This rust /// payload can be a rust float or rust int in case of float and int objects. -#[derive(Default)] pub struct PyObject { - pub payload: PyObjectPayload, pub typ: Option, pub dict: Option>, // __dict__ member + pub payload: Box, +} + +impl Default for PyObject { + fn default() -> Self { + PyObject { + typ: None, + dict: None, + payload: Box::new(()), + } + } } /// A reference to a Python object. @@ -764,12 +688,7 @@ where { pub fn new(ctx: &PyContext, payload: T) -> Self { PyRef { - obj: PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - T::required_type(ctx), - ), + obj: PyObject::new(Box::new(payload), T::required_type(ctx)), _payload: PhantomData, } } @@ -778,12 +697,7 @@ where let required_type = T::required_type(&vm.ctx); if objtype::issubclass(&cls.obj, &required_type) { Ok(PyRef { - obj: PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - cls.obj, - ), + obj: PyObject::new(Box::new(payload), cls.obj), _payload: PhantomData, }) } else { @@ -1452,12 +1366,7 @@ where T: PyObjectPayload2 + Sized, { fn into_pyobject(self, ctx: &PyContext) -> PyResult { - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(self), - }, - T::required_type(ctx), - )) + Ok(PyObject::new(Box::new(self), T::required_type(ctx))) } } @@ -1587,22 +1496,6 @@ into_py_native_func_tuple!((a, A), (b, B), (c, C)); into_py_native_func_tuple!((a, A), (b, B), (c, C), (d, D)); into_py_native_func_tuple!((a, A), (b, B), (c, C), (d, D), (e, E)); -/// Rather than determining the type of a python object, this enum is more -/// a holder for the rust payload of a python object. It is more a carrier -/// of rust data for a particular python object. Determine the python type -/// by using for example the `.typ()` method on a python object. -pub enum PyObjectPayload { - AnyRustValue { value: Box }, -} - -impl Default for PyObjectPayload { - fn default() -> Self { - PyObjectPayload::AnyRustValue { - value: Box::new(()), - } - } -} - // TODO: This is a workaround and shouldn't exist. // Each iterable type should have its own distinct iterator type. #[derive(Debug)] @@ -1617,16 +1510,8 @@ impl PyObjectPayload2 for PyIteratorValue { } } -impl fmt::Debug for PyObjectPayload { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - PyObjectPayload::AnyRustValue { value } => value.fmt(f), - } - } -} - impl PyObject { - pub fn new(payload: PyObjectPayload, typ: PyObjectRef) -> PyObjectRef { + pub fn new(payload: Box, typ: PyObjectRef) -> PyObjectRef { PyObject { payload, typ: Some(typ), @@ -1641,8 +1526,7 @@ impl PyObject { } pub fn payload(&self) -> Option<&T> { - let PyObjectPayload::AnyRustValue { ref value } = self.payload; - value.downcast_ref() + self.payload.downcast_ref() } } diff --git a/vm/src/stdlib/re.rs b/vm/src/stdlib/re.rs index 0828a13d1..bf24d7b51 100644 --- a/vm/src/stdlib/re.rs +++ b/vm/src/stdlib/re.rs @@ -11,9 +11,7 @@ use regex::{Match, Regex}; use std::path::PathBuf; use crate::obj::objstr; -use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol, -}; +use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, TypeProtocol}; use crate::VirtualMachine; /// Create the python `re` module with all its members. @@ -118,12 +116,7 @@ fn create_match(vm: &mut VirtualMachine, match_value: &Match) -> PyResult { end: match_value.end(), }; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(match_value), - }, - match_class.clone(), - )) + Ok(PyObject::new(Box::new(match_value), match_class.clone())) } /// Compile a regular expression into a Pattern object. @@ -141,12 +134,7 @@ fn re_compile(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let module = import::import_module(vm, PathBuf::default(), "re").unwrap(); let pattern_class = vm.ctx.get_attr(&module, "Pattern").unwrap(); - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(regex), - }, - pattern_class.clone(), - )) + Ok(PyObject::new(Box::new(regex), pattern_class.clone())) } fn pattern_match(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -192,8 +180,7 @@ fn match_end(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { /// Retrieve inner rust regex from python object: fn get_regex<'a>(obj: &'a PyObjectRef) -> &'a Regex { // TODO: Regex shouldn't be stored in payload directly, create newtype wrapper - let PyObjectPayload::AnyRustValue { ref value } = obj.payload; - if let Some(regex) = value.downcast_ref::() { + if let Some(regex) = obj.payload.downcast_ref::() { return regex; } panic!("Inner error getting regex {:?}", obj); @@ -201,8 +188,7 @@ fn get_regex<'a>(obj: &'a PyObjectRef) -> &'a Regex { /// Retrieve inner rust match from python object: fn get_match<'a>(obj: &'a PyObjectRef) -> &'a PyMatch { - let PyObjectPayload::AnyRustValue { ref value } = obj.payload; - if let Some(value) = value.downcast_ref::() { + if let Some(value) = obj.payload.downcast_ref::() { return value; } panic!("Inner error getting match {:?}", obj); diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index d6e6283af..b4f1c61b3 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -9,9 +9,7 @@ use crate::obj::objbytes; use crate::obj::objint; use crate::obj::objsequence::get_elements; use crate::obj::objstr; -use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol, -}; +use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, TypeProtocol}; use crate::vm::VirtualMachine; use num_traits::ToPrimitive; @@ -127,8 +125,7 @@ impl Socket { } fn get_socket<'a>(obj: &'a PyObjectRef) -> impl DerefMut + 'a { - let PyObjectPayload::AnyRustValue { ref value } = obj.payload; - if let Some(socket) = value.downcast_ref::>() { + if let Some(socket) = obj.payload.downcast_ref::>() { return socket.borrow_mut(); } panic!("Inner error getting socket {:?}", obj); @@ -151,12 +148,7 @@ fn socket_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let socket = RefCell::new(Socket::new(address_family, kind)); - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(socket), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(socket), cls.clone())) } fn socket_connect(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -274,12 +266,7 @@ fn socket_accept(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { con: Some(Connection::TcpStream(tcp_stream)), }); - let sock_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(socket), - }, - zelf.typ(), - ); + let sock_obj = PyObject::new(Box::new(socket), zelf.typ()); let addr_tuple = get_addr_tuple(vm, addr)?;