Rust Reference Implementation Requirements — Hurray Implementation Requirements
Overview
The Rust reference implementation is the canonical implementation of the Hurray format. It is authoritative: when the spec is ambiguous, the reference implementation defines the correct behaviour. When the implementation deviates from the spec, the implementation is wrong.
The implementation is split across three crates:
| Crate | Responsibility |
|---|---|
hurray-core | Format types, tensor descriptor, buffer handle, quantization descriptors. No I/O, no async. |
hurray-io | Async streaming read/write, file format support. Depends on hurray-core and tokio. |
hurray-ffi | C ABI layer. See c-ffi.md. |
hurray-core
Type System
- MUST define a
TensorDescriptortype that encodes all fields fromdocs/spec/metadata.md. - MUST define an
ElementTypeenum covering all Tier 1 and Tier 2 type tags. - MUST define a
LayoutTagenum covering all named layout tags. - MUST define layout-specific descriptor types for each named layout (e.g.,
StridedLayout,TiledLayout). - MUST define a
BufferHandletype carryingbyte_size,alignment,device_tag, and a release callback (seec-ffi.md). - MUST define an
Errorenum viathiserror. Nounwrap()orexpect()in library code.
Serialization
- MUST implement binary serialization of
TensorDescriptorto the wire format defined indocs/spec/metadata.md. - MUST implement binary deserialization with full validation (magic, version, flag bits, bounds checks, sparse invariants).
- Serialization MUST be
no_std-compatible when theallocfeature is enabled. - A
serdefeature gate MUST provideserde::Serialize/serde::DeserializeforTensorDescriptor(JSON/CBOR interchange for tooling, not the wire format).
Buffer Safety
unsafecode MUST be isolated in dedicated modules.- Every
unsafeblock MUST have a// SAFETY:comment explaining the invariant that makes the code sound. - Buffer aliasing across runtimes MUST be mediated through the
BufferHandlereference count and release callback.
Correctness
cargo clippy -- -D warningsMUST pass.- All public items MUST have
///doc comments with at least one example. - Test coverage for the public API MUST be ≥ 80%.
hurray-io
Streaming Read
- MUST implement an async tensor descriptor reader that reads exactly
descriptor_lengthbytes before emitting a parsedTensorDescriptor. - MUST implement an async data frame reader that yields data in chunks without buffering the entire tensor.
- A reader MUST be able to start processing tensor data without buffering the entire input (streamable principle).
Streaming Write
- MUST implement an async tensor descriptor writer that emits the descriptor before any data bytes.
- MUST implement an async data frame writer that emits data incrementally.
- A writer MUST be able to emit tensors one at a time without buffering the entire output.
Async Runtime
- MUST use
tokioas the async runtime. - MUST NOT mix
rayonthread pool calls directly in async contexts. CPU-bound operations MUST usetokio::task::spawn_blocking. - All async functions MUST be
Send + 'staticto support multi-threaded tokio runtimes.
Streaming Format
- MUST support reading and writing the streaming IPC format defined in
docs/spec/interchange.md: a sequence of zero or more tensor descriptors + data buffers, terminated by an end-of-stream marker. - The streaming format MUST be self-delimiting:
descriptor_lengthallows a reader to advance past any descriptor without full parsing. - Back-references and end-of-file indexes are forbidden in the streaming format (streamable principle).
File Format
- MUST support reading and writing the Hurray file format defined in
docs/spec/file-format.md. The file format is a single-pass writable, random-access readable container for one or more named tensors. - A writer MUST emit the file in a single forward pass (no seek-back), producing the structure:
HRRYFILEmagic + 64-byte file header + zero or more tensor regions (each tensor descriptor followed by its data buffer(s) with appropriate padding) + optional KV metadata section + index section + 40-byte trailer atfile_size - 40. - The writer MUST track per-tensor
(name, descriptor_offset, descriptor_length, data_offset, data_length)tuples in memory and emit them in the index section after all tensor regions are written. - A random-access reader MUST locate the trailer by seeking to
file_size - 40, verifytrailer_magic(ASCIIHRRY), and useindex_offset/index_lengthfrom the trailer to locate the index section. - When the
HAS_INDEX_CRC32Cfile flag is set, the reader MUST verify the CRC-32C of the index section and reject the file on mismatch. - The implementation MUST support mmap-based zero-copy loading of tensor data buffers when the file's
data_buffer_alignmentmatches or exceeds the host page size.
Code Quality
- No
unwrap()orexpect()in library code. All errors propagate with?. - Feature flags:
serde(serialization support),tokio(async I/O, enabled inhurray-io). - MSRV (minimum supported Rust version): tracked in
Cargo.tomland enforced in CI. - All changes to
hurray-coreandhurray-ioMUST be reviewed by therust-revieweragent before merge.