Skip to content

Commit 053ceb1

Browse files
Move payload boxing into PyObject::new
1 parent 4510489 commit 053ceb1

25 files changed

+210
-181
lines changed

vm/src/compile.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ pub fn compile(
4949

5050
let code = compiler.pop_code_object();
5151
trace!("Compilation completed: {:?}", code);
52-
Ok(PyObject::new(
53-
Box::new(objcode::PyCode::new(code)),
54-
code_type,
55-
))
52+
Ok(PyObject::new(objcode::PyCode::new(code), code_type))
5653
}
5754

5855
pub enum Mode {

vm/src/frame.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ impl Frame {
407407
let stop = out[1].take();
408408
let step = if out.len() == 3 { out[2].take() } else { None };
409409

410-
let obj =
411-
PyObject::new(Box::new(PySlice { start, stop, step }), vm.ctx.slice_type());
410+
let obj = PyObject::new(PySlice { start, stop, step }, vm.ctx.slice_type());
412411
self.push_value(obj);
413412
Ok(None)
414413
}
@@ -702,9 +701,7 @@ impl Frame {
702701
}
703702
bytecode::Instruction::LoadBuildClass => {
704703
let rustfunc = PyObject::new(
705-
Box::new(PyBuiltinFunction::new(Box::new(
706-
builtins::builtin_build_class_,
707-
))),
704+
PyBuiltinFunction::new(Box::new(builtins::builtin_build_class_)),
708705
vm.ctx.type_type(),
709706
);
710707
self.push_value(rustfunc);

vm/src/obj/objbytearray.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172172
} else {
173173
vec![]
174174
};
175-
Ok(PyObject::new(
176-
Box::new(PyByteArray::new(value)),
177-
cls.clone(),
178-
))
175+
Ok(PyObject::new(PyByteArray::new(value), cls.clone()))
179176
}
180177

181178
fn bytesarray_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/obj/objbytes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
9494
vec![]
9595
};
9696

97-
Ok(PyObject::new(Box::new(PyBytes::new(value)), cls.clone()))
97+
Ok(PyObject::new(PyBytes::new(value), cls.clone()))
9898
}
9999

100100
fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -203,10 +203,10 @@ fn bytes_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
203203
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytes_type()))]);
204204

205205
let iter_obj = PyObject::new(
206-
Box::new(PyIteratorValue {
206+
PyIteratorValue {
207207
position: Cell::new(0),
208208
iterated_obj: obj.clone(),
209-
}),
209+
},
210210
vm.ctx.iter_type(),
211211
);
212212

vm/src/obj/objcomplex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn complex_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8989

9090
let value = Complex64::new(real, imag);
9191

92-
Ok(PyObject::new(Box::new(PyComplex { value }), cls.clone()))
92+
Ok(PyObject::new(PyComplex { value }, cls.clone()))
9393
}
9494

9595
fn complex_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/obj/objdict.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
251251
let key_list = vm.ctx.new_list(keys);
252252

253253
let iter_obj = PyObject::new(
254-
Box::new(PyIteratorValue {
254+
PyIteratorValue {
255255
position: Cell::new(0),
256256
iterated_obj: key_list,
257-
}),
257+
},
258258
vm.ctx.iter_type(),
259259
);
260260

@@ -271,10 +271,10 @@ fn dict_values(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
271271
let values_list = vm.ctx.new_list(values);
272272

273273
let iter_obj = PyObject::new(
274-
Box::new(PyIteratorValue {
274+
PyIteratorValue {
275275
position: Cell::new(0),
276276
iterated_obj: values_list,
277-
}),
277+
},
278278
vm.ctx.iter_type(),
279279
);
280280

@@ -291,10 +291,10 @@ fn dict_items(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
291291
let items_list = vm.ctx.new_list(items);
292292

293293
let iter_obj = PyObject::new(
294-
Box::new(PyIteratorValue {
294+
PyIteratorValue {
295295
position: Cell::new(0),
296296
iterated_obj: items_list,
297-
}),
297+
},
298298
vm.ctx.iter_type(),
299299
);
300300

vm/src/obj/objenumerate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ fn enumerate_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3636
};
3737
let iterator = objiter::get_iter(vm, iterable)?;
3838
Ok(PyObject::new(
39-
Box::new(PyEnumerate {
39+
PyEnumerate {
4040
counter: RefCell::new(counter),
4141
iterator,
42-
}),
42+
},
4343
cls.clone(),
4444
))
4545
}

vm/src/obj/objfilter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2626
);
2727
let iterator = objiter::get_iter(vm, iterable)?;
2828
Ok(PyObject::new(
29-
Box::new(PyFilter {
29+
PyFilter {
3030
predicate: function.clone(),
3131
iterator,
32-
}),
32+
},
3333
cls.clone(),
3434
))
3535
}

vm/src/obj/objfloat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl PyFloatRef {
188188
let type_name = objtype::get_type_name(&arg.typ());
189189
return Err(vm.new_type_error(format!("can't convert {} to float", type_name)));
190190
};
191-
Ok(PyObject::new(Box::new(PyFloat { value }), cls.clone()))
191+
Ok(PyObject::new(PyFloat { value }, cls.clone()))
192192
}
193193

194194
fn mod_(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {

vm/src/obj/objgenerator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn init(context: &PyContext) {
4040

4141
pub fn new_generator(vm: &mut VirtualMachine, frame: PyObjectRef) -> PyResult {
4242
Ok(PyObject::new(
43-
Box::new(PyGenerator { frame }),
43+
PyGenerator { frame },
4444
vm.ctx.generator_type.clone(),
4545
))
4646
}

vm/src/obj/objint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
105105
Some(val) => to_int(vm, val, base)?,
106106
None => Zero::zero(),
107107
};
108-
Ok(PyObject::new(Box::new(PyInt::new(val)), cls.clone()))
108+
Ok(PyObject::new(PyInt::new(val), cls.clone()))
109109
}
110110

111111
// Casting function:

vm/src/obj/objlist.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ impl PyListRef {
111111

112112
fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef {
113113
PyObject::new(
114-
Box::new(PyIteratorValue {
114+
PyIteratorValue {
115115
position: Cell::new(0),
116116
iterated_obj: self.into_object(),
117-
}),
117+
},
118118
vm.ctx.iter_type(),
119119
)
120120
}
@@ -302,10 +302,7 @@ fn list_new(
302302
vec![]
303303
};
304304

305-
Ok(PyObject::new(
306-
Box::new(PyList::from(elements)),
307-
cls.into_object(),
308-
))
305+
Ok(PyObject::new(PyList::from(elements), cls.into_object()))
309306
}
310307

311308
fn quicksort(

vm/src/obj/objmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ fn map_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3030
.map(|iterable| objiter::get_iter(vm, iterable))
3131
.collect::<Result<Vec<_>, _>>()?;
3232
Ok(PyObject::new(
33-
Box::new(PyMap {
33+
PyMap {
3434
mapper: function.clone(),
3535
iterators,
36-
}),
36+
},
3737
cls.clone(),
3838
))
3939
}

vm/src/obj/objmemory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ pub fn new_memory_view(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1818
arg_check!(vm, args, required = [(cls, None), (bytes_object, None)]);
1919
vm.ctx.set_attr(&cls, "obj", bytes_object.clone());
2020
Ok(PyObject::new(
21-
Box::new(PyMemoryView {
21+
PyMemoryView {
2222
obj: bytes_object.clone(),
23-
}),
23+
},
2424
cls.clone(),
2525
))
2626
}

vm/src/obj/objproperty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ impl<'a, T> PropertyBuilder<'a, T> {
137137
deleter: None,
138138
};
139139

140-
PyObject::new(Box::new(payload), self.ctx.property_type())
140+
PyObject::new(payload, self.ctx.property_type())
141141
} else {
142142
let payload = PyReadOnlyProperty {
143143
getter: self.getter.expect(
144144
"One of add_getter/add_setter must be called when constructing a property",
145145
),
146146
};
147147

148-
PyObject::new(Box::new(payload), self.ctx.readonly_property_type())
148+
PyObject::new(payload, self.ctx.readonly_property_type())
149149
}
150150
}
151151
}

vm/src/obj/objrange.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,18 @@ fn range_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
227227
if step.is_zero() {
228228
Err(vm.new_value_error("range with 0 step size".to_string()))
229229
} else {
230-
Ok(PyObject::new(
231-
Box::new(PyRange { start, end, step }),
232-
cls.clone(),
233-
))
230+
Ok(PyObject::new(PyRange { start, end, step }, cls.clone()))
234231
}
235232
}
236233

237234
fn range_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
238235
arg_check!(vm, args, required = [(range, Some(vm.ctx.range_type()))]);
239236

240237
Ok(PyObject::new(
241-
Box::new(PyIteratorValue {
238+
PyIteratorValue {
242239
position: Cell::new(0),
243240
iterated_obj: range.clone(),
244-
}),
241+
},
245242
vm.ctx.iter_type(),
246243
))
247244
}
@@ -252,10 +249,10 @@ fn range_reversed(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
252249
let range = get_value(zelf).reversed();
253250

254251
Ok(PyObject::new(
255-
Box::new(PyIteratorValue {
252+
PyIteratorValue {
256253
position: Cell::new(0),
257-
iterated_obj: PyObject::new(Box::new(range), vm.ctx.range_type()),
258-
}),
254+
iterated_obj: PyObject::new(range, vm.ctx.range_type()),
255+
},
259256
vm.ctx.iter_type(),
260257
))
261258
}
@@ -318,11 +315,11 @@ fn range_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
318315
};
319316

320317
Ok(PyObject::new(
321-
Box::new(PyRange {
318+
PyRange {
322319
start: new_start,
323320
end: new_end,
324321
step: new_step,
325-
}),
322+
},
326323
vm.ctx.range_type(),
327324
))
328325
} else {

vm/src/obj/objsequence.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,12 @@ pub fn get_item(
165165
if subscript.payload::<PySlice>().is_some() {
166166
if sequence.payload::<PyList>().is_some() {
167167
Ok(PyObject::new(
168-
Box::new(PyList::from(
169-
elements.to_vec().get_slice_items(vm, &subscript)?,
170-
)),
168+
PyList::from(elements.to_vec().get_slice_items(vm, &subscript)?),
171169
sequence.typ(),
172170
))
173171
} else if sequence.payload::<PyTuple>().is_some() {
174172
Ok(PyObject::new(
175-
Box::new(PyTuple::from(
176-
elements.to_vec().get_slice_items(vm, &subscript)?,
177-
)),
173+
PyTuple::from(elements.to_vec().get_slice_items(vm, &subscript)?),
178174
sequence.typ(),
179175
))
180176
} else {

vm/src/obj/objset.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ fn set_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
168168
};
169169

170170
Ok(PyObject::new(
171-
Box::new(PySet {
171+
PySet {
172172
elements: RefCell::new(elements),
173-
}),
173+
},
174174
cls.clone(),
175175
))
176176
}
@@ -187,9 +187,9 @@ fn set_copy(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
187187
arg_check!(vm, args, required = [(s, Some(vm.ctx.set_type()))]);
188188
let elements = get_elements(s);
189189
Ok(PyObject::new(
190-
Box::new(PySet {
190+
PySet {
191191
elements: RefCell::new(elements),
192-
}),
192+
},
193193
vm.ctx.set_type(),
194194
))
195195
}
@@ -341,9 +341,9 @@ fn set_union(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
341341
elements.extend(get_elements(other).clone());
342342

343343
Ok(PyObject::new(
344-
Box::new(PySet {
344+
PySet {
345345
elements: RefCell::new(elements),
346-
}),
346+
},
347347
vm.ctx.set_type(),
348348
))
349349
}
@@ -383,9 +383,9 @@ fn set_symmetric_difference(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResu
383383
}
384384

385385
Ok(PyObject::new(
386-
Box::new(PySet {
386+
PySet {
387387
elements: RefCell::new(elements),
388-
}),
388+
},
389389
vm.ctx.set_type(),
390390
))
391391
}
@@ -423,9 +423,9 @@ fn set_combine_inner(
423423
}
424424

425425
Ok(PyObject::new(
426-
Box::new(PySet {
426+
PySet {
427427
elements: RefCell::new(elements),
428-
}),
428+
},
429429
vm.ctx.set_type(),
430430
))
431431
}
@@ -555,10 +555,10 @@ fn set_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
555555
let items = get_elements(zelf).values().cloned().collect();
556556
let set_list = vm.ctx.new_list(items);
557557
let iter_obj = PyObject::new(
558-
Box::new(PyIteratorValue {
558+
PyIteratorValue {
559559
position: Cell::new(0),
560560
iterated_obj: set_list,
561-
}),
561+
},
562562
vm.ctx.iter_type(),
563563
);
564564

vm/src/obj/objslice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ fn slice_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5454
}
5555
}?;
5656
Ok(PyObject::new(
57-
Box::new(PySlice {
57+
PySlice {
5858
start: start.map(|x| objint::get_value(x)),
5959
stop: stop.map(|x| objint::get_value(x)),
6060
step: step.map(|x| objint::get_value(x)),
61-
}),
61+
},
6262
cls.clone(),
6363
))
6464
}

0 commit comments

Comments
 (0)