Skip to content

Commit e41a32f

Browse files
committed
Add docstrings for typedesc
Signed-off-by: Scott Wilson <scott@propersquid.com>
1 parent 74b7b42 commit e41a32f

File tree

1 file changed

+154
-1
lines changed

1 file changed

+154
-1
lines changed

src/rust/oiio-sys/src/typedesc.rs

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
pub use ffi::*;
22

3+
/// A TypeDesc describes simple data types.
4+
///
5+
/// It frequently comes up (in my experience, with renderers and image
6+
/// handling programs) that you want a way to describe data that is passed
7+
/// through APIs through blind pointers. These are some simple classes
8+
/// that provide a simple type descriptor system. This is not meant to
9+
/// be comprehensive -- for example, there is no provision for structs,
10+
/// unions, typed pointers, const, or 'nested' type definitions. Just simple
11+
/// integer and floating point, *common* aggregates such as 3-points, and
12+
/// reasonably-lengthed arrays thereof.
313
#[derive(Debug, Clone, Copy)]
414
#[repr(C)]
515
pub struct TypeDesc {
16+
/// C data type at the heart of our type
617
pub basetype: u8,
18+
/// What kind of AGGREGATE is it?
719
pub aggregate: u8,
20+
/// Hint: What does the aggregate represent?
821
pub vecsemantics: u8,
9-
pub _reserved: u8,
22+
/// Reserved for future expansion
23+
_reserved: u8,
24+
/// Array length, 0 = not array, -1 = unsized
1025
pub arraylen: i32,
1126
}
1227

@@ -17,50 +32,102 @@ unsafe impl cxx::ExternType for TypeDesc {
1732

1833
#[cxx::bridge(namespace = oiio_ffi)]
1934
mod ffi {
35+
/// BaseType is a simple enum describing the base data types that
36+
/// correspond (mostly) to the C/C++ built-in types.
2037
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
2138
#[repr(u32)]
2239
pub enum BaseType {
40+
/// unknown type
2341
UNKNOWN = 0,
42+
/// void/no type
2443
NONE = 1,
44+
/// 8-bit unsigned int values ranging from 0..255,
45+
/// (C/C++ `unsigned char`).
2546
UINT8 = 2,
47+
/// 8-bit int values ranging from -128..127,
48+
/// (C/C++ `char`).
2649
INT8 = 3,
50+
/// 16-bit int values ranging from 0..65535,
51+
/// (C/C++ `unsigned short`).
2752
UINT16 = 4,
53+
/// 16-bit int values ranging from -32768..32767,
54+
/// (C/C++ `short`).
2855
INT16 = 5,
56+
/// 32-bit unsigned int values (C/C++ `unsigned int`).
2957
UINT32 = 6,
58+
/// signed 32-bit int values (C/C++ `int`).
3059
INT32 = 7,
60+
/// 64-bit unsigned int values (C/C++
61+
/// `unsigned long long` on most architectures).
3162
UINT64 = 8,
63+
/// signed 64-bit int values (C/C++ `long long`
64+
/// on most architectures).
3265
INT64 = 9,
66+
/// 16-bit IEEE floating point values (OpenEXR `half`).
3367
HALF = 10,
68+
/// 32-bit IEEE floating point values, (C/C++ `float`).
3469
FLOAT = 11,
70+
/// 64-bit IEEE floating point values, (C/C++ `double`).
3571
DOUBLE = 12,
72+
/// Character string.
3673
STRING = 13,
74+
/// A pointer value.
3775
PTR = 14,
76+
/// A uint64 that is the hash of a ustring.
3877
USTRINGHASH = 15,
3978
LASTBASE = 16,
4079
}
4180

81+
/// Aggregate describes whether our TypeDesc is a simple scalar of one
82+
/// of the BaseType's, or one of several simple aggregates.
83+
///
84+
/// Note that aggregates and arrays are different. A `TypeDesc(FLOAT,3)`
85+
/// is an array of three floats, a `TypeDesc(FLOAT,VEC3)` is a single
86+
/// 3-component vector comprised of floats, and `TypeDesc(FLOAT,3,VEC3)`
87+
/// is an array of 3 vectors, each of which is comprised of 3 floats.
4288
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
4389
#[repr(u32)]
4490
pub enum Aggregate {
91+
/// A single scalar value (such as a raw `int` or
92+
/// `float` in C). This is the default.
4593
SCALAR = 1,
94+
/// 2 values representing a 2D vector.
4695
VEC2 = 2,
96+
/// 3 values representing a 3D vector.
4797
VEC3 = 3,
98+
/// 4 values representing a 4D vector.
4899
VEC4 = 4,
100+
/// 9 values representing a 3x3 matrix.
49101
MATRIX33 = 9,
102+
/// 16 values representing a 4x4 matrix.
50103
MATRIX44 = 16,
51104
}
52105

106+
/// VecSemantics gives hints about what the data represent (for example,
107+
/// if a spatial vector quantity should transform as a point, direction
108+
/// vector, or surface normal).
53109
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
54110
#[repr(u32)]
55111
pub enum VecSemantics {
112+
/// No semantic hints.
56113
NOSEMANTICS = 0,
114+
/// Color
57115
COLOR = 1,
116+
/// Point: a spatial location
58117
POINT = 2,
118+
/// Vector: a spatial direction
59119
VECTOR = 3,
120+
/// Normal: a surface normal
60121
NORMAL = 4,
122+
/// indicates an `int[2]` representing the standard
123+
/// 4-byte encoding of an SMPTE timecode.
61124
TIMECODE = 5,
125+
/// indicates an `int[7]` representing the standard
126+
/// 28-byte encoding of an SMPTE keycode.
62127
KEYCODE = 6,
128+
/// A VEC2 representing a rational number `val[0] / val[1]`
63129
RATIONAL = 7,
130+
/// A VEC2[2] or VEC3[2] that represents a 2D or 3D bounds (min/max)
64131
BOX = 8,
65132
}
66133

@@ -73,87 +140,173 @@ mod ffi {
73140
type VecSemantics;
74141
type TypeDesc = crate::typedesc::TypeDesc;
75142

143+
/// Construct from a BaseType and optional aggregateness, semantics,
144+
/// and arrayness.
76145
fn typedesc_new(
77146
btype: BaseType,
78147
agg: Aggregate,
79148
semantics: VecSemantics,
80149
arraylen: i32,
81150
) -> TypeDesc;
82151

152+
/// Construct an array of a non-aggregate BaseType.
83153
fn typedesc_from_basetype_arraylen(btype: BaseType, arraylen: i32) -> TypeDesc;
84154

155+
/// Construct an array from BaseType, Aggregate, and array length,
156+
/// with unspecified (or moot) semantic hints.
85157
fn typedesc_from_basetype_aggregate_arraylen(
86158
btype: BaseType,
87159
agg: Aggregate,
88160
arraylen: i32,
89161
) -> TypeDesc;
90162

163+
/// Construct from a string (e.g., "float[3]"). If no valid
164+
/// type could be assembled, set base to UNKNOWN.
165+
///
166+
/// Examples:
167+
///
168+
/// ```
169+
/// # use oiio_sys::typedesc::*;
170+
/// assert!(typedesc_eq(&typedesc_from_string("int"), &typedesc_new(BaseType::INT32, Aggregate::SCALAR, VecSemantics::NOSEMANTICS, 0)));
171+
/// assert!(typedesc_eq(&typedesc_from_string("float"), &typedesc_new(BaseType::FLOAT, Aggregate::SCALAR, VecSemantics::NOSEMANTICS, 0)));
172+
/// assert!(typedesc_eq(&typedesc_from_string("uint16"), &typedesc_new(BaseType::UINT16, Aggregate::SCALAR, VecSemantics::NOSEMANTICS, 0)));
173+
/// assert!(typedesc_eq(&typedesc_from_string("point"), &typedesc_new(BaseType::FLOAT, Aggregate::VEC3, VecSemantics::POINT, 0)));
174+
/// ```
175+
///
91176
fn typedesc_from_string(typestring: &str) -> TypeDesc;
92177

178+
/// Clone constructor.
93179
fn typedesc_clone(t: &TypeDesc) -> TypeDesc;
94180

181+
/// Return the name, for printing and whatnot. For example,
182+
/// "float", "int[5]", "normal"
95183
fn typedesc_as_str(typedesc: &TypeDesc) -> &str;
96184

185+
/// Return the number of elements: 1 if not an array, or the array
186+
/// length. Invalid to call this for arrays of undetermined size.
97187
fn typedesc_numelements(typedesc: &TypeDesc) -> usize;
98188

189+
/// Return the number of basetype values: the aggregate count multiplied
190+
/// by the array length (or 1 if not an array). Invalid to call this
191+
/// for arrays of undetermined size.
99192
fn typedesc_basevalues(typedesc: &TypeDesc) -> usize;
100193

194+
/// Does this TypeDesc describe an array?
101195
fn typedesc_is_array(typedesc: &TypeDesc) -> bool;
102196

197+
/// Does this TypeDesc describe an array, but whose length is not
198+
/// specified?
103199
fn typedesc_is_unsized_array(typedesc: &TypeDesc) -> bool;
104200

201+
/// Does this TypeDesc describe an array, whose length is specified?
105202
fn typedesc_is_sized_array(typedesc: &TypeDesc) -> bool;
106203

204+
/// Return the size, in bytes, of this type.
107205
fn typedesc_size(typedesc: &TypeDesc) -> usize;
108206

207+
/// Return the type of one element, i.e., strip out the array-ness.
109208
fn typedesc_elementtype(typedesc: &TypeDesc) -> TypeDesc;
110209

210+
/// Return the size, in bytes, of one element of this type (that is,
211+
/// ignoring whether it's an array).
111212
fn typedesc_elementsize(typedesc: &TypeDesc) -> usize;
112213

214+
/// Return just the underlying C scalar type, i.e., strip out the
215+
/// array-ness and the aggregateness.
113216
fn typedesc_scalartype(typedesc: &TypeDesc) -> TypeDesc;
114217

218+
/// Return the base type size, i.e., stripped of both array-ness
219+
/// and aggregateness.
115220
fn typedesc_basesize(typedesc: &TypeDesc) -> usize;
116221

222+
/// True if it's a floating-point type (versus a fundamentally
223+
/// integral type or something else like a string).
117224
fn typedesc_is_floating_point(typedesc: &TypeDesc) -> bool;
118225

226+
/// True if it's a signed type that allows for negative values.
119227
fn typedesc_is_signed(typedesc: &TypeDesc) -> bool;
120228

229+
/// Shortcut: is it UNKNOWN?
121230
fn typedesc_is_unknown(typedesc: &TypeDesc) -> bool;
122231

232+
/// Set `typedesc` to the type described in the string. Return the
233+
/// length of the part of the string that describes the type. If
234+
/// no valid type could be assembled, return 0 and do not modify
235+
/// `typedesc`.
123236
fn typedesc_fromstring(typedesc: &mut TypeDesc, typestring: &str) -> usize;
124237

238+
/// Compare two TypeDesc values for equality.
125239
fn typedesc_eq(typedesc: &TypeDesc, t: &TypeDesc) -> bool;
126240

241+
/// Compare two TypeDesc values for inequality.
127242
fn typedesc_ne(typedesc: &TypeDesc, t: &TypeDesc) -> bool;
128243

244+
/// Compare a TypeDesc to a basetype (it's the same if it has the
245+
/// same base type and is not an aggregate or an array).
129246
fn typedesc_eq_basetype(t: &TypeDesc, b: BaseType) -> bool;
130247

248+
/// Compare a TypeDesc to a basetype (it's the same if it has the
249+
/// same base type and is not an aggregate or an array).
131250
fn basetype_eq_typedesc(b: BaseType, t: &TypeDesc) -> bool;
132251

252+
/// Compare a TypeDesc to a basetype (it's the same if it has the
253+
/// same base type and is not an aggregate or an array).
133254
fn typedesc_ne_basetype(t: &TypeDesc, b: BaseType) -> bool;
134255

256+
/// Compare a TypeDesc to a basetype (it's the same if it has the
257+
/// same base type and is not an aggregate or an array).
135258
fn basetype_ne_typedesc(b: BaseType, t: &TypeDesc) -> bool;
136259

260+
/// TypeDesc's are equivalent if they are equal, or if their only
261+
/// inequality is differing vector semantics.
137262
fn typedesc_equivalent(typedesc: &TypeDesc, b: &TypeDesc) -> bool;
138263

264+
/// Is this a 2-vector aggregate (of the given type, float by default)?
139265
fn typedesc_is_vec2(typedesc: &TypeDesc, b: BaseType) -> bool;
140266

267+
/// Is this a 3-vector aggregate (of the given type, float by default)?
141268
fn typedesc_is_vec3(typedesc: &TypeDesc, b: BaseType) -> bool;
142269

270+
/// Is this a 4-vector aggregate (of the given type, float by default)?
143271
fn typedesc_is_vec4(typedesc: &TypeDesc, b: BaseType) -> bool;
144272

273+
/// Is this an array of aggregates that represents a 2D bounding box?
145274
fn typedesc_is_box2(typedesc: &TypeDesc, b: BaseType) -> bool;
146275

276+
/// Is this an array of aggregates that represents a 3D bounding box?
147277
fn typedesc_is_box3(typedesc: &TypeDesc, b: BaseType) -> bool;
148278

279+
/// Demote the type to a non-array
149280
fn typedesc_unarray(typedesc: &mut TypeDesc) -> ();
150281

282+
/// Test for lexicographic 'less', comes in handy for lots of STL
283+
/// containers and algorithms.
151284
fn typedesc_lt(typedesc: &TypeDesc, x: &TypeDesc) -> bool;
152285

286+
/// Given base data types of a and b, return a basetype that is a best
287+
/// guess for one that can handle both without any loss of range or
288+
/// precision.
153289
fn typedesc_basetype_merge(a: TypeDesc, b: TypeDesc) -> BaseType;
154290

291+
/// Given base data types of a and b, return a basetype that is a best
292+
/// guess for one that can handle both without any loss of range or
293+
/// precision.
155294
fn typedesc_basetype_merge_3(a: TypeDesc, b: TypeDesc, c: TypeDesc) -> BaseType;
156295

296+
/// Given data pointed to by src and described by srctype, copy it to the
297+
/// memory pointed to by dst and described by dsttype, and return true if a
298+
/// conversion is possible, false if it is not. If the types are equivalent,
299+
/// this is a straightforward memory copy. If the types differ, there are
300+
/// several non-equivalent type conversions that will nonetheless succeed:
301+
/// * If dsttype is a string (and therefore dst points to a ustring or a
302+
/// `char*`): it will always succeed, producing a string akin to calling
303+
/// `typedesc_tostring()`.
304+
/// * If dsttype is int32 or uint32: other integer types will do their best
305+
/// (caveat emptor if you mix signed/unsigned). Also a source string will
306+
/// convert to int if and only if its characters form a valid integer.
307+
/// * If dsttype is float: inteegers and other float types will do
308+
/// their best conversion; strings will convert if and only if their
309+
/// characters form a valid float number.
157310
fn typedesc_convert_type(
158311
srctype: TypeDesc,
159312
src: &[u8],

0 commit comments

Comments
 (0)