Skip to content

Specify bit width and endianness for ints as constraints #1031

@zyv

Description

@zyv

Description

Currently, I have to put the endianness and bit width for integer fields into my encoder and decoder.

This is suboptimal because it gets spread over two places (encoder and decoder), and I have to write extra code for this.

It would be fantastic if msgspec supported annotations for bit width and endianness. This way, I could do something like:

class Payload(Struct):
  idx: Annotated[int, length=4, byteorder="little"]
  number: Annotated[int, length=8, byteorder="big"]

p = Payload(idx=1, number=42)

assert msgspec.msgpack.decode(msgspec.msgpack.encode(p)) == p  # pure magic 

Thanks for considering!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions