Skip to main content

hdf5_rust/
file.rs

1//! Public [`Hdf5File`] API: in-memory image, optional `std` paths.
2
3use alloc::string::{String, ToString};
4use alloc::vec::Vec;
5
6use crate::error::{HDF5Error, Result};
7use crate::model::{DTypeKind, DatasetRec, FileModel};
8use crate::{HDF5DType, HDF5_MAX_DIMS, HDF5_MAX_NAME_LEN};
9
10/// An HDF5 file held as a logical graph and (re)serialized to bytes on demand.
11#[derive(Clone, Debug)]
12pub struct Hdf5File {
13    model: FileModel,
14}
15
16impl Default for Hdf5File {
17    fn default() -> Self {
18        Self::create()
19    }
20}
21
22impl Hdf5File {
23    /// Empty file with a root group. Writes superblock version 2 on [`Self::to_bytes`].
24    pub fn create() -> Self {
25        Self {
26            model: FileModel::new(),
27        }
28    }
29
30    /// Parse a complete HDF5 file image. Returns [`HDF5Error::InvalidSignature`]
31    /// if the format signature is missing.
32    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
33        Ok(Self {
34            model: crate::decode::decode(bytes)?,
35        })
36    }
37
38    /// Serialize to a standard HDF5 superblock-v2 file image.
39    pub fn to_bytes(&self) -> Result<Vec<u8>> {
40        crate::encode::encode(&self.model)
41    }
42
43    /// Open an existing file from a filesystem path.
44    ///
45    /// Errors: [`HDF5Error::Io`], plus every error [`Self::from_bytes`] can return.
46    #[cfg(feature = "std")]
47    pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
48        let bytes = std::fs::read(path)?;
49        Self::from_bytes(&bytes)
50    }
51
52    /// Create an empty file object (does not write until [`Self::save`]).
53    #[cfg(feature = "std")]
54    pub fn create_file<P: AsRef<std::path::Path>>(_path: P) -> Result<Self> {
55        Ok(Self::create())
56    }
57
58    /// Write the current image to `path`.
59    ///
60    /// Errors: [`HDF5Error::Io`] and encode failures.
61    #[cfg(feature = "std")]
62    pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<()> {
63        let bytes = self.to_bytes()?;
64        std::fs::write(path, bytes)?;
65        Ok(())
66    }
67
68    /// Dataset paths (no leading slash), sorted.
69    pub fn list_datasets(&self) -> Vec<String> {
70        self.model.list_datasets()
71    }
72
73    /// Group paths. Root is `"/"`.
74    pub fn list_groups(&self) -> Vec<String> {
75        self.model.list_groups()
76    }
77
78    /// Dataspace dimensions of `path`.
79    ///
80    /// Errors: [`HDF5Error::NotFound`]. Shape is reported for chunked and
81    /// filtered datasets; reading those still returns a named error.
82    pub fn dataset_shape(&self, path: &str) -> Result<Vec<usize>> {
83        let ds = self.dataset_meta(path)?;
84        Ok(ds.shape.iter().map(|d| *d as usize).collect())
85    }
86
87    /// Datatype of `path`.
88    ///
89    /// Errors: [`HDF5Error::NotFound`]. Chunked datasets still report a type.
90    pub fn dataset_dtype(&self, path: &str) -> Result<HDF5DType> {
91        let p = FileModel::normalize(path)?;
92        let ds = self.model.datasets.get(&p).ok_or(HDF5Error::NotFound(p))?;
93        Ok(ds.kind.to_public())
94    }
95
96    /// Create a group. Parent must exist (root always does).
97    ///
98    /// Errors: [`HDF5Error::NotFound`], [`HDF5Error::NameTooLong`].
99    pub fn create_group(&mut self, path: &str) -> Result<()> {
100        self.model.create_group(path)
101    }
102
103    /// Write IEEE binary64 little-endian lanes. `bits.len()` must equal the
104    /// product of `shape` (empty `shape` is a scalar, one element).
105    /// Rank 0 through [`HDF5_MAX_DIMS`].
106    ///
107    /// Errors: [`HDF5Error::ShapeMismatch`], [`HDF5Error::RankNotSupported`],
108    /// [`HDF5Error::NameTooLong`].
109    pub fn write_f64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
110        self.write_ieee64(path, shape, bits)
111    }
112
113    /// IEEE binary64 little-endian lanes. Same as [`Self::write_f64`].
114    ///
115    /// Named for callers whose sources cannot contain hardware float tokens.
116    pub fn write_ieee64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
117        self.write_bits(path, shape, DTypeKind::Float64, lanes_u64(bits))
118    }
119
120    /// Write IEEE binary32 little-endian lanes.
121    ///
122    /// Errors: same as [`Self::write_f64`].
123    pub fn write_f32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
124        self.write_ieee32(path, shape, bits)
125    }
126
127    /// IEEE binary32 little-endian lanes. Same as [`Self::write_f32`].
128    pub fn write_ieee32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
129        self.write_bits(path, shape, DTypeKind::Float32, lanes_u32(bits))
130    }
131
132    /// Write opaque records of `elem_size` bytes. `data.len()` must equal
133    /// `shape.product() * elem_size`.
134    ///
135    /// Errors: same as [`Self::write_f64`].
136    pub fn write_opaque(
137        &mut self,
138        path: &str,
139        shape: &[usize],
140        elem_size: usize,
141        data: &[u8],
142    ) -> Result<()> {
143        self.write_bits(path, shape, DTypeKind::Opaque(elem_size), data.to_vec())
144    }
145
146    /// Write signed 8-bit lanes.
147    pub fn write_i8(&mut self, path: &str, shape: &[usize], bits: &[i8]) -> Result<()> {
148        self.write_bits(path, shape, DTypeKind::Int8, i8_lanes(bits))
149    }
150    /// Write signed 16-bit little-endian lanes.
151    pub fn write_i16(&mut self, path: &str, shape: &[usize], bits: &[i16]) -> Result<()> {
152        self.write_bits(path, shape, DTypeKind::Int16, i16_lanes(bits))
153    }
154    /// Write signed 32-bit little-endian lanes.
155    pub fn write_i32(&mut self, path: &str, shape: &[usize], bits: &[i32]) -> Result<()> {
156        self.write_bits(path, shape, DTypeKind::Int32, i32_lanes(bits))
157    }
158    /// Write signed 64-bit little-endian lanes.
159    pub fn write_i64(&mut self, path: &str, shape: &[usize], bits: &[i64]) -> Result<()> {
160        self.write_bits(path, shape, DTypeKind::Int64, i64_lanes(bits))
161    }
162    /// Write unsigned 8-bit lanes.
163    pub fn write_u8(&mut self, path: &str, shape: &[usize], bits: &[u8]) -> Result<()> {
164        self.write_bits(path, shape, DTypeKind::UInt8, bits.to_vec())
165    }
166    /// Write unsigned 16-bit little-endian lanes.
167    pub fn write_u16(&mut self, path: &str, shape: &[usize], bits: &[u16]) -> Result<()> {
168        self.write_bits(path, shape, DTypeKind::UInt16, u16_lanes(bits))
169    }
170    /// Write unsigned 32-bit little-endian lanes.
171    pub fn write_u32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
172        self.write_bits(path, shape, DTypeKind::UInt32, lanes_u32(bits))
173    }
174    /// Write unsigned 64-bit little-endian lanes.
175    pub fn write_u64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
176        self.write_bits(path, shape, DTypeKind::UInt64, lanes_u64(bits))
177    }
178
179    /// Read IEEE binary64 lanes and the dataspace shape.
180    ///
181    /// Errors: [`HDF5Error::NotFound`], [`HDF5Error::ChunkedNotSupported`],
182    /// [`HDF5Error::FilteredNotSupported`], [`HDF5Error::TypeMismatch`].
183    pub fn read_f64(&self, path: &str) -> Result<(Vec<usize>, Vec<u64>)> {
184        self.read_ieee64(path)
185    }
186
187    /// IEEE binary64 lanes and dataspace shape. Same as [`Self::read_f64`].
188    pub fn read_ieee64(&self, path: &str) -> Result<(Vec<usize>, Vec<u64>)> {
189        let ds = self.dataset(path)?;
190        if ds.kind != DTypeKind::Float64 {
191            return Err(HDF5Error::TypeMismatch);
192        }
193        let shape = ds.shape.iter().map(|d| *d as usize).collect();
194        Ok((shape, from_u64_le(&ds.data)))
195    }
196
197    /// Read IEEE binary32 lanes and the dataspace shape.
198    ///
199    /// Errors: same as [`Self::read_f64`].
200    pub fn read_f32(&self, path: &str) -> Result<(Vec<usize>, Vec<u32>)> {
201        self.read_ieee32(path)
202    }
203
204    /// IEEE binary32 lanes and dataspace shape. Same as [`Self::read_f32`].
205    pub fn read_ieee32(&self, path: &str) -> Result<(Vec<usize>, Vec<u32>)> {
206        let ds = self.dataset(path)?;
207        if ds.kind != DTypeKind::Float32 {
208            return Err(HDF5Error::TypeMismatch);
209        }
210        let shape = ds.shape.iter().map(|d| *d as usize).collect();
211        Ok((shape, from_u32_le(&ds.data)))
212    }
213
214    /// Read opaque payload, shape, and element size.
215    ///
216    /// Errors: [`HDF5Error::NotFound`], [`HDF5Error::ChunkedNotSupported`],
217    /// [`HDF5Error::TypeMismatch`].
218    pub fn read_opaque(&self, path: &str) -> Result<(Vec<usize>, usize, Vec<u8>)> {
219        let ds = self.dataset(path)?;
220        let DTypeKind::Opaque(sz) = ds.kind else {
221            return Err(HDF5Error::TypeMismatch);
222        };
223        let shape = ds.shape.iter().map(|d| *d as usize).collect();
224        Ok((shape, sz, ds.data.clone()))
225    }
226
227    /// Compound field layout for `path`, or empty if not a compound dataset.
228    ///
229    /// Errors: [`HDF5Error::NotFound`].
230    pub fn compound_fields(&self, path: &str) -> Result<Vec<crate::CompoundField>> {
231        let ds = self.dataset_meta(path)?;
232        Ok(ds.compound_fields.clone())
233    }
234
235    /// Raw element bytes for any dataset whose storage was materialized (including
236    /// compound / `Other`). Does not reinterpret lanes.
237    ///
238    /// Returns `(shape, element_size, bytes)`.
239    ///
240    /// Errors: [`HDF5Error::NotFound`], [`HDF5Error::ChunkedNotSupported`],
241    /// [`HDF5Error::FilteredNotSupported`].
242    pub fn read_raw(&self, path: &str) -> Result<(Vec<usize>, usize, Vec<u8>)> {
243        let ds = self.dataset(path)?;
244        let shape = ds.shape.iter().map(|d| *d as usize).collect();
245        Ok((shape, ds.kind.elem_size(), ds.data.clone()))
246    }
247
248    /// Read signed 8-bit lanes.
249    pub fn read_i8(&self, path: &str) -> Result<(Vec<usize>, Vec<i8>)> {
250        self.read_kind(path, DTypeKind::Int8, from_i8)
251    }
252    /// Read signed 16-bit little-endian lanes.
253    pub fn read_i16(&self, path: &str) -> Result<(Vec<usize>, Vec<i16>)> {
254        self.read_kind(path, DTypeKind::Int16, from_i16_le)
255    }
256    /// Read signed 32-bit little-endian lanes.
257    pub fn read_i32(&self, path: &str) -> Result<(Vec<usize>, Vec<i32>)> {
258        self.read_kind(path, DTypeKind::Int32, from_i32_le)
259    }
260    /// Read signed 64-bit little-endian lanes.
261    pub fn read_i64(&self, path: &str) -> Result<(Vec<usize>, Vec<i64>)> {
262        self.read_kind(path, DTypeKind::Int64, from_i64_le)
263    }
264    /// Read unsigned 8-bit lanes.
265    pub fn read_u8(&self, path: &str) -> Result<(Vec<usize>, Vec<u8>)> {
266        let ds = self.dataset(path)?;
267        if ds.kind != DTypeKind::UInt8 {
268            return Err(HDF5Error::TypeMismatch);
269        }
270        let shape = ds.shape.iter().map(|d| *d as usize).collect();
271        Ok((shape, ds.data.clone()))
272    }
273    /// Read unsigned 16-bit little-endian lanes.
274    pub fn read_u16(&self, path: &str) -> Result<(Vec<usize>, Vec<u16>)> {
275        self.read_kind(path, DTypeKind::UInt16, from_u16_le)
276    }
277    /// Read unsigned 32-bit little-endian lanes.
278    pub fn read_u32(&self, path: &str) -> Result<(Vec<usize>, Vec<u32>)> {
279        self.read_kind(path, DTypeKind::UInt32, from_u32_le)
280    }
281    /// Read unsigned 64-bit little-endian lanes.
282    pub fn read_u64(&self, path: &str) -> Result<(Vec<usize>, Vec<u64>)> {
283        self.read_kind(path, DTypeKind::UInt64, from_u64_le)
284    }
285
286    /// Rows `[row_start, row_end)` along the first axis of an IEEE binary64 dataset.
287    ///
288    /// Errors: [`HDF5Error::NotFound`], [`HDF5Error::ChunkedNotSupported`],
289    /// [`HDF5Error::TypeMismatch`], [`HDF5Error::ShapeMismatch`].
290    pub fn read_f64_slice(
291        &self,
292        path: &str,
293        row_start: usize,
294        row_end: usize,
295    ) -> Result<(Vec<usize>, Vec<u64>)> {
296        let (shape, bits) = self.read_f64(path)?;
297        if row_end < row_start {
298            return Err(HDF5Error::ShapeMismatch);
299        }
300        if shape.is_empty() {
301            return Err(HDF5Error::ShapeMismatch);
302        }
303        if row_end > shape[0] {
304            return Err(HDF5Error::ShapeMismatch);
305        }
306        let rest: usize = shape[1..]
307            .iter()
308            .try_fold(1usize, |a, b| a.checked_mul(*b))
309            .ok_or(HDF5Error::ShapeMismatch)?;
310        let start = row_start.saturating_mul(rest);
311        let end = row_end.saturating_mul(rest);
312        let mut out_shape = alloc::vec![row_end - row_start];
313        out_shape.extend_from_slice(&shape[1..]);
314        Ok((out_shape, bits[start..end].to_vec()))
315    }
316
317    /// Append rows to an existing IEEE binary64 dataset. Trailing dimensions
318    /// must match. Rank-1 appends to the single axis.
319    ///
320    /// Errors: [`HDF5Error::NotFound`], [`HDF5Error::ShapeMismatch`],
321    /// [`HDF5Error::TypeMismatch`], [`HDF5Error::ChunkedNotSupported`].
322    pub fn append_f64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
323        self.append_ieee64(path, shape, bits)
324    }
325
326    /// Append IEEE binary64 rows. Same as [`Self::append_f64`].
327    pub fn append_ieee64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
328        self.append_kind(
329            path,
330            shape,
331            DTypeKind::Float64,
332            &lanes_u64(bits),
333            bits.len(),
334        )
335    }
336
337    /// Append IEEE binary32 rows.
338    pub fn append_f32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
339        self.append_ieee32(path, shape, bits)
340    }
341
342    /// Append IEEE binary32 rows. Same as [`Self::append_f32`].
343    pub fn append_ieee32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
344        self.append_kind(
345            path,
346            shape,
347            DTypeKind::Float32,
348            &lanes_u32(bits),
349            bits.len(),
350        )
351    }
352
353    /// Append signed 32-bit rows.
354    pub fn append_i32(&mut self, path: &str, shape: &[usize], bits: &[i32]) -> Result<()> {
355        self.append_kind(path, shape, DTypeKind::Int32, &i32_lanes(bits), bits.len())
356    }
357
358    /// Append unsigned 64-bit rows.
359    pub fn append_u64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
360        self.append_kind(path, shape, DTypeKind::UInt64, &lanes_u64(bits), bits.len())
361    }
362
363    /// Append opaque records. `data.len()` must equal `shape.product() * elem_size`
364    /// of the existing dataset.
365    pub fn append_opaque(&mut self, path: &str, shape: &[usize], data: &[u8]) -> Result<()> {
366        let p = FileModel::normalize(path)?;
367        let elem = match self.model.datasets.get(&p) {
368            Some(ds) => ds.kind.elem_size(),
369            None => return Err(HDF5Error::NotFound(p)),
370        };
371        let n: usize = if shape.is_empty() {
372            1
373        } else {
374            shape
375                .iter()
376                .try_fold(1usize, |a, b| a.checked_mul(*b))
377                .ok_or(HDF5Error::ShapeMismatch)?
378        };
379        let need = n.checked_mul(elem).ok_or(HDF5Error::ShapeMismatch)?;
380        if data.len() != need {
381            return Err(HDF5Error::ShapeMismatch);
382        }
383        self.append_kind(path, shape, DTypeKind::Opaque(elem), data, n)
384    }
385
386    /// Read a string attribute on a dataset.
387    ///
388    /// Errors: [`HDF5Error::NotFound`].
389    pub fn read_attr_str(&self, path: &str, name: &str) -> Result<String> {
390        let ds = self.dataset_meta(path)?;
391        ds.attrs
392            .iter()
393            .find(|(n, _)| n == name)
394            .map(|(_, v)| v.clone())
395            .ok_or_else(|| HDF5Error::NotFound(name.to_string()))
396    }
397
398    /// String attributes on `path` (name, value), in file order.
399    pub fn list_attrs(&self, path: &str) -> Result<Vec<(String, String)>> {
400        Ok(self.dataset_meta(path)?.attrs.clone())
401    }
402
403    /// Write or replace a string attribute on a dataset.
404    ///
405    /// Errors: [`HDF5Error::NotFound`], [`HDF5Error::NameTooLong`].
406    pub fn write_attr_str(&mut self, path: &str, name: &str, value: &str) -> Result<()> {
407        if name.len() > HDF5_MAX_NAME_LEN || value.len() > HDF5_MAX_NAME_LEN {
408            return Err(HDF5Error::NameTooLong);
409        }
410        let p = FileModel::normalize(path)?;
411        let ds = self
412            .model
413            .datasets
414            .get_mut(&p)
415            .ok_or(HDF5Error::NotFound(p))?;
416        if let Some(slot) = ds.attrs.iter_mut().find(|(n, _)| n == name) {
417            slot.1 = value.to_string();
418        } else {
419            ds.attrs.push((name.to_string(), value.to_string()));
420        }
421        Ok(())
422    }
423
424    fn dataset_meta(&self, path: &str) -> Result<&DatasetRec> {
425        let p = FileModel::normalize(path)?;
426        self.model.datasets.get(&p).ok_or(HDF5Error::NotFound(p))
427    }
428
429    fn dataset(&self, path: &str) -> Result<&DatasetRec> {
430        let ds = self.dataset_meta(path)?;
431        if ds.filtered {
432            return Err(HDF5Error::FilteredNotSupported);
433        }
434        if ds.chunked {
435            return Err(HDF5Error::ChunkedNotSupported);
436        }
437        Ok(ds)
438    }
439
440    fn read_kind<T>(
441        &self,
442        path: &str,
443        kind: DTypeKind,
444        decode: fn(&[u8]) -> Vec<T>,
445    ) -> Result<(Vec<usize>, Vec<T>)> {
446        let ds = self.dataset(path)?;
447        if ds.kind != kind {
448            return Err(HDF5Error::TypeMismatch);
449        }
450        let shape = ds.shape.iter().map(|d| *d as usize).collect();
451        Ok((shape, decode(&ds.data)))
452    }
453
454    fn append_kind(
455        &mut self,
456        path: &str,
457        shape: &[usize],
458        kind: DTypeKind,
459        extra: &[u8],
460        n_elems: usize,
461    ) -> Result<()> {
462        let p = FileModel::normalize(path)?;
463        let ds = self
464            .model
465            .datasets
466            .get_mut(&p)
467            .ok_or_else(|| HDF5Error::NotFound(p.clone()))?;
468        if ds.filtered {
469            return Err(HDF5Error::FilteredNotSupported);
470        }
471        if ds.chunked {
472            return Err(HDF5Error::ChunkedNotSupported);
473        }
474        if ds.kind != kind {
475            return Err(HDF5Error::TypeMismatch);
476        }
477        let expect: usize = if shape.is_empty() {
478            1
479        } else {
480            shape
481                .iter()
482                .try_fold(1usize, |a, b| a.checked_mul(*b))
483                .ok_or(HDF5Error::ShapeMismatch)?
484        };
485        if n_elems != expect {
486            return Err(HDF5Error::ShapeMismatch);
487        }
488        if ds.shape.is_empty() {
489            return Err(HDF5Error::ShapeMismatch);
490        }
491        if ds.shape.len() != shape.len() {
492            return Err(HDF5Error::ShapeMismatch);
493        }
494        if ds.shape.len() >= 2
495            && ds.shape[1..] != shape[1..].iter().map(|d| *d as u64).collect::<Vec<_>>()
496        {
497            return Err(HDF5Error::ShapeMismatch);
498        }
499        ds.shape[0] = ds.shape[0].saturating_add(shape[0] as u64);
500        ds.data.extend_from_slice(extra);
501        Ok(())
502    }
503
504    fn write_bits(
505        &mut self,
506        path: &str,
507        shape: &[usize],
508        kind: DTypeKind,
509        data: Vec<u8>,
510    ) -> Result<()> {
511        if shape.len() > HDF5_MAX_DIMS {
512            return Err(HDF5Error::RankNotSupported);
513        }
514        let n: usize = if shape.is_empty() {
515            1
516        } else {
517            shape
518                .iter()
519                .try_fold(1usize, |a, b| a.checked_mul(*b))
520                .ok_or(HDF5Error::ShapeMismatch)?
521        };
522        let need = n
523            .checked_mul(kind.elem_size())
524            .ok_or(HDF5Error::ShapeMismatch)?;
525        if data.len() != need {
526            return Err(HDF5Error::ShapeMismatch);
527        }
528        let rec = DatasetRec {
529            shape: shape.iter().map(|d| *d as u64).collect(),
530            kind,
531            data,
532            attrs: FileModel::default_attrs(),
533            chunked: false,
534            filtered: false,
535            compound_fields: Vec::new(),
536        };
537        self.model.put_dataset(path, rec)
538    }
539}
540
541fn lanes_u64(bits: &[u64]) -> Vec<u8> {
542    let mut o = Vec::with_capacity(bits.len().saturating_mul(8));
543    for b in bits {
544        o.extend_from_slice(&b.to_le_bytes());
545    }
546    o
547}
548
549fn lanes_u32(bits: &[u32]) -> Vec<u8> {
550    let mut o = Vec::with_capacity(bits.len().saturating_mul(4));
551    for b in bits {
552        o.extend_from_slice(&b.to_le_bytes());
553    }
554    o
555}
556
557fn u16_lanes(bits: &[u16]) -> Vec<u8> {
558    let mut o = Vec::with_capacity(bits.len().saturating_mul(2));
559    for b in bits {
560        o.extend_from_slice(&b.to_le_bytes());
561    }
562    o
563}
564
565fn i8_lanes(bits: &[i8]) -> Vec<u8> {
566    bits.iter().map(|b| b.to_le_bytes()[0]).collect()
567}
568
569fn i16_lanes(bits: &[i16]) -> Vec<u8> {
570    let mut o = Vec::with_capacity(bits.len().saturating_mul(2));
571    for b in bits {
572        o.extend_from_slice(&b.to_le_bytes());
573    }
574    o
575}
576
577fn i32_lanes(bits: &[i32]) -> Vec<u8> {
578    let mut o = Vec::with_capacity(bits.len().saturating_mul(4));
579    for b in bits {
580        o.extend_from_slice(&b.to_le_bytes());
581    }
582    o
583}
584
585fn i64_lanes(bits: &[i64]) -> Vec<u8> {
586    let mut o = Vec::with_capacity(bits.len().saturating_mul(8));
587    for b in bits {
588        o.extend_from_slice(&b.to_le_bytes());
589    }
590    o
591}
592
593fn from_u64_le(data: &[u8]) -> Vec<u64> {
594    data.as_chunks::<8>()
595        .0
596        .iter()
597        .map(|c| u64::from_le_bytes(*c))
598        .collect()
599}
600
601fn from_u32_le(data: &[u8]) -> Vec<u32> {
602    data.as_chunks::<4>()
603        .0
604        .iter()
605        .map(|c| u32::from_le_bytes(*c))
606        .collect()
607}
608
609fn from_u16_le(data: &[u8]) -> Vec<u16> {
610    data.as_chunks::<2>()
611        .0
612        .iter()
613        .map(|c| u16::from_le_bytes(*c))
614        .collect()
615}
616
617fn from_i8(data: &[u8]) -> Vec<i8> {
618    data.iter().map(|b| i8::from_le_bytes([*b])).collect()
619}
620
621fn from_i16_le(data: &[u8]) -> Vec<i16> {
622    data.as_chunks::<2>()
623        .0
624        .iter()
625        .map(|c| i16::from_le_bytes(*c))
626        .collect()
627}
628
629fn from_i32_le(data: &[u8]) -> Vec<i32> {
630    data.as_chunks::<4>()
631        .0
632        .iter()
633        .map(|c| i32::from_le_bytes(*c))
634        .collect()
635}
636
637fn from_i64_le(data: &[u8]) -> Vec<i64> {
638    data.as_chunks::<8>()
639        .0
640        .iter()
641        .map(|c| i64::from_le_bytes(*c))
642        .collect()
643}
644
645/// Dataset paths in `file`.
646pub fn list_datasets(file: &Hdf5File) -> Vec<String> {
647    file.list_datasets()
648}
649
650/// Group paths in `file`.
651pub fn list_groups(file: &Hdf5File) -> Vec<String> {
652    file.list_groups()
653}
654
655/// Shape of dataset `path`.
656pub fn dataset_shape(file: &Hdf5File, path: &str) -> Result<Vec<usize>> {
657    file.dataset_shape(path)
658}
659
660/// Datatype of dataset `path`.
661pub fn dataset_dtype(file: &Hdf5File, path: &str) -> Result<HDF5DType> {
662    file.dataset_dtype(path)
663}