Skip to main content

rustruct

Tests Latest Version Python Versions License

A Rust core for parsing and building binary wire formats from Python.

A declarative schema built from Python primitives is compiled into a program executed with a single Python -> Rust call for the whole structure. The parse result is a plain dict.

Building and testing

# Rust core (no Python)
cargo test -p rustruct-core

# Python package (maturin backend) + pytest
uv sync
uv run pytest

# Sphinx documentation (warnings are errors)
make test-docs

Example

from rustruct import Field, Incomplete, compile

codec = compile(
    (
        Field(name="tag", kind="u8", opts={}),
        Field(name="size", kind="u8", opts={}),
        Field(
            name="value",
            kind="struct",
            opts={
                "fields": (Field(name="payload", kind="bytes", opts={"len": "*"}),),
                "size": ("ref", "size"),
            },
        ),
        Field(name="crc", kind="digest", opts={"algo": "crc32", "over": "*"}),
    )
)

data = codec.pack({"tag": 7, "value": {"payload": b"abc"}})
# size and crc are derived: computed and patched in automatically

values = codec.unpack(data)  # full buffer, a tail is an error
values, pos = codec.unpack_from(data, 0)  # a tail is allowed

r = codec.parse(data[:3])  # streaming parse
if not r:  # Incomplete: not enough data yet
    print("need at least", r.needed, "more bytes")

Documentation

The full documentation is at mosquito.github.io/rustruct and is organised by user need: a guided tutorial, task-oriented how-to guides, explanations of the schema and execution model, and concise API reference.

Declarative frontend

A Struct metaclass on top of compile()/Codec: class-body annotations and field descriptors (described, slice, array, switch, bits, convert, sized, an open registry()) compile lazily, per class, into a Codec, and convert to/from typed instances rather than plain dicts.

from rustruct import Struct, U8, U16, switch, slice, registry

Payload = Struct  # a registry base: class Foo(Payload, registry=True): ...

See src/rustruct/protocols/{inet,tcp,udp,dns}.py for a full worked example (IPv4/TCP/UDP header dispatch via an open registry, DNS with hand-written name-compression as the one piece that doesn't fit the declarative model) and tests/test_frontend_*.py, tests/test_protocols_*.py for the tests.

Repository layout

crates/rustruct/       core (rustruct-core): schema compiler, unpack/pack,
                       expressions, bits, flags, windows, digests; zero pyo3
crates/rustruct-py/    pyo3 cdylib -> the rustruct.core module
src/rustruct/          Python wrapper: low-level re-export, __abi__ check,
                       the Struct frontend (struct.py/fields.py/scalars.py/
                       expr.py), and protocols/ (IPv4/TCP/UDP/DNS example)
tests/                 pytest tests: public API, frontend, protocols
docs/                  tutorials, how-to guides, explanation, API reference
crates/rustruct/tests/ Rust integration tests, one file per feature, plus
                       tests/common/mod.rs for shared helpers
benchmark/             a separate uv project comparing rustruct against
                       struct/ctypes/dataclasses-struct/construct

Implementation status

Covered:

  • all fixed-width types, raw, bytes/str/cstr, bits (MSB-first), flags (keep/strict/ignore), struct (including size windows), array (count/until_eof), switch, digest (CRC presets with Rocksoft overrides, md5/sha1/sha256, over as a name tuple or "*" with self-zeroing);
  • expressions (the full operator set), lexical backward-only ref resolution, registers and span registers, limits (max, max_count, depth 64, an 8-deep Expr stack);
  • pack with backpatching: derived lengths (linear inversion a*x + b, including derived bits fields), digests computed innermost-first, consistency checks across multiple consumers of one register;
  • the streaming contract: parse -> Incomplete.needed with monotonic progress, distinguishing "hit a window boundary" (Invalid) from "ran out of buffer" (Incomplete);
  • errors carry kind/path/offset; the path is assembled only while unwinding;
  • coalescing of fixed fields (a static schema lowers to exactly one Fixed op, enforced by a test), min_size/static_size.

Known gaps and limitations:

  • to_bytes/from_bytes (Program serialization) raise NotImplementedError; the cache format hasn't been designed.
  • A switch discriminant is never derived from its own on= ref at pack time: the caller always supplies it explicitly, and a field cannot be both derived and a switch discriminant (SchemaError).
  • byteorder accepts "network" as a struct-module-style alias for "big". "native" is forbidden, since it would make the wire format depend on the machine running the code.
  • String encodings in the core: utf-8 / ascii / latin-1, errors="strict" only. Other CPython codecs are a frontend concern; the core currently raises SchemaError for anything else.
  • An extra digest.algo="ip" preset (RFC 1071, the IPv4 header checksum) is needed for the bundled IPv4 reference format.
  • No cargo-fuzz targets and no refcount tests yet; deterministic and randomized round-trip tests stand in for a proper proptest-based fuzz suite.
  • The hot path still builds an intermediate Value tree (the core stays Python-free); raw FFI and METH_FASTCALL benchmarking haven't been done.

Metadata

Release files for rustruct 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rustruct 0.1.7
File Size Uploaded
rustruct-0.1.7.tar.gz 109.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for rustruct 0.1.7
File
rustruct-0.1.7-cp311-abi3-win_arm64.whl CPython 3.11 abi3 Windows ARM64 Details
rustruct-0.1.7-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details
rustruct-0.1.7-cp311-abi3-musllinux_1_2_x86_64.whl CPython 3.11 abi3 Linux musl 1.2+ x86-64 Details
rustruct-0.1.7-cp311-abi3-musllinux_1_2_aarch64.whl CPython 3.11 abi3 Linux musl 1.2+ ARM64 Details
rustruct-0.1.7-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details
rustruct-0.1.7-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details
rustruct-0.1.7-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl CPython 3.11 abi3 macOS 11.0+ ARM64, macOS 10.12+ x86-64, macOS 10.12+ universal2 (ARM64, x86-64) Details

Total release size: 3.9 MB

Release files / rustruct-0.1.7.tar.gz

Download URL rustruct-0.1.7.tar.gz
Size 109.6 kB
Tags Source
SHA-256 checksum
How to use checksums
09fe3ea390ba5db5b1a0219f0fab301561f744c364363a82333a3f5d90e539e1
BLAKE2b-256 checksum
How to use checksums
fd82ca74f9594fd41e18acb63f5880a4dcff784601b5466f76e26de4f63cdb20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Release files / rustruct-0.1.7-cp311-abi3-win_arm64.whl

Download URL rustruct-0.1.7-cp311-abi3-win_arm64.whl
Size 332.1 kB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
4e29fdd64b1f09e4cbaf7cbc75a1b5669a44a47ab8736c3de27ff1a4bf235b7f
BLAKE2b-256 checksum
How to use checksums
be61c78a8c32fb63562a805e49d58df651c8592f3273c4181e95897016535ae8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Release files / rustruct-0.1.7-cp311-abi3-win_amd64.whl

Download URL rustruct-0.1.7-cp311-abi3-win_amd64.whl
Size 345.1 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
52adc10e394d207322d6e15bf4122175dab5c795677696ea1e333e4abecf0b01
BLAKE2b-256 checksum
How to use checksums
cdf877ba02f783ef1fec33a82c6346aa353e68869f0f14d1923c778a3f38f06b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Release files / rustruct-0.1.7-cp311-abi3-musllinux_1_2_x86_64.whl

Download URL rustruct-0.1.7-cp311-abi3-musllinux_1_2_x86_64.whl
Size 696.4 kB
Tags CPython 3.11 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
c14a682fb31471c04d62dbd4379e800a6ac406d7ce1e297b6ab7b3efb5b30700
BLAKE2b-256 checksum
How to use checksums
4baa82d22909728b04712366c74d9baed8aa6a2c305316c5ae6db2e4baf0f085
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Release files / rustruct-0.1.7-cp311-abi3-musllinux_1_2_aarch64.whl

Download URL rustruct-0.1.7-cp311-abi3-musllinux_1_2_aarch64.whl
Size 654.0 kB
Tags CPython 3.11 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
ccee6d688adb8c042dddbc1af23390401edd998d7724f6bdc967090457992d4a
BLAKE2b-256 checksum
How to use checksums
d1aa57c5a7f0f53d8c17b8daaea49456c20509bceb63936a623773950d0f6b25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Release files / rustruct-0.1.7-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL rustruct-0.1.7-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 481.5 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
31772960adda107a28682cd0e58666e5db119cbc655cdb9f26ca1e024d2e7ba0
BLAKE2b-256 checksum
How to use checksums
f481111846fbe707289172105a9010ce6cc8696963bbd6b66fb041a89e278431
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Release files / rustruct-0.1.7-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL rustruct-0.1.7-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 476.3 kB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
8cd8e86e6f3cc3b208f7d3470e9fb8653e35da7f2723ccece60dd34cb37c57f7
BLAKE2b-256 checksum
How to use checksums
7cbae0481f13590c46873349a0747cccf382f538b8731c0e1a3a997dec6b103e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Release files / rustruct-0.1.7-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Download URL rustruct-0.1.7-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 835.0 kB
Tags CPython 3.11 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
95d54436d33590192710061ef83cb49dc14cfc82e1ff5ce3f6beebf1eed4f9a4
BLAKE2b-256 checksum
How to use checksums
df55fbed80ce674764350614816ec36a04f5820330b2294448bc5ddf8b2ab120
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Release history Release notifications | RSS feed

0.2.0

8 release files

0.1.9

8 release files

0.1.8

8 release files

This release

0.1.7 This release

8 release files

0.1.6

8 release files

0.1.5

8 release files

0.1.4

7 release files

0.1.3

8 release files

0.1.1

8 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page