From 7f6cdc68c4f4ff40613ee5131255dfd2c84bcac7 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Thu, 20 Feb 2020 23:08:37 -0600 Subject: [PATCH] Add struct.Struct.size property --- vm/src/stdlib/pystruct.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/vm/src/stdlib/pystruct.rs b/vm/src/stdlib/pystruct.rs index abba7398ba..568d54c4b8 100644 --- a/vm/src/stdlib/pystruct.rs +++ b/vm/src/stdlib/pystruct.rs @@ -120,6 +120,10 @@ impl FormatSpec { Ok(PyTuple::from(items)) } + + fn size(&self) -> usize { + self.codes.iter().map(FormatCode::size).sum() + } } /// Parse endianness @@ -413,7 +417,7 @@ where fn struct_calcsize(fmt: PyStringRef, vm: &VirtualMachine) -> PyResult { let fmt_str = fmt.as_str(); let format_spec = FormatSpec::parse(fmt_str).map_err(|e| vm.new_value_error(e))?; - Ok(format_spec.codes.iter().map(|code| code.size()).sum()) + Ok(format_spec.size()) } #[pyclass(name = "Struct")] @@ -442,6 +446,10 @@ impl PyStruct { fn format(&self) -> PyStringRef { self.fmt_str.clone() } + #[pyproperty] + fn size(&self) -> usize { + self.spec.size() + } #[pymethod] fn pack(&self, args: Args, vm: &VirtualMachine) -> PyResult> {