Skip to content

Commit 7c737df

Browse files
authored
Merge pull request #145 from mulimoen/feature/ArrayReplace
Replace Array trait with const generics
2 parents 63bb0b1 + d717f79 commit 7c737df

File tree

11 files changed

+90
-133
lines changed

11 files changed

+90
-133
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
- Const generics support (MSRV 1.51): `hdf5-types` now uses const generics for array types,
6161
allowing fixed-size arrays of arbitrary sizes.
6262
- The `ndarray` dependency has been updated to `0.15`.
63+
- `hdf5_types::Array` trait has been removed and replaced with const generics. String types
64+
are now generic over size only: `FixedAscii<N>` and `FixedUnicode<N>`.
6365

6466
## 0.7.1
6567

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ HDF5 for Rust.
66
[![Latest Version](https://img.shields.io/crates/v/hdf5.svg)](https://crates.io/crates/hdf5)
77
[![Documentation](https://docs.rs/hdf5/badge.svg)](https://docs.rs/hdf5)
88
[![Changelog](https://img.shields.io/github/v/release/aldanor/hdf5-rust)](https://github.com/aldanor/hdf5-rust/blob/master/CHANGELOG.md)
9+
![hdf5: rustc 1.51+](https://img.shields.io/badge/hdf5-rustc_1.51+-lightblue.svg)
910
[![Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
1011
[![MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
1112

@@ -89,7 +90,7 @@ toolchains; macOS Catalina).
8990
### Rust
9091

9192
`hdf5` crate is tested continuously for all three official release channels, and
92-
requires a reasonably recent Rust compiler (e.g. of version 1.40 or newer).
93+
requires a reasonably recent Rust compiler (e.g. of version 1.51 or newer).
9394

9495
### HDF5
9596

hdf5-derive/tests/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ struct A {
1818
#[repr(C)]
1919
struct B {
2020
a: [A; 4],
21-
b: FixedAscii<[u8; 8]>,
21+
b: FixedAscii<8>,
2222
c: VarLenArray<f64>,
2323
d: bool,
24-
e: FixedUnicode<[u8; 7]>,
24+
e: FixedUnicode<7>,
2525
f: VarLenAscii,
2626
g: VarLenUnicode,
2727
}

hdf5-types/src/array.rs

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,6 @@ use std::ops::Deref;
55
use std::ptr;
66
use std::slice;
77

8-
/* This trait is borrowed from arrayvec::Array (C) @bluss */
9-
pub unsafe trait Array: 'static {
10-
type Item;
11-
12-
fn as_ptr(&self) -> *const Self::Item;
13-
fn as_mut_ptr(&mut self) -> *mut Self::Item;
14-
fn capacity() -> usize;
15-
}
16-
17-
unsafe impl<T: 'static, const N: usize> Array for [T; N] {
18-
type Item = T;
19-
20-
#[inline(always)]
21-
fn as_ptr(&self) -> *const T {
22-
self as *const _
23-
}
24-
25-
#[inline(always)]
26-
fn as_mut_ptr(&mut self) -> *mut T {
27-
self as *mut _ as *mut _
28-
}
29-
30-
#[inline(always)]
31-
fn capacity() -> usize {
32-
N
33-
}
34-
}
35-
368
#[repr(C)]
379
pub struct VarLenArray<T: Copy> {
3810
len: usize,
@@ -126,10 +98,10 @@ impl<T: Copy> From<VarLenArray<T>> for Vec<T> {
12698
}
12799
}
128100

129-
impl<T: Copy, A: Array<Item = T>> From<A> for VarLenArray<T> {
101+
impl<T: Copy, const N: usize> From<[T; N]> for VarLenArray<T> {
130102
#[inline]
131-
fn from(arr: A) -> VarLenArray<T> {
132-
unsafe { VarLenArray::from_parts(arr.as_ptr(), A::capacity()) }
103+
fn from(arr: [T; N]) -> VarLenArray<T> {
104+
unsafe { VarLenArray::from_parts(arr.as_ptr(), arr.len()) }
133105
}
134106
}
135107

@@ -156,10 +128,10 @@ impl<T: Copy + PartialEq> PartialEq<[T]> for VarLenArray<T> {
156128
}
157129
}
158130

159-
impl<T: Copy + PartialEq, A: Array<Item = T>> PartialEq<A> for VarLenArray<T> {
131+
impl<T: Copy + PartialEq, const N: usize> PartialEq<[T; N]> for VarLenArray<T> {
160132
#[inline]
161-
fn eq(&self, other: &A) -> bool {
162-
self.as_slice() == unsafe { slice::from_raw_parts(other.as_ptr(), A::capacity()) }
133+
fn eq(&self, other: &[T; N]) -> bool {
134+
self.as_slice() == other
163135
}
164136
}
165137

@@ -172,19 +144,10 @@ impl<T: Copy + fmt::Debug> fmt::Debug for VarLenArray<T> {
172144

173145
#[cfg(test)]
174146
pub mod tests {
175-
use super::{Array, VarLenArray};
147+
use super::VarLenArray;
176148

177149
type S = VarLenArray<u16>;
178150

179-
#[test]
180-
pub fn test_array_trait() {
181-
type T = [u32; 256];
182-
assert_eq!(<T as Array>::capacity(), 256);
183-
let mut arr = [1, 2, 3];
184-
assert_eq!(arr.as_ptr(), &arr[0] as *const _);
185-
assert_eq!(arr.as_mut_ptr(), &mut arr[0] as *mut _);
186-
}
187-
188151
#[test]
189152
pub fn test_vla_empty_default() {
190153
assert_eq!(&*S::default(), &[]);

hdf5-types/src/dyn_value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,8 @@ mod tests {
803803
#[repr(C)]
804804
struct Data {
805805
points: VarLenArray<Point>,
806-
fa: FixedAscii<[u8; 5]>,
807-
fu: FixedUnicode<[u8; 5]>,
806+
fa: FixedAscii<5>,
807+
fu: FixedUnicode<5>,
808808
va: VarLenAscii,
809809
vu: VarLenUnicode,
810810
}

hdf5-types/src/h5type.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::mem;
33
use std::os::raw::c_void;
44
use std::ptr;
55

6-
use crate::array::{Array, VarLenArray};
6+
use crate::array::VarLenArray;
77
use crate::string::{FixedAscii, FixedUnicode, VarLenAscii, VarLenUnicode};
88

99
#[allow(non_camel_case_types)]
@@ -331,13 +331,10 @@ macro_rules! impl_tuple {
331331

332332
impl_tuple! { A, B, C, D, E, F, G, H, I, J, K, L }
333333

334-
unsafe impl<T: Array<Item = I>, I: H5Type> H5Type for T {
334+
unsafe impl<T: H5Type, const N: usize> H5Type for [T; N] {
335335
#[inline]
336336
fn type_descriptor() -> TypeDescriptor {
337-
TypeDescriptor::FixedArray(
338-
Box::new(<I as H5Type>::type_descriptor()),
339-
<T as Array>::capacity(),
340-
)
337+
TypeDescriptor::FixedArray(Box::new(<T as H5Type>::type_descriptor()), N)
341338
}
342339
}
343340

@@ -348,17 +345,17 @@ unsafe impl<T: Copy + H5Type> H5Type for VarLenArray<T> {
348345
}
349346
}
350347

351-
unsafe impl<A: Array<Item = u8>> H5Type for FixedAscii<A> {
348+
unsafe impl<const N: usize> H5Type for FixedAscii<N> {
352349
#[inline]
353350
fn type_descriptor() -> TypeDescriptor {
354-
TypeDescriptor::FixedAscii(A::capacity())
351+
TypeDescriptor::FixedAscii(N)
355352
}
356353
}
357354

358-
unsafe impl<A: Array<Item = u8>> H5Type for FixedUnicode<A> {
355+
unsafe impl<const N: usize> H5Type for FixedUnicode<N> {
359356
#[inline]
360357
fn type_descriptor() -> TypeDescriptor {
361-
TypeDescriptor::FixedUnicode(A::capacity())
358+
TypeDescriptor::FixedUnicode(N)
362359
}
363360
}
364361

@@ -439,8 +436,8 @@ pub mod tests {
439436

440437
#[test]
441438
pub fn test_string_types() {
442-
type FA = FixedAscii<[u8; 16]>;
443-
type FU = FixedUnicode<[u8; 32]>;
439+
type FA = FixedAscii<16>;
440+
type FU = FixedUnicode<32>;
444441
assert_eq!(FA::type_descriptor(), TD::FixedAscii(16));
445442
assert_eq!(FU::type_descriptor(), TD::FixedUnicode(32));
446443
assert_eq!(VarLenAscii::type_descriptor(), TD::VarLenAscii);

hdf5-types/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod dyn_value;
1919
mod h5type;
2020
mod string;
2121

22-
pub use self::array::{Array, VarLenArray};
22+
pub use self::array::VarLenArray;
2323
pub use self::dyn_value::{DynValue, OwnedDynValue};
2424
pub use self::h5type::{
2525
CompoundField, CompoundType, EnumMember, EnumType, FloatSize, H5Type, IntSize, TypeDescriptor,

0 commit comments

Comments
 (0)