@@ -14,15 +14,21 @@ pub(crate) struct hvl_t {
1414 pub ptr : * mut c_void ,
1515}
1616
17+ /// A valid integer size.
1718#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
1819pub enum IntSize {
20+ /// 1 byte.
1921 U1 = 1 ,
22+ /// 2 bytes.
2023 U2 = 2 ,
24+ /// 4 bytes.
2125 U4 = 4 ,
26+ /// 8 bytes.
2227 U8 = 8 ,
2328}
2429
2530impl IntSize {
31+ /// Returns an `IntSize` of `size` bytes, or `None` if `size` is invalid.
2632 pub const fn from_int ( size : usize ) -> Option < Self > {
2733 if size == 1 {
2834 Some ( Self :: U1 )
@@ -38,15 +44,20 @@ impl IntSize {
3844 }
3945}
4046
47+ /// A valid floating-point number size.
4148#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
4249pub enum FloatSize {
50+ /// 2 bytes.
4351 #[ cfg( feature = "f16" ) ]
4452 U2 = 2 ,
53+ /// 4 bytes.
4554 U4 = 4 ,
55+ /// 8 bytes.
4656 U8 = 8 ,
4757}
4858
4959impl FloatSize {
60+ /// Returns a `FloatSize` of `size` bytes, or `None` if `size` is invalid.
5061 pub const fn from_int ( size : usize ) -> Option < Self > {
5162 #[ cfg( feature = "f16" ) ]
5263 {
@@ -62,20 +73,28 @@ impl FloatSize {
6273 }
6374}
6475
76+ /// A descriptor for an enumeration datatype member.
6577#[ derive( Clone , Debug , PartialEq , Eq ) ]
6678pub struct EnumMember {
79+ /// The name of the member.
6780 pub name : String ,
81+ /// The value of the member.
6882 pub value : u64 ,
6983}
7084
85+ /// A descriptor for an enumeration datatype.
7186#[ derive( Clone , Debug , PartialEq , Eq ) ]
7287pub struct EnumType {
88+ /// The size of the underlying integer type.
7389 pub size : IntSize ,
90+ /// Whether to use a signed integer.
7491 pub signed : bool ,
92+ /// The enumeration datatype members.
7593 pub members : Vec < EnumMember > ,
7694}
7795
7896impl EnumType {
97+ /// Returns the type descriptor of the underlying integer datatype.
7998 #[ inline]
8099 pub fn base_type ( & self ) -> TypeDescriptor {
81100 if self . signed {
@@ -86,31 +105,42 @@ impl EnumType {
86105 }
87106}
88107
108+ /// A descriptor for a compound datatype field.
89109#[ derive( Clone , Debug , PartialEq , Eq ) ]
90110pub struct CompoundField {
111+ /// The name of the field.
91112 pub name : String ,
113+ /// The type of the field.
92114 pub ty : TypeDescriptor ,
115+ /// The byte offset of the field.
93116 pub offset : usize ,
117+ /// The ordering of the field within the compound type.
94118 pub index : usize ,
95119}
96120
97121impl CompoundField {
122+ /// Creates a new `CompoundField`.
98123 pub fn new ( name : & str , ty : TypeDescriptor , offset : usize , index : usize ) -> Self {
99124 Self { name : name. to_owned ( ) , ty, offset, index }
100125 }
101126
127+ /// Creates a new `CompoundField` for a concrete type.
102128 pub fn typed < T : H5Type > ( name : & str , offset : usize , index : usize ) -> Self {
103129 Self :: new ( name, T :: type_descriptor ( ) , offset, index)
104130 }
105131}
106132
133+ /// A descriptor for a compound datatype.
107134#[ derive( Clone , Debug , PartialEq , Eq ) ]
108135pub struct CompoundType {
136+ /// The fields of the datatype.
109137 pub fields : Vec < CompoundField > ,
138+ /// The size in bytes of the datatype.
110139 pub size : usize ,
111140}
112141
113142impl CompoundType {
143+ /// Converts `self` to a C struct representation.
114144 pub fn to_c_repr ( & self ) -> Self {
115145 let mut layout = self . clone ( ) ;
116146 layout. fields . sort_by_key ( |f| f. index ) ;
@@ -133,6 +163,7 @@ impl CompoundType {
133163 layout
134164 }
135165
166+ /// Converts `self` to a packed representation.
136167 pub fn to_packed_repr ( & self ) -> Self {
137168 let mut layout = self . clone ( ) ;
138169 layout. fields . sort_by_key ( |f| f. index ) ;
@@ -146,19 +177,32 @@ impl CompoundType {
146177 }
147178}
148179
180+ /// A descriptor for an HDF5 datatype.
149181#[ derive( Clone , Debug , PartialEq , Eq ) ]
150182pub enum TypeDescriptor {
183+ /// A signed integer.
151184 Integer ( IntSize ) ,
185+ /// An unsigned integer.
152186 Unsigned ( IntSize ) ,
187+ /// A floating-point number.
153188 Float ( FloatSize ) ,
189+ /// A boolean value.
154190 Boolean ,
191+ /// An enumeration datatype.
155192 Enum ( EnumType ) ,
193+ /// A compound datatype.
156194 Compound ( CompoundType ) ,
195+ /// A fixed-length array.
157196 FixedArray ( Box < Self > , usize ) ,
197+ /// A fixed-length ASCII string.
158198 FixedAscii ( usize ) ,
199+ /// A fixed-length UTF-8 string.
159200 FixedUnicode ( usize ) ,
201+ /// A variable-length array.
160202 VarLenArray ( Box < Self > ) ,
203+ /// A variable-length ASCII string.
161204 VarLenAscii ,
205+ /// A variable-length UTF-8 string.
162206 VarLenUnicode ,
163207}
164208
@@ -191,6 +235,7 @@ impl Display for TypeDescriptor {
191235}
192236
193237impl TypeDescriptor {
238+ /// Returns the size of the type in bytes.
194239 pub fn size ( & self ) -> usize {
195240 match * self {
196241 Self :: Integer ( size) | Self :: Unsigned ( size) => size as _ ,
@@ -217,6 +262,7 @@ impl TypeDescriptor {
217262 }
218263 }
219264
265+ /// Converts `self` to a C-compatible representation.
220266 pub fn to_c_repr ( & self ) -> Self {
221267 match * self {
222268 Self :: Compound ( ref compound) => Self :: Compound ( compound. to_c_repr ( ) ) ,
@@ -226,6 +272,7 @@ impl TypeDescriptor {
226272 }
227273 }
228274
275+ /// Converts `self` to a packed representation.
229276 pub fn to_packed_repr ( & self ) -> Self {
230277 match * self {
231278 Self :: Compound ( ref compound) => Self :: Compound ( compound. to_packed_repr ( ) ) ,
@@ -236,7 +283,18 @@ impl TypeDescriptor {
236283 }
237284}
238285
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.
239296pub unsafe trait H5Type : ' static {
297+ /// Returns a descriptor for an equivalent HDF5 datatype.
240298 fn type_descriptor ( ) -> TypeDescriptor ;
241299}
242300
0 commit comments