Expand description
Pure-Rust HDF5 file reading, writing, and in-place editing library.
hdf5-pure is a zero-C-dependency crate for creating, reading, and editing
HDF5 files. It is WASM-compatible and supports no_std environments with
alloc.
§Writing files
use hdf5_pure::{FileBuilder, AttrValue};
let mut builder = FileBuilder::new();
builder.create_dataset("data")
.with_f64_data(&[1.0, 2.0, 3.0])
.with_shape(&[3])
.set_attr("unit", AttrValue::String("m/s".into()));
let bytes = builder.finish().unwrap();§Reading files
use hdf5_pure::File;
let file = File::from_bytes(std::fs::read("output.h5").unwrap()).unwrap();
let ds = file.dataset("data").unwrap();
let values = ds.read_f64().unwrap();§Generic over the element type
The typed with_*_data / read_* methods have generic counterparts bounded
by H5Element: DatasetBuilder::with_data writes a flat slice of any
supported scalar and Dataset::read reads one back, so you can write code
generic over the stored type.
use hdf5_pure::{File, FileBuilder, H5Element};
fn store<T: H5Element>(fb: &mut FileBuilder, name: &str, values: &[T]) {
fb.create_dataset(name).with_data(values);
}
let mut fb = FileBuilder::new();
store(&mut fb, "counts", &[1u32, 2, 3]);
let file = File::from_bytes(fb.finish().unwrap()).unwrap();
let counts: Vec<u32> = file.dataset("counts").unwrap().read().unwrap();
assert_eq!(counts, vec![1, 2, 3]);§Editing files in place
Open an existing file for reading and writing with File::open_rw and
reach every object by name through owned Dataset and Group handles
that add, delete, copy, or overwrite objects without rewriting the file from
scratch. New data and rebuilt object headers are appended at end-of-file
and the superblock is repointed last, so the cost is proportional to what
changes rather than to the file size. It edits files written by this crate,
the reference HDF5 C library, and h5py across all of their on-disk formats,
and refuses — rather than silently degrade the file — anything it cannot
reproduce faithfully. Memory follows the file rather than the caller’s choice
of function: a latest-format file with no userblock is edited without ever
building a whole-file copy of it (see MemoryStrategy).
use hdf5_pure::File;
let file = File::open_rw("output.h5").unwrap();
let root = file.root();
root.create_dataset("extra", |b| { b.with_f64_data(&[4.0, 5.0]); }).unwrap();
file.dataset("data").unwrap().write(&[7.0, 8.0]).unwrap(); // H5Dwrite (overwrite)
root.delete("old").unwrap(); // H5Ldelete
file.commit().unwrap(); // apply staged editsA value overwrite (Dataset::write) must match the on-disk datatype and
shape (it is a value write, not a reshape/retype); a same-length contiguous
overwrite is applied straight into the existing data block without rewriting
any header. Immediate, amortized-O(1) row appends use Dataset::append
and need no commit.
§Streaming large files
File::open reads the whole file into memory. To read a file too large to
buffer (for example a multi-gigabyte file on a 32-bit host, where it exceeds
the address space), use File::open_streaming, which fetches metadata and
dataset chunks from the file on demand instead of buffering it whole. The
reading API is identical; only the backing store differs. Reads of
contiguous, compact, and chunked datasets are supported; the streaming
backend currently resolves only latest-format (v2) groups and does not yet
read attributes.
use hdf5_pure::File;
let file = File::open_streaming("huge.h5").unwrap();
let values = file.dataset("signal").unwrap().read_f64().unwrap();§N-dimensional arrays (ndarray feature)
With the ndarray feature, datasets can be written from and read back into
ndarray arrays of any rank, in row-major (C) order:
use hdf5_pure::{File, FileBuilder};
use ndarray::{array, Array2};
let a: Array2<f64> = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
let mut fb = FileBuilder::new();
fb.create_dataset("m").with_ndarray(&a);
let bytes = fb.finish().unwrap();
let file = File::from_bytes(bytes).unwrap();
let back: Array2<f64> = file.dataset("m").unwrap().read_array().unwrap();
assert_eq!(a, back);Modules§
- mat
- MATLAB v7.3 (
.mat) file conventions on top of HDF5.
Structs§
- Append
Builder - Accumulates elements to append to an existing chunked, unlimited dataset via
Dataset::append_staged, in call order along the dataset’s first (axis-0) dimension. - Chunk
- The location and on-disk footprint of one stored chunk.
- Chunk
Cache Config - Configuration for a per-dataset chunk cache.
- Chunk
Cache Stats - A read-only snapshot of a dataset’s chunk-cache occupancy.
- Compound
Member - A member of a compound datatype.
- Compound
Type Builder - Builder for constructing HDF5 compound (struct) datatypes.
- Dataset
- An owned handle to an HDF5 dataset.
- Dataset
Access Properties - Dataset-access properties applied when opening a single dataset.
- Dataset
Builder - Builder for datasets.
- Enum
Member - A member of an enumeration datatype.
- Enum
Type Builder - Builder for constructing HDF5 enumeration datatypes.
- Explicit
Compound Type Builder - Builder for an HDF5 compound datatype with explicit field offsets and size.
- File
- An open HDF5 file.
- File
Access Properties - File-access properties applied when opening an HDF5 file.
- File
Builder - Builder for creating a new HDF5 file.
- File
Create Properties - File-creation properties applied when writing a new HDF5 file.
- File
Space Info - A parsed (or to-be-written) File Space Info message.
- Filter
- One filter in a dataset’s pipeline.
- Finished
Group - A finished group ready for the file writer.
- Group
- An owned handle to an HDF5 group.
- Group
Builder - Builder for HDF5 groups.
- Metadata
Cache Config - Initial metadata-cache settings for streaming file access.
- Repack
Options - Options controlling a
repack. - Space
Accounting - A snapshot of a writable file’s live space usage (issue #150).
- Staged
Group - A group that exists only as a staged edit, handed to
Group::create_group_with’s closure so attributes can be set on a group that is not yet committed (and so has no resolvable header to hang aGrouphandle off). - Vlen
String Read Options - Allocation limits for reading variable-length strings.
Enums§
- Attr
Value - Convenient attribute values for the write API.
- Character
Set - Character set encoding.
- Chunk
Index - The kind of index a chunked dataset uses to locate its chunks.
- DType
- Simplified datatype enum for the high-level API.
- Datatype
- Parsed HDF5 datatype.
- Datatype
Byte Order - Byte order of numeric data.
- Edit
Backing - Which of the two read-write backends a file’s editing session is actually
using, from
File::edit_backing. - Error
- Errors that can occur when using the high-level API.
- File
Locking - Policy for OS advisory file locking when opening a file for editing.
- File
Space Strategy - File-space management strategy, mirroring HDF5’s
H5F_fspace_strategy_t(set withH5Pset_file_space_strategy). - Format
Error - Errors that can occur when parsing HDF5 binary format structures.
- Layout
- How a dataset’s raw data is arranged on disk.
- LibVer
- A library-version boundary, mirroring HDF5’s
H5F_libver_t. - Memory
Strategy - How much memory a read-write open may use to hold the file being edited.
- Object
- The resolved target of an HDF5 object reference (
H5R_OBJECT): either a group or a dataset. - Reference
Type - Reference type.
- Scale
Offset - Scale-offset compression mode requested by the writer.
- String
Padding - String padding type.
- Verify
Result - Outcome of checking a dataset against its stored provenance hash.
Constants§
- OBJECT_
HEADER_ MESSAGE_ MAX - The largest message a version 2 object header can describe: its per-message
size field is 2 bytes wide. A message past this must be refused rather than
written with a truncated length (see
FormatError::ObjectHeaderMessageTooLarge).
Traits§
- Compound
Field - A type that can occupy one field of an HDF5 compound value.
- Compound
Type - A safely encoded HDF5 compound element.
- H5Element
- A Rust scalar type that can be stored as an HDF5 dataset element.
Functions§
- is_hdf5
- Test whether a file looks like an HDF5 file, without reading it whole.
- is_
hdf5_ bytes - Test whether an in-memory buffer begins (at a permitted offset) with the
HDF5 signature. The buffer-backed counterpart of
is_hdf5. - make_
f32_ type - make_
f64_ type - make_
i8_ type - make_
i16_ type - make_
i32_ type - make_
i64_ type - make_
u8_ type - make_
u16_ type - make_
u32_ type - make_
u64_ type - repack
- Repack
srcinto a new file atdst, applyingoptions.
Type Aliases§
- Dataset
Access Options Deprecated - Former name of
DatasetAccessProperties. - File
Access Options Deprecated - Former name of
FileAccessProperties. - File
Create Options Deprecated - Former name of
FileCreateProperties.