Skip to content

Obtaining size of struct (after allocation) #9

Description

@nb-programmer

It would be really nice to be able to obtain the size of the internal struct object in bytes. For example, to access the correct number of bytes to read from a buffered reader or socket.

A getter or @property would be great
... or ...
the __len__ dunder currently returns the size of the internal buffer used to parse the struct. But I think it would be more useful to know the size of the dataclass in bytes using this.

Example usage

@dataclass
class MyPacket:
   magic: Annotated[bytes, 4]

packet_data = b'...'  # Just some data stream
buff_file = BytesIO(packet_data)

reader = Reader[MyPacket]().allocate()

reader.feed(buff_file.read(len(reader)))  # Something like this. len(reader) = 4 in this case
reader.feed(buff_file.read(reader.size))  # or this would be fine as well

packet = reader.build()

...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions