Skip to content

Commit 3b57e59

Browse files
authored
Merge pull request #131 from mulimoen/feature/const_generics_hdf5types
use const generics in hdf5-types for array impl
2 parents 4762376 + 911bb7b commit 3b57e59

File tree

4 files changed

+78
-36
lines changed

4 files changed

+78
-36
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ jobs:
162162
run: |
163163
[ "${{matrix.mpi}}" != "serial" ] && FEATURES=mpio
164164
cargo test -vv --features="$FEATURES"
165+
- name: Test const generics
166+
if: matrix.rust == 'nightly'
167+
run: cargo test -p hdf5-types --features hdf5-types/const_generics
165168

166169
msi:
167170
name: msi

hdf5-types/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ repository = "https://github.com/aldanor/hdf5-rust"
99
homepage = "https://github.com/aldanor/hdf5-rust"
1010
edition = "2018"
1111

12+
[features]
13+
const_generics = []
14+
1215
[dependencies]
1316
ascii = "1.0"
1417
libc = "0.2"

hdf5-types/src/array.rs

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,78 @@ pub unsafe trait Array: 'static {
1414
fn capacity() -> usize;
1515
}
1616

17-
macro_rules! impl_array {
18-
() => ();
17+
#[cfg(not(feature = "const_generics"))]
18+
mod impl_array {
19+
use super::*;
20+
21+
macro_rules! impl_array {
22+
() => ();
23+
24+
($n:expr, $($ns:expr,)*) => (
25+
unsafe impl<T: 'static> Array for [T; $n] {
26+
type Item = T;
27+
28+
#[inline(always)]
29+
fn as_ptr(&self) -> *const T {
30+
self as *const _ as *const _
31+
}
32+
33+
#[inline(always)]
34+
fn as_mut_ptr(&mut self) -> *mut T {
35+
self as *mut _ as *mut _
36+
}
37+
38+
#[inline(always)]
39+
fn capacity() -> usize {
40+
$n
41+
}
42+
}
1943

20-
($n:expr, $($ns:expr,)*) => (
21-
unsafe impl<T: 'static> Array for [T; $n] {
22-
type Item = T;
44+
impl_array!($($ns,)*);
45+
);
46+
}
2347

24-
#[inline(always)]
25-
fn as_ptr(&self) -> *const T {
26-
self as *const _ as *const _
27-
}
48+
impl_array!(
49+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
50+
25, 26, 27, 28, 29, 30, 31,
51+
);
52+
impl_array!(
53+
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
54+
55, 56, 57, 58, 59, 60, 61, 62, 63,
55+
);
56+
impl_array!(
57+
64, 70, 72, 80, 90, 96, 100, 110, 120, 128, 130, 140, 150, 160, 170, 180, 190, 192, 200,
58+
210, 220, 224, 230, 240, 250,
59+
);
60+
impl_array!(
61+
256, 300, 365, 366, 384, 400, 500, 512, 600, 700, 768, 800, 900, 1000, 1024, 2048, 4096,
62+
8192, 16384, 32768,
63+
);
64+
}
2865

29-
#[inline(always)]
30-
fn as_mut_ptr(&mut self) -> *mut T {
31-
self as *mut _ as *mut _
32-
}
66+
#[cfg(feature = "const_generics")]
67+
mod impl_array {
68+
use super::*;
3369

34-
#[inline(always)]
35-
fn capacity() -> usize {
36-
$n
37-
}
70+
unsafe impl<T: 'static, const N: usize> Array for [T; N] {
71+
type Item = T;
72+
73+
#[inline(always)]
74+
fn as_ptr(&self) -> *const T {
75+
self as *const _
3876
}
3977

40-
impl_array!($($ns,)*);
41-
);
42-
}
78+
#[inline(always)]
79+
fn as_mut_ptr(&mut self) -> *mut T {
80+
self as *mut _ as *mut _
81+
}
4382

44-
impl_array!(
45-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
46-
26, 27, 28, 29, 30, 31,
47-
);
48-
impl_array!(
49-
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
50-
56, 57, 58, 59, 60, 61, 62, 63,
51-
);
52-
impl_array!(
53-
64, 70, 72, 80, 90, 96, 100, 110, 120, 128, 130, 140, 150, 160, 170, 180, 190, 192, 200, 210,
54-
220, 224, 230, 240, 250,
55-
);
56-
impl_array!(
57-
256, 300, 384, 400, 500, 512, 600, 700, 768, 800, 900, 1000, 1024, 2048, 4096, 8192, 16384,
58-
32768,
59-
);
83+
#[inline(always)]
84+
fn capacity() -> usize {
85+
N
86+
}
87+
}
88+
}
6089

6190
#[repr(C)]
6291
pub struct VarLenArray<T: Copy> {

hdf5-types/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#![recursion_limit = "1024"]
22
#![cfg_attr(feature = "cargo-clippy", allow(clippy::missing_safety_doc))]
33

4+
//! Types that can be stored and retrieved from a `HDF5` dataset
5+
//!
6+
//! Crate features:
7+
//! * `const_generics`: Uses const generics to enable arrays [T; N] for all N.
8+
//! Compiling without this limits arrays to certain prespecified
9+
//! sizes
10+
411
#[cfg(test)]
512
#[macro_use]
613
extern crate quickcheck;

0 commit comments

Comments
 (0)