1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
use crate::field::*;
use crate::*;
use core::fmt;
/// A struct-like [`Valuable`] sub-type.
///
/// Implemented by [`Valuable`] types that have a struct-like shape. Fields may
/// be named or unnamed (tuple). Values that implement `Structable` must return
/// [`Value::Structable`] from their [`Valuable::as_value`] implementation.
///
/// # Inspecting
///
/// Inspecting fields contained by a `Structable` instance is done by visiting
/// the struct. When visiting a `Structable`, either the `visit_named_fields()`
/// or the `visit_unnamed_fields()` methods of `Visit` are called. Each method
/// may be called multiple times per `Structable`, but the two methods are never
/// mixed.
///
/// ```
/// use valuable::{NamedValues, Valuable, Value, Visit};
///
/// #[derive(Valuable)]
/// struct MyStruct {
/// foo: u32,
/// bar: u32,
/// }
///
/// struct PrintFields;
///
/// impl Visit for PrintFields {
/// fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
/// for (field, value) in named_values.iter() {
/// println!("{}: {:?}", field.name(), value);
/// }
/// }
///
/// fn visit_value(&mut self, value: Value<'_>) {
/// match value {
/// Value::Structable(v) => v.visit(self),
/// _ => {} // do nothing for other types
/// }
/// }
/// }
///
/// let my_struct = MyStruct {
/// foo: 123,
/// bar: 456,
/// };
///
/// valuable::visit(&my_struct, &mut PrintFields);
/// ```
///
/// If the struct is **statically** defined, then all fields are known ahead of
/// time and may be accessed via the [`StructDef`] instance returned by
/// [`definition()`]. [`NamedField`] instances returned by [`definition()`]
/// maybe used to efficiently extract specific field values.
///
/// # Implementing
///
/// Implementing `Structable` is usually done by adding `#[derive(Valuable)]` to
/// a Rust `struct` definition.
///
/// ```
/// use valuable::{Fields, Valuable, Structable, StructDef};
///
/// #[derive(Valuable)]
/// struct MyStruct {
/// foo: &'static str,
/// }
///
/// let my_struct = MyStruct { foo: "Hello" };
/// let fields = match my_struct.definition() {
/// StructDef::Static { name, fields, .. } => {
/// assert_eq!("MyStruct", name);
/// fields
/// }
/// _ => unreachable!(),
/// };
///
/// match fields {
/// Fields::Named(named_fields) => {
/// assert_eq!(1, named_fields.len());
/// assert_eq!("foo", named_fields[0].name());
/// }
/// _ => unreachable!(),
/// }
/// ```
///
/// [`definition()`]: Structable::definition()
pub trait Structable: Valuable {
/// Returns the struct's definition.
///
/// See [`StructDef`] documentation for more details.
///
/// # Examples
///
/// ```
/// use valuable::{Structable, Valuable};
///
/// #[derive(Valuable)]
/// struct MyStruct {
/// foo: u32,
/// }
///
/// let my_struct = MyStruct {
/// foo: 123,
/// };
///
/// assert_eq!("MyStruct", my_struct.definition().name());
fn definition(&self) -> StructDef<'_>;
}
/// A struct's name, fields, and other struct-level information.
///
/// Returned by [`Structable::definition()`], `StructDef` provides the caller
/// with information about the struct's definition.
///
/// [`Structable::definition()`]: Structable::definition
#[derive(Debug)]
#[non_exhaustive]
pub enum StructDef<'a> {
/// The struct is statically-defined, all fields are known ahead of time.
///
/// Most `Structable` definitions for Rust struct types will be
/// `StructDef::Static`.
///
/// # Examples
///
/// A statically defined struct
///
/// ```
/// use valuable::{Fields, Valuable, Structable, StructDef};
///
/// #[derive(Valuable)]
/// struct MyStruct {
/// foo: &'static str,
/// }
///
/// let my_struct = MyStruct { foo: "Hello" };
/// let fields = match my_struct.definition() {
/// StructDef::Static { name, fields, ..} => {
/// assert_eq!("MyStruct", name);
/// fields
/// }
/// _ => unreachable!(),
/// };
///
/// match fields {
/// Fields::Named(named_fields) => {
/// assert_eq!(1, named_fields.len());
/// assert_eq!("foo", named_fields[0].name());
/// }
/// _ => unreachable!(),
/// }
/// ```
#[non_exhaustive]
Static {
/// The struct's name.
name: &'static str,
/// The struct's fields.
fields: Fields<'static>,
},
/// The struct is dynamically-defined, not all fields are known ahead of
/// time.
///
/// A dynamically-defined struct **could** be represented using
/// [`Mappable`], though, using `Structable` offers benefits in a couple of
/// cases. For example, when serializing a `Value`, some formats will
/// serialize maps and structs differently. In this case, differentiating
/// the two is required. There also are times when **some** struct fields
/// are known statically, but not all of them (see second example).
///
/// # Examples
///
/// The struct stores field values in a `HashMap`.
///
/// ```
/// use valuable::{Fields, NamedField, NamedValues, Structable, StructDef, Value, Valuable, Visit};
/// use std::collections::HashMap;
///
/// /// A dynamic struct
/// struct Dyn {
/// // The struct name
/// name: String,
///
/// // Named values.
/// values: HashMap<String, Box<dyn Valuable>>,
/// }
///
/// impl Valuable for Dyn {
/// fn as_value(&self) -> Value<'_> {
/// Value::Structable(self)
/// }
///
/// fn visit(&self, visit: &mut dyn Visit) {
/// // This could be optimized to batch some.
/// for (field, value) in self.values.iter() {
/// visit.visit_named_fields(&NamedValues::new(
/// &[NamedField::new(field)],
/// &[value.as_value()],
/// ));
/// }
/// }
/// }
///
/// impl Structable for Dyn {
/// fn definition(&self) -> StructDef<'_> {
/// StructDef::new_dynamic(&self.name, Fields::Named(&[]))
/// }
/// }
/// ```
///
/// Some fields are known statically.
///
/// ```
/// use valuable::{Fields, NamedField, NamedValues, Structable, StructDef, Value, Valuable, Visit};
/// use std::collections::HashMap;
///
/// struct HalfStatic {
/// foo: u32,
/// bar: u32,
/// extra_values: HashMap<String, Box<dyn Valuable>>,
/// }
///
/// impl Valuable for HalfStatic {
/// fn as_value(&self) -> Value<'_> {
/// Value::Structable(self)
/// }
///
/// fn visit(&self, visit: &mut dyn Visit) {
/// // First, visit static fields
/// visit.visit_named_fields(&NamedValues::new(
/// FIELDS,
/// &[self.foo.as_value(), self.bar.as_value()],
/// ));
///
/// // This could be optimized to batch some.
/// for (field, value) in self.extra_values.iter() {
/// visit.visit_named_fields(&NamedValues::new(
/// &[NamedField::new(field)],
/// &[value.as_value()],
/// ));
/// }
/// }
/// }
///
/// static FIELDS: &[NamedField<'static>] = &[
/// NamedField::new("foo"),
/// NamedField::new("bar"),
/// ];
///
/// impl Structable for HalfStatic {
/// fn definition(&self) -> StructDef<'_> {
/// // Include known fields.
/// StructDef::new_dynamic(
/// "HalfStatic",
/// Fields::Named(FIELDS))
/// }
/// }
/// ```
#[non_exhaustive]
Dynamic {
/// The struct's name
name: &'a str,
/// The struct's fields.
fields: Fields<'a>,
},
}
impl fmt::Debug for dyn Structable + '_ {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let def = self.definition();
if def.fields().is_named() {
struct DebugStruct<'a, 'b> {
fmt: fmt::DebugStruct<'a, 'b>,
}
let mut debug = DebugStruct {
fmt: fmt.debug_struct(def.name()),
};
impl Visit for DebugStruct<'_, '_> {
fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
for (field, value) in named_values {
self.fmt.field(field.name(), value);
}
}
fn visit_value(&mut self, _: Value<'_>) {
unreachable!()
}
}
self.visit(&mut debug);
debug.fmt.finish()
} else {
struct DebugStruct<'a, 'b> {
fmt: fmt::DebugTuple<'a, 'b>,
}
let mut debug = DebugStruct {
fmt: fmt.debug_tuple(def.name()),
};
impl Visit for DebugStruct<'_, '_> {
fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
for value in values {
self.fmt.field(value);
}
}
fn visit_value(&mut self, _: Value<'_>) {
unreachable!();
}
}
self.visit(&mut debug);
debug.fmt.finish()
}
}
}
impl<'a> StructDef<'a> {
/// Create a new [`StructDef::Static`] instance.
///
/// This should be used when a struct's fields are fixed and known ahead of time.
///
/// # Examples
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_static("Foo", Fields::Unnamed(2));
/// ```
pub const fn new_static(name: &'static str, fields: Fields<'static>) -> StructDef<'a> {
StructDef::Static { name, fields }
}
/// Create a new [`StructDef::Dynamic`] instance.
///
/// This is used when the struct's fields may vary at runtime.
///
/// # Examples
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_dynamic("Foo", Fields::Unnamed(3));
/// ```
pub const fn new_dynamic(name: &'a str, fields: Fields<'a>) -> StructDef<'a> {
StructDef::Dynamic { name, fields }
}
/// Returns the struct's name
///
/// # Examples
///
/// With a static struct
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_static("Foo", Fields::Unnamed(1));
/// assert_eq!("Foo", def.name());
/// ```
///
/// With a dynamic struct
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_dynamic("Foo", Fields::Unnamed(2));
/// assert_eq!("Foo", def.name());
/// ```
pub const fn name(&self) -> &'a str {
match self {
StructDef::Static { name, .. } => name,
StructDef::Dynamic { name, .. } => name,
}
}
/// Returns the struct's fields
///
/// # Examples
///
/// With a static struct
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_static("Foo", Fields::Unnamed(3));
/// assert!(matches!(def.fields(), Fields::Unnamed(_)));
/// ```
///
/// With a dynamic struct
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_dynamic("Foo", Fields::Unnamed(1));
/// assert!(matches!(def.fields(), Fields::Unnamed(_)));
/// ```
pub const fn fields(&self) -> &Fields<'a> {
match self {
StructDef::Static { fields, .. } => fields,
StructDef::Dynamic { fields, .. } => fields,
}
}
/// Returns `true` if the struct is [statically defined](StructDef::Static).
///
/// # Examples
///
/// With a static struct
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_static("Foo", Fields::Unnamed(2));
/// assert!(def.is_static());
/// ```
///
/// With a dynamic struct
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_dynamic("Foo", Fields::Unnamed(4));
/// assert!(!def.is_static());
/// ```
pub const fn is_static(&self) -> bool {
matches!(self, StructDef::Static { .. })
}
/// Returns `true` if the struct is [dynamically defined](StructDef::Dynamic).
///
/// # Examples
///
/// With a static struct
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_static("Foo", Fields::Unnamed(1));
/// assert!(!def.is_dynamic());
/// ```
///
/// With a dynamic struct
///
/// ```
/// use valuable::{StructDef, Fields};
///
/// let def = StructDef::new_dynamic("Foo", Fields::Unnamed(1));
/// assert!(def.is_dynamic());
/// ```
pub const fn is_dynamic(&self) -> bool {
matches!(self, StructDef::Dynamic { .. })
}
}
macro_rules! deref {
(
$(
$(#[$attrs:meta])*
$ty:ty,
)*
) => {
$(
$(#[$attrs])*
impl<T: ?Sized + Structable> Structable for $ty {
fn definition(&self) -> StructDef<'_> {
T::definition(&**self)
}
}
)*
};
}
deref! {
&T,
&mut T,
#[cfg(feature = "alloc")]
alloc::boxed::Box<T>,
#[cfg(feature = "alloc")]
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
#[cfg(feature = "alloc")]
alloc::sync::Arc<T>,
}
|