h5rio
A simple HDF5 Rust IO package
h5rio
A small Rust library for writing and reading appendable HDF5 datasets.
h5rio provides a compact interface for two common data-acquisition and
simulation output patterns:
- Tables: append typed records to a one-dimensional HDF5 dataset.
- Arrays: append fixed-shape
ndarrayvalues along a resizable first axis.
Datasets are buffered in memory and written in chunks, using Blosc/Zlib
compression through hdf5-metno.
A convenience attribute macro, #[h5type], is provided for defining
HDF5-compatible table records.
Features
- Append-only HDF5 table writer for compound Rust types.
- Append-only HDF5 array writer for fixed-shape
ndarrayentries. - Configurable buffering through the number of entries stored per chunk.
- Read helpers for complete table and array datasets.
#[h5type]macro for HDF5-compatible row types.
Data model
TableHdf5Writer<T> creates a dataset with shape
(n_rows,)
where each call to write(value) appends one record of type T.
ArrayHdf5Writer<T> creates a dataset with shape
(n_entries, *item_shape)
where item_shape is fixed at construction time and the leading dimension
grows as arrays are appended. For example, a writer created with
shape = vec![2, 3] stores successive 2 x 3 arrays in a dataset with shape
(n_entries, 2, 3).
Both writers use a resizable leading axis and Blosc/Zlib compression.
Installation
To use the current repository version, depend on the Git repository:
[]
= "0.1.0"
# Needed to create/open HDF5 files and by the #[h5type] macro expansion.
= { = "hdf5-metno", = "0.12.3", = ["blosc-zlib"] }
# Needed when writing ndarray values.
= "0.17.2"
The library requires an HDF5 installation available to hdf5-metno. The
repository includes a Nix development shell that provides HDF5 and the pinned
Rust toolchain.
Quick start: writing a table
use Rc;
use ;
use hdf5_metno as hdf5;
The #[h5type] attribute expands the struct definition with the derives and
representation required for table storage:
Writing fixed-shape arrays
ArrayHdf5Writer is useful for storing successive waveforms, images,
response maps, or any stream of arrays with the same shape.
use Rc;
use ;
use hdf5_metno as hdf5;
use arr2;
API overview
Writers
| Type | Purpose | Main methods |
|---|---|---|
TableHdf5Writer<T> |
Append scalar records of an HDF5-compatible type | new, write, flush |
ArrayHdf5Writer<T> |
Append fixed-shape ndarray::Array entries |
new, write, flush |
The chunk_size constructor argument is the number of appended entries
buffered before a write to disk. For table datasets, one entry is one record.
For array datasets, one entry is one full array with the configured
item_shape.
Readers
The current readers load the complete dataset into memory.
Macro
#[h5type] is intended for plain record structs used with
TableHdf5Writer<T> and read_table<T>.
Development
With Nix
The recommended development environment is provided by the flake. It includes
the pinned Rust toolchain, HDF5, cargo-nextest, just, bacon, and
Rust Analyzer support.
Without Nix
Install HDF5 and a compatible Rust toolchain, then run:
The repository pins Rust 1.95.0 in rust-toolchain.toml. The project's
justfile uses cargo nextest for its test recipes.
Testing and continuous integration
Tests cover:
- construction of valid and invalid writers;
- table and array round trips;
- flushing incomplete chunks when writers are dropped;
- the derives and C-compatible memory layout generated by
#[h5type].
On every push and pull request, the GitHub Actions workflow builds and tests the project inside the Nix development environment with warnings treated as errors.
License
This project is licensed under the terms of the GNU General Public License v3.0.