@@ -14,15 +14,21 @@ pub(crate) struct hvl_t {
14
14
pub ptr : * mut c_void ,
15
15
}
16
16
17
+ /// A valid integer size.
17
18
#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
18
19
pub enum IntSize {
20
+ /// 1 byte.
19
21
U1 = 1 ,
22
+ /// 2 bytes.
20
23
U2 = 2 ,
24
+ /// 4 bytes.
21
25
U4 = 4 ,
26
+ /// 8 bytes.
22
27
U8 = 8 ,
23
28
}
24
29
25
30
impl IntSize {
31
+ /// Returns an `IntSize` of `size` bytes, or `None` if `size` is invalid.
26
32
pub const fn from_int ( size : usize ) -> Option < Self > {
27
33
if size == 1 {
28
34
Some ( Self :: U1 )
@@ -38,15 +44,20 @@ impl IntSize {
38
44
}
39
45
}
40
46
47
+ /// A valid floating-point number size.
41
48
#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
42
49
pub enum FloatSize {
50
+ /// 2 bytes.
43
51
#[ cfg( feature = "f16" ) ]
44
52
U2 = 2 ,
53
+ /// 4 bytes.
45
54
U4 = 4 ,
55
+ /// 8 bytes.
46
56
U8 = 8 ,
47
57
}
48
58
49
59
impl FloatSize {
60
+ /// Returns a `FloatSize` of `size` bytes, or `None` if `size` is invalid.
50
61
pub const fn from_int ( size : usize ) -> Option < Self > {
51
62
#[ cfg( feature = "f16" ) ]
52
63
{
@@ -62,20 +73,28 @@ impl FloatSize {
62
73
}
63
74
}
64
75
76
+ /// A descriptor for an enumeration datatype member.
65
77
#[ derive( Clone , Debug , PartialEq , Eq ) ]
66
78
pub struct EnumMember {
79
+ /// The name of the member.
67
80
pub name : String ,
81
+ /// The value of the member.
68
82
pub value : u64 ,
69
83
}
70
84
85
+ /// A descriptor for an enumeration datatype.
71
86
#[ derive( Clone , Debug , PartialEq , Eq ) ]
72
87
pub struct EnumType {
88
+ /// The size of the underlying integer type.
73
89
pub size : IntSize ,
90
+ /// Whether to use a signed integer.
74
91
pub signed : bool ,
92
+ /// The enumeration datatype members.
75
93
pub members : Vec < EnumMember > ,
76
94
}
77
95
78
96
impl EnumType {
97
+ /// Returns the type descriptor of the underlying integer datatype.
79
98
#[ inline]
80
99
pub fn base_type ( & self ) -> TypeDescriptor {
81
100
if self . signed {
@@ -86,31 +105,42 @@ impl EnumType {
86
105
}
87
106
}
88
107
108
+ /// A descriptor for a compound datatype field.
89
109
#[ derive( Clone , Debug , PartialEq , Eq ) ]
90
110
pub struct CompoundField {
111
+ /// The name of the field.
91
112
pub name : String ,
113
+ /// The type of the field.
92
114
pub ty : TypeDescriptor ,
115
+ /// The byte offset of the field.
93
116
pub offset : usize ,
117
+ /// The ordering of the field within the compound type.
94
118
pub index : usize ,
95
119
}
96
120
97
121
impl CompoundField {
122
+ /// Creates a new `CompoundField`.
98
123
pub fn new ( name : & str , ty : TypeDescriptor , offset : usize , index : usize ) -> Self {
99
124
Self { name : name. to_owned ( ) , ty, offset, index }
100
125
}
101
126
127
+ /// Creates a new `CompoundField` for a concrete type.
102
128
pub fn typed < T : H5Type > ( name : & str , offset : usize , index : usize ) -> Self {
103
129
Self :: new ( name, T :: type_descriptor ( ) , offset, index)
104
130
}
105
131
}
106
132
133
+ /// A descriptor for a compound datatype.
107
134
#[ derive( Clone , Debug , PartialEq , Eq ) ]
108
135
pub struct CompoundType {
136
+ /// The fields of the datatype.
109
137
pub fields : Vec < CompoundField > ,
138
+ /// The size in bytes of the datatype.
110
139
pub size : usize ,
111
140
}
112
141
113
142
impl CompoundType {
143
+ /// Converts `self` to a C struct representation.
114
144
pub fn to_c_repr ( & self ) -> Self {
115
145
let mut layout = self . clone ( ) ;
116
146
layout. fields . sort_by_key ( |f| f. index ) ;
@@ -133,6 +163,7 @@ impl CompoundType {
133
163
layout
134
164
}
135
165
166
+ /// Converts `self` to a packed representation.
136
167
pub fn to_packed_repr ( & self ) -> Self {
137
168
let mut layout = self . clone ( ) ;
138
169
layout. fields . sort_by_key ( |f| f. index ) ;
@@ -146,19 +177,32 @@ impl CompoundType {
146
177
}
147
178
}
148
179
180
+ /// A descriptor for an HDF5 datatype.
149
181
#[ derive( Clone , Debug , PartialEq , Eq ) ]
150
182
pub enum TypeDescriptor {
183
+ /// A signed integer.
151
184
Integer ( IntSize ) ,
185
+ /// An unsigned integer.
152
186
Unsigned ( IntSize ) ,
187
+ /// A floating-point number.
153
188
Float ( FloatSize ) ,
189
+ /// A boolean value.
154
190
Boolean ,
191
+ /// An enumeration datatype.
155
192
Enum ( EnumType ) ,
193
+ /// A compound datatype.
156
194
Compound ( CompoundType ) ,
195
+ /// A fixed-length array.
157
196
FixedArray ( Box < Self > , usize ) ,
197
+ /// A fixed-length ASCII string.
158
198
FixedAscii ( usize ) ,
199
+ /// A fixed-length UTF-8 string.
159
200
FixedUnicode ( usize ) ,
201
+ /// A variable-length array.
160
202
VarLenArray ( Box < Self > ) ,
203
+ /// A variable-length ASCII string.
161
204
VarLenAscii ,
205
+ /// A variable-length UTF-8 string.
162
206
VarLenUnicode ,
163
207
}
164
208
@@ -191,6 +235,7 @@ impl Display for TypeDescriptor {
191
235
}
192
236
193
237
impl TypeDescriptor {
238
+ /// Returns the size of the type in bytes.
194
239
pub fn size ( & self ) -> usize {
195
240
match * self {
196
241
Self :: Integer ( size) | Self :: Unsigned ( size) => size as _ ,
@@ -217,6 +262,7 @@ impl TypeDescriptor {
217
262
}
218
263
}
219
264
265
+ /// Converts `self` to a C-compatible representation.
220
266
pub fn to_c_repr ( & self ) -> Self {
221
267
match * self {
222
268
Self :: Compound ( ref compound) => Self :: Compound ( compound. to_c_repr ( ) ) ,
@@ -226,6 +272,7 @@ impl TypeDescriptor {
226
272
}
227
273
}
228
274
275
+ /// Converts `self` to a packed representation.
229
276
pub fn to_packed_repr ( & self ) -> Self {
230
277
match * self {
231
278
Self :: Compound ( ref compound) => Self :: Compound ( compound. to_packed_repr ( ) ) ,
@@ -236,7 +283,18 @@ impl TypeDescriptor {
236
283
}
237
284
}
238
285
286
+ /// A type that can be represented as an HDF5 datatype.
287
+ ///
288
+ /// # Derivable
289
+ /// This trait can be used with `#[derive]`.
290
+ ///
291
+ /// To `derive` on structs, they must be one of: `repr(C)`, `repr(packed)`, or `repr(transparent)`,
292
+ /// and have at least one field, all of which must implement `H5Type`.
293
+ ///
294
+ /// To `derive` on enums, they must have an explicit `repr` and at least one variant, all of which
295
+ /// must be unit variants with explicit discriminants.
239
296
pub unsafe trait H5Type : ' static {
297
+ /// Returns a descriptor for an equivalent HDF5 datatype.
240
298
fn type_descriptor ( ) -> TypeDescriptor ;
241
299
}
242
300
0 commit comments