Skip to content

Commit 320ed26

Browse files
committed
marshal _dumps to take &mut vec
1 parent 5b1e92f commit 320ed26

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

vm/src/stdlib/marshal.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ mod decl {
1818
/// PyBytes: Currently getting recursion error with match_class!
1919
use ascii::AsciiStr;
2020
use num_bigint::{BigInt, Sign};
21-
use std::ops::Deref;
22-
use std::slice::Iter;
21+
use std::{ops::Deref, slice::Iter};
2322

2423
const STR_BYTE: u8 = b's';
2524
const INT_BYTE: u8 = b'i';
@@ -49,15 +48,16 @@ mod decl {
4948
let mut byte_list = size_to_bytes(pyobjs.len(), vm)?.to_vec();
5049
// For each element, dump into binary, then add its length and value.
5150
for element in pyobjs {
52-
let element_bytes: Vec<u8> = _dumps(element.clone(), vm)?;
51+
let mut element_bytes = Vec::new();
52+
_dumps(&mut element_bytes, element.clone(), vm)?;
5353
byte_list.extend(size_to_bytes(element_bytes.len(), vm)?);
5454
byte_list.extend(element_bytes)
5555
}
5656
Ok(byte_list)
5757
}
5858

5959
/// Dumping helper function to turn a value into bytes.
60-
fn _dumps(value: PyObjectRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
60+
fn _dumps(buf: &mut Vec<u8>, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
6161
let r = match_class!(match value {
6262
pyint @ PyInt => {
6363
if pyint.class().is(vm.ctx.types.bool_type) {
@@ -138,12 +138,15 @@ mod decl {
138138
));
139139
}
140140
});
141+
buf.extend(r.as_slice());
141142
Ok(r)
142143
}
143144

144145
#[pyfunction]
145146
fn dumps(value: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyBytes> {
146-
Ok(PyBytes::from(_dumps(value, vm)?))
147+
let mut buf = Vec::new();
148+
_dumps(&mut buf, value, vm)?;
149+
Ok(PyBytes::from(buf))
147150
}
148151

149152
#[pyfunction]

0 commit comments

Comments
 (0)