Skip to content

Commit 7f6cdc6

Browse files
committed
Add struct.Struct.size property
1 parent e1b63bd commit 7f6cdc6

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

vm/src/stdlib/pystruct.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ impl FormatSpec {
120120

121121
Ok(PyTuple::from(items))
122122
}
123+
124+
fn size(&self) -> usize {
125+
self.codes.iter().map(FormatCode::size).sum()
126+
}
123127
}
124128

125129
/// Parse endianness
@@ -413,7 +417,7 @@ where
413417
fn struct_calcsize(fmt: PyStringRef, vm: &VirtualMachine) -> PyResult<usize> {
414418
let fmt_str = fmt.as_str();
415419
let format_spec = FormatSpec::parse(fmt_str).map_err(|e| vm.new_value_error(e))?;
416-
Ok(format_spec.codes.iter().map(|code| code.size()).sum())
420+
Ok(format_spec.size())
417421
}
418422

419423
#[pyclass(name = "Struct")]
@@ -442,6 +446,10 @@ impl PyStruct {
442446
fn format(&self) -> PyStringRef {
443447
self.fmt_str.clone()
444448
}
449+
#[pyproperty]
450+
fn size(&self) -> usize {
451+
self.spec.size()
452+
}
445453

446454
#[pymethod]
447455
fn pack(&self, args: Args, vm: &VirtualMachine) -> PyResult<Vec<u8>> {

0 commit comments

Comments
 (0)