forked from Rust-related/RustPython
Add struct.Struct.size property
This commit is contained in:
@@ -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>> {
|
||||
|
||||
Reference in New Issue
Block a user