Skip to content

Commit 9969b86

Browse files
committed
Add backwards compatibility
1 parent 9cfff46 commit 9969b86

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Changelog
22

3-
## hdf5 unreleased
43
## hdf5-types unreleased
54
## hdf5-derive unreleased
65
## hdf5-sys unreleased
76
## hdf5-src unreleased
87

8+
## hdf5 unreleased
9+
- Added support for Single Writer Multiple Readers (SWMR) (breaking change, OpenMode has extra variant)
10+
911
## hdf5-types v0.10.1
1012
Release date: Mar 19, 2025
1113
- Fixed deref of null ptr in `VarLenAscii`/`VarLenUnicode`

hdf5/examples/swmr.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#[cfg(feature = "1.10.0")]
12
use hdf5_metno as hdf5;
23

4+
#[cfg(feature = "1.10.0")]
35
fn reader() {
46
let file = hdf5::File::open_as("swmr.h5", hdf5::OpenMode::ReadSWMR).unwrap();
57
println!("Reader: Opened file");
@@ -17,23 +19,31 @@ fn reader() {
1719
}
1820

1921
fn main() {
20-
let file =
21-
hdf5::File::with_options().with_fapl(|fapl| fapl.libver_v110()).create("swmr.h5").unwrap();
22+
#[cfg(not(feature = "1.10.0"))]
23+
println("This examples requires hdf5 >= 1.10.0");
2224

23-
let var = file.new_dataset::<u8>().shape((0.., 5)).create("foo").unwrap();
25+
#[cfg(feature = "1.10.0")]
26+
{
27+
let file = hdf5::File::with_options()
28+
.with_fapl(|fapl| fapl.libver_v110())
29+
.create("swmr.h5")
30+
.unwrap();
2431

25-
file.start_swmr().unwrap();
26-
println!("Writer: Wrote file");
32+
let var = file.new_dataset::<u8>().shape((0.., 5)).create("foo").unwrap();
2733

28-
let thread = std::thread::spawn(|| reader());
34+
file.start_swmr().unwrap();
35+
println!("Writer: Wrote file");
2936

30-
for i in 0..5 {
31-
var.resize((i + 1, 5)).unwrap();
32-
var.write_slice(&[i, i, i, i, i], (i, 0..)).unwrap();
33-
var.flush().unwrap();
34-
println!("Writer: Wrote {i}");
35-
std::thread::sleep(std::time::Duration::from_secs(5));
36-
}
37+
let thread = std::thread::spawn(|| reader());
3738

38-
thread.join().unwrap();
39+
for i in 0..5 {
40+
var.resize((i + 1, 5)).unwrap();
41+
var.write_slice(&[i, i, i, i, i], (i, 0..)).unwrap();
42+
var.flush().unwrap();
43+
println!("Writer: Wrote {i}");
44+
std::thread::sleep(std::time::Duration::from_secs(5));
45+
}
46+
47+
thread.join().unwrap();
48+
}
3949
}

hdf5/src/hl/dataset.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ use ndarray::{self, ArrayView};
55

66
use hdf5_sys::h5::HADDR_UNDEF;
77
use hdf5_sys::h5d::{
8-
H5Dcreate2, H5Dcreate_anon, H5Dflush, H5Dget_access_plist, H5Dget_create_plist, H5Dget_offset,
9-
H5Drefresh, H5Dset_extent,
8+
H5Dcreate2, H5Dcreate_anon, H5Dget_access_plist, H5Dget_create_plist, H5Dget_offset,
9+
H5Dset_extent,
1010
};
11+
#[cfg(feature = "1.10.0")]
12+
use hdf5_sys::h5d::{H5Dflush, H5Drefresh};
1113
use hdf5_sys::h5l::H5Ldelete;
1214
use hdf5_sys::h5p::H5P_DEFAULT;
1315
use hdf5_sys::h5z::H5Z_filter_t;
@@ -156,13 +158,15 @@ impl Dataset {
156158
}
157159

158160
/// Flush the dataset metadata from the metadata cache to the file
161+
#[cfg(feature = "1.10.0")]
159162
pub fn flush(&self) -> Result<()> {
160163
let id = self.id();
161164
h5call!(H5Dflush(id))?;
162165
Ok(())
163166
}
164167

165168
/// Refresh metadata items assosicated with the dataset
169+
#[cfg(feature = "1.10.0")]
166170
pub fn refresh(&self) -> Result<()> {
167171
let id = self.id();
168172
h5call!(H5Drefresh(id))?;

hdf5/src/hl/file.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use std::path::Path;
55

66
use hdf5_sys::h5f::{
77
H5Fclose, H5Fcreate, H5Fflush, H5Fget_access_plist, H5Fget_create_plist, H5Fget_filesize,
8-
H5Fget_freespace, H5Fget_intent, H5Fget_obj_count, H5Fget_obj_ids, H5Fopen,
9-
H5Fstart_swmr_write, H5F_ACC_DEFAULT, H5F_ACC_EXCL, H5F_ACC_RDONLY, H5F_ACC_RDWR,
10-
H5F_ACC_SWMR_READ, H5F_ACC_TRUNC, H5F_SCOPE_LOCAL,
8+
H5Fget_freespace, H5Fget_intent, H5Fget_obj_count, H5Fget_obj_ids, H5Fopen, H5F_ACC_DEFAULT,
9+
H5F_ACC_EXCL, H5F_ACC_RDONLY, H5F_ACC_RDWR, H5F_ACC_TRUNC, H5F_SCOPE_LOCAL,
1110
};
11+
#[cfg(feature = "1.10.0")]
12+
use hdf5_sys::h5f::{H5Fstart_swmr_write, H5F_ACC_SWMR_READ};
1213

1314
use crate::hl::plist::{
1415
file_access::{FileAccess, FileAccessBuilder},
@@ -18,10 +19,12 @@ use crate::internal_prelude::*;
1819

1920
/// File opening mode.
2021
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22+
#[cfg_attr(not(feature = "1.10.0"), non_exhaustive)]
2123
pub enum OpenMode {
2224
/// Open a file as read-only, file must exist.
2325
Read,
2426
/// Open a file as read-only in SWMR mode, file must exist.
27+
#[cfg(feature = "1.10.0")]
2528
ReadSWMR,
2629
/// Open a file as read/write, file must exist.
2730
ReadWrite,
@@ -182,6 +185,8 @@ impl File {
182185
self.create_plist()
183186
}
184187

188+
#[cfg(feature = "1.10.0")]
189+
/// Mark this file as ready for opening as SWMR
185190
pub fn start_swmr(&self) -> Result<()> {
186191
let id = self.id();
187192
h5call!(H5Fstart_swmr_write(id))?;
@@ -240,18 +245,23 @@ impl FileBuilder {
240245
)?;
241246
let flags = match mode {
242247
OpenMode::Read => H5F_ACC_RDONLY,
248+
#[cfg(feature = "1.10.0")]
243249
OpenMode::ReadSWMR => H5F_ACC_RDONLY | H5F_ACC_SWMR_READ,
244250
OpenMode::ReadWrite => H5F_ACC_RDWR,
245251
OpenMode::Create => H5F_ACC_TRUNC,
246252
OpenMode::CreateExcl | OpenMode::Append => H5F_ACC_EXCL,
253+
#[cfg(not(feature = "1.10.0"))]
254+
_ => unreachable!(),
247255
};
248256
let fname_ptr = filename.as_ptr();
249257
h5lock!({
250258
let fapl = self.fapl.finish()?;
251259
match mode {
252-
OpenMode::Read | OpenMode::ReadWrite | OpenMode::ReadSWMR => {
260+
OpenMode::Read | OpenMode::ReadWrite => {
253261
File::from_id(h5try!(H5Fopen(fname_ptr, flags, fapl.id())))
254262
}
263+
#[cfg(feature = "1.10.0")]
264+
OpenMode::ReadSWMR => File::from_id(h5try!(H5Fopen(fname_ptr, flags, fapl.id()))),
255265
_ => {
256266
let fcpl = self.fcpl.finish()?;
257267
File::from_id(h5try!(H5Fcreate(fname_ptr, flags, fcpl.id(), fapl.id())))

0 commit comments

Comments
 (0)