Skip to content

Commit 91a2186

Browse files
committed
Implement H5Type for complex
1 parent 66515f1 commit 91a2186

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

hdf5-types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ edition.workspace = true
1515

1616
[features]
1717
h5-alloc = []
18+
complex = ["num-complex"]
1819

1920
[dependencies]
2021
ascii = "1.1"
2122
cfg-if = { workspace = true }
2223
hdf5-sys = { workspace = true }
2324
libc = { workspace = true }
25+
num-complex = { version = "0.4", optional = true, default-features = false }
2426

2527
[dev-dependencies]
2628
quickcheck = { version = "1.0", default-features = false }

hdf5-types/src/complex.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use super::{CompoundField, CompoundType, H5Type, TypeDescriptor};
2+
3+
use std::mem::size_of;
4+
5+
use num_complex::Complex;
6+
7+
unsafe impl<T: H5Type> H5Type for Complex<T> {
8+
fn type_descriptor() -> TypeDescriptor {
9+
TypeDescriptor::Compound(CompoundType {
10+
fields: vec![
11+
// Compatible with h5py definition of complex
12+
CompoundField::typed::<T>("r", 0, 0),
13+
CompoundField::typed::<T>("i", size_of::<T>(), 1),
14+
],
15+
size: size_of::<T>() * 2,
16+
})
17+
}
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use super::*;
23+
use num_complex::{Complex32, Complex64};
24+
25+
#[test]
26+
fn complex() {
27+
assert_eq!(Complex32::type_descriptor().size(), 8);
28+
assert_eq!(Complex64::type_descriptor().size(), 16);
29+
}
30+
}

hdf5-types/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ pub mod dyn_value;
2222
mod h5type;
2323
mod string;
2424

25+
#[cfg(feature = "complex")]
26+
mod complex;
27+
2528
pub use self::array::VarLenArray;
2629
pub use self::dyn_value::{DynValue, OwnedDynValue};
2730
pub use self::h5type::{

0 commit comments

Comments
 (0)