Add struct.Struct.size property

This commit is contained in:
Noah
2020-02-20 23:08:37 -06:00
parent e1b63bdd4a
commit 7f6cdc68c4

View File

@@ -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<usize> {
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<Vec<u8>> {