Skip to content

Commit f10032b

Browse files
committed
Add minimal docs for public items
1 parent 4a9b537 commit f10032b

26 files changed

+771
-10
lines changed

hdf5-derive/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use syn::{
1212
TypeGenerics, TypePath,
1313
};
1414

15+
/// Derive macro generating an impl of the trait `H5Type`.
1516
#[proc_macro_derive(H5Type, attributes(hdf5))]
1617
#[proc_macro_error]
1718
pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

hdf5-types/src/array.rs

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::ops::Deref;
55
use std::ptr;
66
use std::slice;
77

8+
/// A variable-length array.
89
#[repr(C)]
910
pub struct VarLenArray<T: Copy> {
1011
len: usize,
@@ -13,6 +14,14 @@ pub struct VarLenArray<T: Copy> {
1314
}
1415

1516
impl<T: Copy> VarLenArray<T> {
17+
/// Creates a `VarLenArray<T>` by copying the first `len` elements stored at `p`.
18+
///
19+
/// Returns an empty array if `p` is null.
20+
///
21+
/// # Safety
22+
///
23+
/// - `p` must be valid for reads of `len * size_of::<T>()` bytes.
24+
/// - `p` must point to `len` consecutive properly initialized and aligned values of type `T`.
1625
pub unsafe fn from_parts(p: *const T, len: usize) -> Self {
1726
let (len, ptr) = if !p.is_null() && len != 0 {
1827
let dst = crate::malloc(len * mem::size_of::<T>());
@@ -24,26 +33,31 @@ impl<T: Copy> VarLenArray<T> {
2433
Self { len, ptr: ptr as *const _, tag: PhantomData }
2534
}
2635

36+
/// Creates a `VarLenArray<T>` from a slice by copying its elements.
2737
#[inline]
2838
pub fn from_slice(arr: &[T]) -> Self {
2939
unsafe { Self::from_parts(arr.as_ptr(), arr.len()) }
3040
}
3141

42+
/// Returns a raw pointer to the array's buffer.
3243
#[inline]
3344
pub fn as_ptr(&self) -> *const T {
3445
self.ptr
3546
}
3647

48+
/// Returns the number of elements in the array.
3749
#[inline]
3850
pub fn len(&self) -> usize {
3951
self.len as _
4052
}
4153

54+
/// Returns `true` if the array has a length of zero.
4255
#[inline]
4356
pub fn is_empty(&self) -> bool {
4457
self.len == 0
4558
}
4659

60+
/// Returns a slice containing the entire array.
4761
#[inline]
4862
pub fn as_slice(&self) -> &[T] {
4963
self

hdf5-types/src/dyn_value.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Dynamically-typed values.
2+
13
use std::fmt::{self, Debug, Display};
24
use std::mem;
35
use std::ptr;
@@ -26,6 +28,7 @@ unsafe trait DynClone {
2628
fn dyn_clone(&mut self, out: &mut [u8]);
2729
}
2830

31+
/// A dynamically-typed integer.
2932
#[derive(Copy, Clone, PartialEq, Eq)]
3033
pub enum DynInteger {
3134
Int8(i8),
@@ -114,6 +117,7 @@ impl From<DynInteger> for DynValue<'_> {
114117
}
115118
}
116119

120+
/// A dynamically-typed floating-point value.
117121
#[derive(Copy, Clone, PartialEq)]
118122
pub enum DynFloat {
119123
#[cfg(feature = "f16")]
@@ -173,6 +177,7 @@ impl From<DynFloat> for DynValue<'_> {
173177
}
174178
}
175179

180+
/// A dynamically-typed scalar value.
176181
#[derive(Copy, Clone, PartialEq)]
177182
pub enum DynScalar {
178183
Integer(DynInteger),
@@ -212,6 +217,7 @@ impl From<DynScalar> for DynValue<'static> {
212217
}
213218
}
214219

220+
/// A dynamically-typed enumeration value.
215221
#[derive(Copy, Clone)]
216222
pub struct DynEnum<'a> {
217223
tp: &'a EnumType,
@@ -269,6 +275,7 @@ impl<'a> From<DynEnum<'a>> for DynValue<'a> {
269275
}
270276
}
271277

278+
/// A dynamically-typed compound value.
272279
pub struct DynCompound<'a> {
273280
tp: &'a CompoundType,
274281
buf: &'a [u8],
@@ -354,6 +361,7 @@ impl<'a> From<DynCompound<'a>> for DynValue<'a> {
354361
}
355362
}
356363

364+
/// A dynamically-typed array.
357365
pub struct DynArray<'a> {
358366
tp: &'a TypeDescriptor,
359367
buf: &'a [u8],
@@ -470,6 +478,7 @@ impl<'a> From<DynArray<'a>> for DynValue<'a> {
470478
}
471479
}
472480

481+
/// A fixed-length string with a dynamic encoding.
473482
pub struct DynFixedString<'a> {
474483
buf: &'a [u8],
475484
unicode: bool,
@@ -529,6 +538,7 @@ impl<'a> From<DynFixedString<'a>> for DynValue<'a> {
529538
}
530539
}
531540

541+
/// A variable-length string with a dynamic encoding.
532542
pub struct DynVarLenString<'a> {
533543
buf: &'a [u8],
534544
unicode: bool,
@@ -633,6 +643,7 @@ impl<'a> From<DynVarLenString<'a>> for DynValue<'a> {
633643
}
634644
}
635645

646+
/// A dynamically-typed string.
636647
#[derive(PartialEq, Eq)]
637648
pub enum DynString<'a> {
638649
Fixed(DynFixedString<'a>),
@@ -677,6 +688,7 @@ impl<'a> From<DynString<'a>> for DynValue<'a> {
677688
}
678689
}
679690

691+
/// A borrowed value with dynamic type.
680692
#[derive(PartialEq)]
681693
pub enum DynValue<'a> {
682694
Scalar(DynScalar),
@@ -687,6 +699,7 @@ pub enum DynValue<'a> {
687699
}
688700

689701
impl<'a> DynValue<'a> {
702+
/// Constructs a new `DynValue` from a `TypeDescriptor` and a byte slice.
690703
pub fn new(tp: &'a TypeDescriptor, buf: &'a [u8]) -> Self {
691704
use TypeDescriptor::*;
692705
debug_assert_eq!(tp.size(), buf.len());
@@ -748,12 +761,14 @@ impl Display for DynValue<'_> {
748761
}
749762
}
750763

764+
/// An owned value with dynamic type.
751765
pub struct OwnedDynValue {
752766
tp: TypeDescriptor,
753767
buf: Box<[u8]>,
754768
}
755769

756770
impl OwnedDynValue {
771+
/// Constructs a new `OwnedDynValue` from the given value.
757772
pub fn new<T: H5Type>(value: T) -> Self {
758773
let ptr = (&value as *const T).cast::<u8>();
759774
let len = mem::size_of_val(&value);
@@ -762,10 +777,12 @@ impl OwnedDynValue {
762777
Self { tp: T::type_descriptor(), buf: buf.to_owned().into_boxed_slice() }
763778
}
764779

780+
/// Returns a borrowed version of the contained value.
765781
pub fn get(&self) -> DynValue {
766782
DynValue::new(&self.tp, &self.buf)
767783
}
768784

785+
/// Returns the value's type descriptor.
769786
pub fn type_descriptor(&self) -> &TypeDescriptor {
770787
&self.tp
771788
}
@@ -780,9 +797,12 @@ impl OwnedDynValue {
780797
Self { tp, buf }
781798
}
782799

783-
/// Cast to the concrete type
800+
/// Tries to downcast the value to a concrete type.
801+
///
802+
/// # Errors
784803
///
785-
/// Will fail if the type-descriptors are not equal
804+
/// If the type descriptors of `self` and `T` are not equal, this will fail and return a
805+
/// `Result::Err` containing the original value.
786806
pub fn cast<T: H5Type>(mut self) -> Result<T, Self> {
787807
use mem::MaybeUninit;
788808
if self.tp != T::type_descriptor() {

hdf5-types/src/h5type.rs

+58
Original file line numberDiff line numberDiff line change
@@ -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)]
1819
pub 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

2530
impl 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)]
4249
pub 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

4959
impl 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)]
6678
pub 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)]
7287
pub 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

7896
impl 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)]
90110
pub 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

97121
impl 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)]
108135
pub 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

113142
impl 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)]
150182
pub 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

193237
impl 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.
239296
pub unsafe trait H5Type: 'static {
297+
/// Returns a descriptor for an equivalent HDF5 datatype.
240298
fn type_descriptor() -> TypeDescriptor;
241299
}
242300

0 commit comments

Comments
 (0)