rust_hdf5/swmr.rs
1//! Single Writer / Multiple Reader (SWMR) API.
2//!
3//! Provides a high-level wrapper around the SWMR protocol for streaming
4//! frame-based data (e.g., area detector images).
5
6use std::path::Path;
7
8use crate::format::messages::attribute::AttributeMessage;
9use crate::io::locking::FileLocking;
10use crate::io::Hdf5Reader;
11use crate::io::SwmrWriter as IoSwmrWriter;
12
13use crate::error::Result;
14use crate::types::H5Type;
15
16/// SWMR writer for streaming frame-based data to an HDF5 file.
17///
18/// Usage:
19/// ```no_run
20/// use rust_hdf5::swmr::SwmrFileWriter;
21///
22/// let mut writer = SwmrFileWriter::create("stream.h5").unwrap();
23/// let ds = writer.create_streaming_dataset::<f32>("frames", &[256, 256]).unwrap();
24/// writer.start_swmr().unwrap();
25///
26/// // Write frames
27/// let frame_data = vec![0.0f32; 256 * 256];
28/// let raw: Vec<u8> = frame_data.iter()
29/// .flat_map(|v| v.to_le_bytes())
30/// .collect();
31/// writer.append_frame(ds, &raw).unwrap();
32/// writer.flush().unwrap();
33///
34/// writer.close().unwrap();
35/// ```
36pub struct SwmrFileWriter {
37 inner: IoSwmrWriter,
38}
39
40impl SwmrFileWriter {
41 /// Create a new HDF5 file for SWMR streaming using the env-var-derived
42 /// locking policy.
43 pub fn create<P: AsRef<Path>>(path: P) -> Result<Self> {
44 let inner = IoSwmrWriter::create(path.as_ref())?;
45 Ok(Self { inner })
46 }
47
48 /// Create a new HDF5 file for SWMR streaming with an explicit locking
49 /// policy. The writer holds an exclusive lock until [`Self::start_swmr`]
50 /// is called, at which point the lock is downgraded to shared so
51 /// concurrent SWMR readers can attach.
52 pub fn create_with_locking<P: AsRef<Path>>(path: P, locking: FileLocking) -> Result<Self> {
53 let inner = IoSwmrWriter::create_with_locking(path.as_ref(), locking)?;
54 Ok(Self { inner })
55 }
56
57 /// Reopen a cleanly-closed HDF5 file to resume SWMR streaming.
58 ///
59 /// Existing datasets are reconstructed; locate them with
60 /// [`dataset_index`](Self::dataset_index), call [`start_swmr`](Self::start_swmr)
61 /// to re-enter SWMR mode, then continue with [`append_frame`](Self::append_frame).
62 /// Appending to a multi-frame-chunk dataset (`chunk[0] > 1`) after reopen
63 /// is rejected — its final partial band was zero-padded at the original
64 /// close. Recovering a crashed (never cleanly closed) file is not supported.
65 pub fn open_append<P: AsRef<Path>>(path: P) -> Result<Self> {
66 let inner = IoSwmrWriter::open_append(path.as_ref())?;
67 Ok(Self { inner })
68 }
69
70 /// Reopen a cleanly-closed HDF5 file to resume SWMR streaming with an
71 /// explicit locking policy. See [`Self::open_append`].
72 pub fn open_append_with_locking<P: AsRef<Path>>(path: P, locking: FileLocking) -> Result<Self> {
73 let inner = IoSwmrWriter::open_append_with_locking(path.as_ref(), locking)?;
74 Ok(Self { inner })
75 }
76
77 /// Return the index of a dataset by name, or `None` if absent.
78 ///
79 /// Mainly used after [`open_append`](Self::open_append) to recover the
80 /// index of a reconstructed dataset for [`append_frame`](Self::append_frame).
81 pub fn dataset_index(&self, name: &str) -> Option<usize> {
82 self.inner.dataset_index(name)
83 }
84
85 /// Create a streaming dataset.
86 ///
87 /// The dataset will have shape `[0, frame_dims...]` initially, with
88 /// chunk dimensions `[1, frame_dims...]` and unlimited first dimension.
89 ///
90 /// Returns the dataset index for use with `append_frame`.
91 pub fn create_streaming_dataset<T: H5Type>(
92 &mut self,
93 name: &str,
94 frame_dims: &[u64],
95 ) -> Result<usize> {
96 let datatype = T::hdf5_type();
97 let idx = self
98 .inner
99 .create_streaming_dataset(name, datatype, frame_dims)?;
100 Ok(idx)
101 }
102
103 /// Create a streaming dataset whose frames are compressed.
104 ///
105 /// Like [`create_streaming_dataset`](Self::create_streaming_dataset) but
106 /// each appended frame is run through `pipeline` (e.g.
107 /// `FilterPipeline::deflate(4)`). SWMR appends and in-place header
108 /// updates work the same as for uncompressed streaming datasets.
109 pub fn create_streaming_dataset_compressed<T: H5Type>(
110 &mut self,
111 name: &str,
112 frame_dims: &[u64],
113 pipeline: crate::format::messages::filter::FilterPipeline,
114 ) -> Result<usize> {
115 let idx = self.inner.create_streaming_dataset_compressed(
116 name,
117 T::hdf5_type(),
118 frame_dims,
119 pipeline,
120 )?;
121 Ok(idx)
122 }
123
124 /// Create a streaming dataset whose frames are split into fixed-size
125 /// chunk tiles.
126 ///
127 /// `frame_dims` is the per-frame shape (e.g. `[1024, 1024]`);
128 /// `frame_chunk` is the tile shape within a frame (e.g. `[256, 256]`),
129 /// of the same rank. The on-disk chunk shape becomes
130 /// `[1, frame_chunk...]`, so each frame is stored as
131 /// `product(frame_dims / frame_chunk)` chunks instead of one. This
132 /// mirrors area-detector tiling controls such as NDFileHDF5's
133 /// `nRowChunks` / `nColChunks`: it changes only the partial-read
134 /// granularity and compression unit, not the stored data.
135 /// [`append_frame`](Self::append_frame) accepts a whole frame and
136 /// splits it into tiles automatically.
137 pub fn create_streaming_dataset_tiled<T: H5Type>(
138 &mut self,
139 name: &str,
140 frame_dims: &[u64],
141 frame_chunk: &[u64],
142 ) -> Result<usize> {
143 let idx = self.inner.create_streaming_dataset_tiled(
144 name,
145 T::hdf5_type(),
146 frame_dims,
147 frame_chunk,
148 )?;
149 Ok(idx)
150 }
151
152 /// Create a compressed streaming dataset whose frames are split into
153 /// fixed-size chunk tiles. See
154 /// [`create_streaming_dataset_tiled`](Self::create_streaming_dataset_tiled)
155 /// for the meaning of `frame_chunk`; each tile is the compression unit.
156 pub fn create_streaming_dataset_tiled_compressed<T: H5Type>(
157 &mut self,
158 name: &str,
159 frame_dims: &[u64],
160 frame_chunk: &[u64],
161 pipeline: crate::format::messages::filter::FilterPipeline,
162 ) -> Result<usize> {
163 let idx = self.inner.create_streaming_dataset_tiled_compressed(
164 name,
165 T::hdf5_type(),
166 frame_dims,
167 frame_chunk,
168 pipeline,
169 )?;
170 Ok(idx)
171 }
172
173 /// Create a streaming dataset with full control over the chunk shape,
174 /// including the frame axis.
175 ///
176 /// `chunk` is the complete per-chunk shape, of rank
177 /// `frame_dims.len() + 1`: `chunk[0]` frames per chunk (the NDFileHDF5
178 /// `nFramesChunks` control) and `chunk[1..]` the per-frame tile shape
179 /// (`nRowChunks` / `nColChunks`). When `chunk[0] > 1`,
180 /// [`append_frame`](Self::append_frame) buffers whole frames until a
181 /// chunk band fills; the final partial band is written (zero-padded) at
182 /// [`close`](Self::close), and the dataset's logical frame count always
183 /// equals the exact number of frames appended.
184 pub fn create_streaming_dataset_chunked<T: H5Type>(
185 &mut self,
186 name: &str,
187 frame_dims: &[u64],
188 chunk: &[u64],
189 ) -> Result<usize> {
190 let idx =
191 self.inner
192 .create_streaming_dataset_chunked(name, T::hdf5_type(), frame_dims, chunk)?;
193 Ok(idx)
194 }
195
196 /// Compressed variant of
197 /// [`create_streaming_dataset_chunked`](Self::create_streaming_dataset_chunked);
198 /// each chunk is filtered independently through `pipeline`.
199 pub fn create_streaming_dataset_chunked_compressed<T: H5Type>(
200 &mut self,
201 name: &str,
202 frame_dims: &[u64],
203 chunk: &[u64],
204 pipeline: crate::format::messages::filter::FilterPipeline,
205 ) -> Result<usize> {
206 let idx = self.inner.create_streaming_dataset_chunked_compressed(
207 name,
208 T::hdf5_type(),
209 frame_dims,
210 chunk,
211 pipeline,
212 )?;
213 Ok(idx)
214 }
215
216 /// Create a fixed-shape multi-dimensional grid dataset that fills at
217 /// explicit positions as frames arrive.
218 ///
219 /// Unlike [`create_streaming_dataset`](Self::create_streaming_dataset),
220 /// which appends frames along a single unlimited leading axis, this
221 /// creates a dataset of the full bounded shape `dims` (no unlimited axis)
222 /// and lets you place each frame at an arbitrary chunk position with
223 /// [`write_chunk_at`](Self::write_chunk_at). This mirrors AreaDetector's
224 /// "extra dimensions" layout, where a scan of known size (e.g.
225 /// `[Na, Nb, H, W]`) is filled in odometer order. `chunk` is the per-chunk
226 /// shape of the same rank (typically `[1, …, 1, H, W]`). Returns the
227 /// dataset index.
228 pub fn create_grid_dataset<T: H5Type>(
229 &mut self,
230 name: &str,
231 dims: &[u64],
232 chunk: &[u64],
233 ) -> Result<usize> {
234 let idx = self
235 .inner
236 .create_grid_dataset(name, T::hdf5_type(), dims, chunk)?;
237 Ok(idx)
238 }
239
240 /// Create a hard link: an additional name for a dataset or group that
241 /// already exists in the file.
242 ///
243 /// No data is copied — the link and its target share one object header,
244 /// exactly as `h5py` / libhdf5 hard links do. This is the NeXus-style way
245 /// to expose a streaming dataset at an aliased path.
246 ///
247 /// * `parent_group_path` — full path of the group that will hold the
248 /// link (`"/"` for the root group).
249 /// * `link_name` — leaf name of the new link within that group.
250 /// * `target_path` — full path of an existing dataset or group.
251 ///
252 /// # Visibility relative to SWMR mode
253 ///
254 /// A link created **before** [`start_swmr`](Self::start_swmr) is committed
255 /// by `start_swmr` and is visible to SWMR readers for the whole streaming
256 /// window. A link created **after** `start_swmr` is committed only by
257 /// [`close`](Self::close); it does not appear to readers that attach
258 /// during the live SWMR window. Create layout links before `start_swmr`
259 /// when readers must resolve them while streaming.
260 pub fn create_hard_link(
261 &mut self,
262 parent_group_path: &str,
263 link_name: &str,
264 target_path: &str,
265 ) -> Result<()> {
266 self.inner
267 .writer_mut()
268 .create_hard_link(parent_group_path, link_name, target_path)?;
269 Ok(())
270 }
271
272 /// Create a group in the file hierarchy.
273 ///
274 /// * `parent_group_path` — full path of the parent group (`"/"` for the
275 /// root group).
276 /// * `name` — leaf name of the new group.
277 ///
278 /// A nested NeXus layout is built one level at a time, parent first:
279 ///
280 /// ```no_run
281 /// # use rust_hdf5::swmr::SwmrFileWriter;
282 /// # let mut writer = SwmrFileWriter::create("stream.h5").unwrap();
283 /// writer.create_group("/", "entry").unwrap();
284 /// writer.create_group("/entry", "data").unwrap();
285 /// ```
286 ///
287 /// Like [`create_hard_link`](Self::create_hard_link), a group created
288 /// before [`start_swmr`](Self::start_swmr) is visible to SWMR readers for
289 /// the whole streaming window; one created after is committed only by
290 /// [`close`](Self::close).
291 pub fn create_group(&mut self, parent_group_path: &str, name: &str) -> Result<()> {
292 self.inner
293 .writer_mut()
294 .create_group(parent_group_path, name)?;
295 Ok(())
296 }
297
298 /// Set a string attribute on a group, or on the root group when
299 /// `group_path` is `"/"`.
300 ///
301 /// This is the NeXus way to tag a group with its class — for example
302 /// `set_group_attr_string("/entry", "NX_class", "NXentry")`. An existing
303 /// attribute of the same name is replaced.
304 ///
305 /// The same SWMR visibility rule as [`create_group`](Self::create_group)
306 /// applies: set before [`start_swmr`](Self::start_swmr) for the attribute
307 /// to be visible to readers during streaming.
308 pub fn set_group_attr_string(
309 &mut self,
310 group_path: &str,
311 name: &str,
312 value: &str,
313 ) -> Result<()> {
314 let attr = self.inner.writer_mut().vlen_string_attribute(name, value)?;
315 if group_path == "/" {
316 self.inner.writer_mut().add_root_attribute(attr);
317 } else {
318 self.inner
319 .writer_mut()
320 .add_group_attribute(group_path, attr)?;
321 }
322 Ok(())
323 }
324
325 /// Set a numeric scalar attribute on a group, or on the root group when
326 /// `group_path` is `"/"`. An existing attribute of the same name is
327 /// replaced. See [`set_group_attr_string`](Self::set_group_attr_string)
328 /// for the SWMR visibility rule.
329 pub fn set_group_attr_numeric<T: H5Type>(
330 &mut self,
331 group_path: &str,
332 name: &str,
333 value: &T,
334 ) -> Result<()> {
335 let attr = AttributeMessage::scalar_numeric(name, T::hdf5_type(), scalar_to_bytes(value));
336 if group_path == "/" {
337 self.inner.writer_mut().add_root_attribute(attr);
338 } else {
339 self.inner
340 .writer_mut()
341 .add_group_attribute(group_path, attr)?;
342 }
343 Ok(())
344 }
345
346 /// Create a fixed-shape (non-streaming) dataset and write all its data in
347 /// one call. Returns the dataset index.
348 ///
349 /// This is for the NeXus metadata that surrounds the image stream —
350 /// coordinate axes, detector geometry, and (with `dims = &[]`) scalar
351 /// values such as `/entry/instrument/detector/distance`. Unlike a
352 /// streaming dataset, it is written once and not appended to.
353 pub fn write_dataset<T: H5Type>(
354 &mut self,
355 name: &str,
356 dims: &[u64],
357 data: &[T],
358 ) -> Result<usize> {
359 let expected: u64 = if dims.is_empty() {
360 1
361 } else {
362 dims.iter().product()
363 };
364 if data.len() as u64 != expected {
365 return Err(crate::error::Hdf5Error::InvalidState(format!(
366 "write_dataset: data has {} elements but shape {dims:?} needs {expected}",
367 data.len()
368 )));
369 }
370 let idx = self
371 .inner
372 .writer_mut()
373 .create_dataset(name, T::hdf5_type(), dims)?;
374 self.inner
375 .writer_mut()
376 .write_dataset_raw(idx, slice_to_bytes(data))?;
377 Ok(idx)
378 }
379
380 /// Create a variable-length string dataset (one element per string).
381 /// Returns the dataset index. Useful for NeXus metadata such as
382 /// `/entry/start_time` or per-frame timestamp arrays.
383 pub fn write_string_dataset(&mut self, name: &str, strings: &[&str]) -> Result<usize> {
384 let idx = self
385 .inner
386 .writer_mut()
387 .create_vlen_string_dataset(name, strings)?;
388 Ok(idx)
389 }
390
391 /// Set a string attribute on a dataset, addressed by its index. The
392 /// NeXus way to record `units`, `long_name`, `signal`, etc. An existing
393 /// attribute of the same name is replaced.
394 pub fn set_dataset_attr_string(
395 &mut self,
396 ds_index: usize,
397 name: &str,
398 value: &str,
399 ) -> Result<()> {
400 let attr = self.inner.writer_mut().vlen_string_attribute(name, value)?;
401 self.inner
402 .writer_mut()
403 .add_dataset_attribute(ds_index, attr)?;
404 Ok(())
405 }
406
407 /// Set a numeric scalar attribute on a dataset, addressed by its index.
408 /// An existing attribute of the same name is replaced.
409 pub fn set_dataset_attr_numeric<T: H5Type>(
410 &mut self,
411 ds_index: usize,
412 name: &str,
413 value: &T,
414 ) -> Result<()> {
415 let attr = AttributeMessage::scalar_numeric(name, T::hdf5_type(), scalar_to_bytes(value));
416 self.inner
417 .writer_mut()
418 .add_dataset_attribute(ds_index, attr)?;
419 Ok(())
420 }
421
422 /// Set a numeric array attribute on a dataset, addressed by its index.
423 ///
424 /// `dims` are the dimension sizes (e.g. `&[3]` for a 1-D array) and the
425 /// number of `values` must equal their product. This is the SWMR
426 /// counterpart of [`H5Attribute::write_array`](crate::H5Attribute::write_array),
427 /// for the length-ndims `int32` array attributes AreaDetector writes
428 /// (`NDArrayDimOffset`, `NDArrayDimBinning`, `NDArrayDimReverse`). An
429 /// existing attribute of the same name is replaced.
430 ///
431 /// In SWMR mode, attributes can only be added before
432 /// [`start_swmr`](Self::start_swmr); HDF5 forbids adding dataset
433 /// attributes once the file is in SWMR write mode. Resolve a dataset path
434 /// to its index with [`dataset_index`](Self::dataset_index).
435 pub fn set_dataset_attr_array<T: H5Type>(
436 &mut self,
437 ds_index: usize,
438 name: &str,
439 dims: &[u64],
440 values: &[T],
441 ) -> Result<()> {
442 // Product of an empty shape is 1 (a scalar holds one element).
443 let expected: u64 = dims.iter().product();
444 if values.len() as u64 != expected {
445 return Err(crate::error::Hdf5Error::InvalidState(format!(
446 "set_dataset_attr_array: {} values but shape {dims:?} needs {expected}",
447 values.len()
448 )));
449 }
450 let attr = AttributeMessage::array_numeric(
451 name,
452 T::hdf5_type(),
453 dims,
454 slice_to_bytes(values).to_vec(),
455 );
456 self.inner
457 .writer_mut()
458 .add_dataset_attribute(ds_index, attr)?;
459 Ok(())
460 }
461
462 /// Set the fill value of a streaming dataset, addressed by its index.
463 ///
464 /// Call this before the first [`append_frame`](Self::append_frame): it
465 /// determines the value of chunk regions that are never written (a
466 /// partial final band, or unwritten tiles).
467 pub fn set_dataset_fill_value<T: H5Type>(&mut self, ds_index: usize, value: &T) -> Result<()> {
468 self.inner
469 .writer_mut()
470 .set_dataset_fill_value(ds_index, scalar_to_bytes(value))?;
471 Ok(())
472 }
473
474 /// Place an existing dataset inside a group.
475 ///
476 /// By default a dataset created through this writer lives at the root
477 /// level; this moves its link record into `group_path` (which must
478 /// already exist). The group must be created before `start_swmr` for the
479 /// placement to be visible to readers during streaming.
480 pub fn assign_dataset_to_group(&mut self, group_path: &str, ds_index: usize) -> Result<()> {
481 self.inner
482 .writer_mut()
483 .assign_dataset_to_group(group_path, ds_index)?;
484 Ok(())
485 }
486
487 /// Signal the start of SWMR mode.
488 pub fn start_swmr(&mut self) -> Result<()> {
489 self.inner.start_swmr()?;
490 Ok(())
491 }
492
493 /// Append a frame of raw data to a streaming dataset.
494 ///
495 /// The data size must match one frame (product of frame_dims * element_size).
496 pub fn append_frame(&mut self, ds_index: usize, data: &[u8]) -> Result<()> {
497 self.inner.append_frame(ds_index, data)?;
498 Ok(())
499 }
500
501 /// Write one frame at an explicit chunk position of a grid dataset
502 /// created with [`create_grid_dataset`](Self::create_grid_dataset).
503 ///
504 /// `chunk_coords` are in units of chunks (row-major over the chunk grid)
505 /// and `data` must be exactly one full chunk (`product(chunk) *
506 /// element_size` bytes; edge chunks are zero-padded by the caller). The
507 /// logical extent is fixed, so positions may be written in any order and
508 /// unwritten positions read back as fill. As with the streaming path,
509 /// call [`flush`](Self::flush) to make writes visible to SWMR readers and
510 /// set dataset attributes before [`start_swmr`](Self::start_swmr).
511 pub fn write_chunk_at(
512 &mut self,
513 ds_index: usize,
514 chunk_coords: &[u64],
515 data: &[u8],
516 ) -> Result<()> {
517 self.inner.write_chunk_at(ds_index, chunk_coords, data)?;
518 Ok(())
519 }
520
521 /// Flush all dataset index structures to disk with SWMR ordering.
522 pub fn flush(&mut self) -> Result<()> {
523 self.inner.flush()?;
524 Ok(())
525 }
526
527 /// Close and finalize the file.
528 pub fn close(self) -> Result<()> {
529 self.inner.close()?;
530 Ok(())
531 }
532}
533
534/// SWMR reader for monitoring a streaming HDF5 file.
535///
536/// Opens a file being written by a concurrent [`SwmrFileWriter`] and
537/// periodically calls [`refresh`](Self::refresh) to pick up new data.
538///
539/// ```no_run
540/// use rust_hdf5::swmr::SwmrFileReader;
541///
542/// let mut reader = SwmrFileReader::open("stream.h5").unwrap();
543///
544/// loop {
545/// reader.refresh().unwrap();
546/// let names = reader.dataset_names();
547/// if let Some(shape) = reader.dataset_shape("frames").ok() {
548/// println!("frames shape: {:?}", shape);
549/// if shape[0] > 0 {
550/// let data = reader.read_dataset_raw("frames").unwrap();
551/// println!("got {} bytes", data.len());
552/// break;
553/// }
554/// }
555/// std::thread::sleep(std::time::Duration::from_millis(100));
556/// }
557/// ```
558pub struct SwmrFileReader {
559 reader: Hdf5Reader,
560}
561
562impl SwmrFileReader {
563 /// Open an HDF5 file for SWMR reading using the env-var-derived
564 /// locking policy. Takes a shared lock so it coexists with the
565 /// downgraded shared lock held by [`SwmrFileWriter`] after
566 /// `start_swmr`, and with other concurrent SWMR readers.
567 pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
568 let reader = Hdf5Reader::open_swmr(path.as_ref())?;
569 Ok(Self { reader })
570 }
571
572 /// Open an HDF5 file for SWMR reading with an explicit locking policy.
573 pub fn open_with_locking<P: AsRef<Path>>(path: P, locking: FileLocking) -> Result<Self> {
574 let reader = Hdf5Reader::open_swmr_with_locking(path.as_ref(), locking)?;
575 Ok(Self { reader })
576 }
577
578 /// Re-read the superblock and dataset metadata from disk.
579 ///
580 /// Call this periodically to pick up new data written by the concurrent
581 /// SWMR writer.
582 pub fn refresh(&mut self) -> Result<()> {
583 self.reader.refresh()?;
584 Ok(())
585 }
586
587 /// Return the names of all datasets.
588 pub fn dataset_names(&self) -> Vec<String> {
589 self.reader
590 .dataset_names()
591 .iter()
592 .map(|s| s.to_string())
593 .collect()
594 }
595
596 /// Return the current shape of a dataset.
597 pub fn dataset_shape(&self, name: &str) -> Result<Vec<u64>> {
598 Ok(self.reader.dataset_shape(name)?)
599 }
600
601 /// Read the raw bytes of a dataset.
602 pub fn read_dataset_raw(&mut self, name: &str) -> Result<Vec<u8>> {
603 Ok(self.reader.read_dataset_raw(name)?)
604 }
605
606 /// Read a dataset as a typed vector.
607 pub fn read_dataset<T: H5Type>(&mut self, name: &str) -> Result<Vec<T>> {
608 bytes_to_typed(self.reader.read_dataset_raw(name)?)
609 }
610
611 /// Read a slice (hyperslab) of a dataset as raw bytes.
612 ///
613 /// `starts[d]` is the first index along dimension `d`, `counts[d]` is how
614 /// many. For a streaming dataset this reads only the chunks the slice
615 /// overlaps — the efficient way for a live viewer to fetch the latest
616 /// frame without re-reading the whole stream.
617 pub fn read_slice_raw(
618 &mut self,
619 name: &str,
620 starts: &[u64],
621 counts: &[u64],
622 ) -> Result<Vec<u8>> {
623 Ok(self.reader.read_slice(name, starts, counts)?)
624 }
625
626 /// Read a slice (hyperslab) of a dataset as a typed vector.
627 /// See [`read_slice_raw`](Self::read_slice_raw).
628 pub fn read_slice<T: H5Type>(
629 &mut self,
630 name: &str,
631 starts: &[u64],
632 counts: &[u64],
633 ) -> Result<Vec<T>> {
634 bytes_to_typed(self.reader.read_slice(name, starts, counts)?)
635 }
636
637 /// Read a variable-length string dataset.
638 pub fn read_vlen_strings(&mut self, name: &str) -> Result<Vec<String>> {
639 Ok(self.reader.read_vlen_strings(name)?)
640 }
641
642 /// Element size of a dataset's datatype, in bytes — enough to size a
643 /// read buffer without knowing the concrete element type at compile time.
644 pub fn dataset_element_size(&self, name: &str) -> Result<usize> {
645 self.reader
646 .dataset_info(name)
647 .map(|i| i.datatype.element_size() as usize)
648 .ok_or_else(|| crate::error::Hdf5Error::NotFound(name.to_string()))
649 }
650
651 /// All group paths in the file.
652 pub fn group_paths(&self) -> Vec<String> {
653 self.reader.group_paths().iter().cloned().collect()
654 }
655
656 /// Whether a group exists. A leading `/` is tolerated.
657 pub fn has_group(&self, group_path: &str) -> bool {
658 self.reader.has_group(group_path.trim_start_matches('/'))
659 }
660
661 /// Names of the attributes on a dataset.
662 pub fn dataset_attr_names(&self, name: &str) -> Result<Vec<String>> {
663 Ok(self.reader.dataset_attr_names(name)?)
664 }
665
666 /// Read a dataset's attribute as a string (e.g. `units`, `NX_class`).
667 pub fn dataset_attr_string(&mut self, dataset: &str, attr: &str) -> Result<String> {
668 let a = self.reader.dataset_attr(dataset, attr)?.clone();
669 Ok(self.reader.attr_string_value(&a)?)
670 }
671
672 /// Names of the attributes on a group, or on the root group when
673 /// `group_path` is `"/"`. A leading `/` is tolerated.
674 pub fn group_attr_names(&self, group_path: &str) -> Vec<String> {
675 if group_path == "/" {
676 self.reader.root_attr_names()
677 } else {
678 self.reader
679 .group_attr_names(group_path.trim_start_matches('/'))
680 }
681 }
682
683 /// Read a group's attribute as a string (the NeXus `NX_class` etc.), or
684 /// a root attribute when `group_path` is `"/"`. A leading `/` is tolerated.
685 pub fn group_attr_string(&mut self, group_path: &str, attr: &str) -> Result<String> {
686 let a = if group_path == "/" {
687 self.reader.root_attr(attr)
688 } else {
689 self.reader
690 .group_attr(group_path.trim_start_matches('/'), attr)
691 }
692 .ok_or_else(|| crate::error::Hdf5Error::NotFound(attr.to_string()))?
693 .clone();
694 Ok(self.reader.attr_string_value(&a)?)
695 }
696}
697
698/// Reinterpret a raw byte buffer as a typed vector. The buffer length must
699/// be a whole multiple of `T`'s element size.
700fn bytes_to_typed<T: H5Type>(raw: Vec<u8>) -> Result<Vec<T>> {
701 let es = T::element_size();
702 if es == 0 || !raw.len().is_multiple_of(es) {
703 return Err(crate::error::Hdf5Error::TypeMismatch(format!(
704 "raw data size {} is not a multiple of element size {es}",
705 raw.len()
706 )));
707 }
708 let count = raw.len() / es;
709 let mut result = Vec::<T>::with_capacity(count);
710 // Safety: `T: H5Type` is a `Copy` POD primitive exactly `element_size()`
711 // bytes wide, so the byte buffer is a valid array of `count` `T`s.
712 unsafe {
713 std::ptr::copy_nonoverlapping(raw.as_ptr(), result.as_mut_ptr() as *mut u8, raw.len());
714 result.set_len(count);
715 }
716 Ok(result)
717}
718
719/// Raw bytes of one `H5Type` scalar.
720fn scalar_to_bytes<T: H5Type>(value: &T) -> Vec<u8> {
721 let es = T::element_size();
722 // Safety: `T: H5Type` is a `Copy` POD primitive exactly `element_size()`
723 // bytes wide.
724 unsafe { std::slice::from_raw_parts(value as *const T as *const u8, es) }.to_vec()
725}
726
727/// Raw bytes of an `H5Type` slice, in element order.
728fn slice_to_bytes<T: H5Type>(data: &[T]) -> &[u8] {
729 // Safety: as `scalar_to_bytes`; the slice is a contiguous POD array.
730 unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, std::mem::size_of_val(data)) }
731}