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
|
use crate::{Slice, Value, Visit};
use core::fmt;
use core::num::Wrapping;
/// A type that can be converted to a [`Value`].
///
/// `Valuable` types are inspected by defining a [`Visit`] implementation and
/// using it when calling [`Valuable::visit`]. See [`Visit`] documentation for
/// more details.
///
/// The `Valuable` procedural macro makes implementing `Valuable` easy. Users
/// can add add [`#[derive(Valuable)]`][macro] to their types.
///
/// `Valuable` provides implementations for many Rust primitives and standard
/// library types.
///
/// Types implementing `Valuable` may also implement one of the more specific
/// traits: [`Structable`], [`Enumerable`], [`Listable`], and [`Mappable`]. These traits
/// should be implemented when the type is a nested container of other `Valuable` types.
///
/// [`Value`]: Value
/// [`Visit`]: Visit
/// [`Valuable::visit`]: Valuable::visit
/// [`Structable`]: crate::Structable
/// [`Enumerable`]: crate::Enumerable
/// [`Listable`]: crate::Listable
/// [`Mappable`]: crate::Mappable
/// [macro]: macro@crate::Valuable
pub trait Valuable {
/// Converts self into a [`Value`] instance.
///
/// # Examples
///
/// ```
/// use valuable::Valuable;
///
/// let _ = "hello".as_value();
/// ```
fn as_value(&self) -> Value<'_>;
/// Calls the relevant method on [`Visit`] to extract data from `self`.
///
/// This method is used to extract type-specific data from the value and is
/// intended to be an implementation detail. For example, `Vec` implements
/// `visit` by calling [`visit_value()`] on each of its elements. Structs
/// implement `visit` by calling [`visit_named_fields()`] or
/// [`visit_unnamed_fields()`].
///
/// Usually, users will call the [`visit`] function instead.
///
/// [`Visit`]: Visit
/// [`visit`]: visit()
/// [`visit_value()`]: Visit::visit_value()
/// [`visit_named_fields()`]: Visit::visit_named_fields()
/// [`visit_unnamed_fields()`]: Visit::visit_unnamed_fields()
fn visit(&self, visit: &mut dyn Visit);
/// Calls [`Visit::visit_primitive_slice()`] with `self`.
///
/// This method is an implementation detail used to optimize visiting
/// primitive slices.
///
/// [`Visit::visit_primitive_slice()`]: Visit::visit_primitive_slice
fn visit_slice(slice: &[Self], visit: &mut dyn Visit)
where
Self: Sized,
{
for item in slice {
visit.visit_value(item.as_value());
}
}
}
macro_rules! deref {
(
$(
$(#[$attrs:meta])*
$ty:ty,
)*
) => {
$(
$(#[$attrs])*
impl<T: ?Sized + Valuable> Valuable for $ty {
fn as_value(&self) -> Value<'_> {
T::as_value(&**self)
}
fn visit(&self, visit: &mut dyn Visit) {
T::visit(&**self, visit);
}
}
)*
};
}
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>,
}
macro_rules! valuable {
(
$(
$variant:ident($ty:ty),
)*
) => {
$(
impl Valuable for $ty {
fn as_value(&self) -> Value<'_> {
Value::$variant(*self)
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_value(self.as_value());
}
fn visit_slice(slice: &[Self], visit: &mut dyn Visit)
where
Self: Sized,
{
visit.visit_primitive_slice(Slice::$variant(slice));
}
}
)*
};
}
valuable! {
Bool(bool),
Char(char),
F32(f32),
F64(f64),
I8(i8),
I16(i16),
I32(i32),
I64(i64),
I128(i128),
Isize(isize),
U8(u8),
U16(u16),
U32(u32),
U64(u64),
U128(u128),
Usize(usize),
}
macro_rules! nonzero {
(
$(
$variant:ident($ty:ident),
)*
) => {
$(
impl Valuable for core::num::$ty {
fn as_value(&self) -> Value<'_> {
Value::$variant(self.get())
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_value(self.as_value());
}
}
)*
};
}
nonzero! {
I8(NonZeroI8),
I16(NonZeroI16),
I32(NonZeroI32),
I64(NonZeroI64),
I128(NonZeroI128),
Isize(NonZeroIsize),
U8(NonZeroU8),
U16(NonZeroU16),
U32(NonZeroU32),
U64(NonZeroU64),
U128(NonZeroU128),
Usize(NonZeroUsize),
}
#[cfg(not(valuable_no_atomic))]
macro_rules! atomic {
(
$(
$(#[$attrs:meta])*
$variant:ident($ty:ident),
)*
) => {
$(
$(#[$attrs])*
impl Valuable for core::sync::atomic::$ty {
fn as_value(&self) -> Value<'_> {
// Use SeqCst to match Debug and serde which use SeqCst.
// https://github.com/rust-lang/rust/blob/1.52.1/library/core/src/sync/atomic.rs#L1361-L1366
// https://github.com/serde-rs/serde/issues/1496
Value::$variant(self.load(core::sync::atomic::Ordering::SeqCst))
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_value(self.as_value());
}
}
)*
};
}
#[cfg(not(valuable_no_atomic))]
atomic! {
Bool(AtomicBool),
I8(AtomicI8),
I16(AtomicI16),
I32(AtomicI32),
#[cfg(not(valuable_no_atomic_64))]
I64(AtomicI64),
Isize(AtomicIsize),
U8(AtomicU8),
U16(AtomicU16),
U32(AtomicU32),
#[cfg(not(valuable_no_atomic_64))]
U64(AtomicU64),
Usize(AtomicUsize),
}
impl<T: Valuable> Valuable for Wrapping<T> {
fn as_value(&self) -> Value<'_> {
self.0.as_value()
}
fn visit(&self, visit: &mut dyn Visit) {
self.0.visit(visit);
}
}
impl Valuable for () {
fn as_value(&self) -> Value<'_> {
Value::Tuplable(self)
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_unnamed_fields(&[]);
}
}
impl<T: Valuable> Valuable for Option<T> {
fn as_value(&self) -> Value<'_> {
match self {
Some(v) => v.as_value(),
None => Value::Unit,
}
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_value(self.as_value());
}
}
impl Valuable for &'_ str {
fn as_value(&self) -> Value<'_> {
Value::String(self)
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_value(Value::String(self));
}
fn visit_slice(slice: &[Self], visit: &mut dyn Visit)
where
Self: Sized,
{
visit.visit_primitive_slice(Slice::Str(slice));
}
}
#[cfg(feature = "alloc")]
impl Valuable for alloc::string::String {
fn as_value(&self) -> Value<'_> {
Value::String(&self[..])
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_value(Value::String(self));
}
fn visit_slice(slice: &[Self], visit: &mut dyn Visit)
where
Self: Sized,
{
visit.visit_primitive_slice(Slice::String(slice));
}
}
#[cfg(feature = "std")]
impl Valuable for &std::path::Path {
fn as_value(&self) -> Value<'_> {
Value::Path(self)
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_value(Value::Path(self));
}
}
#[cfg(feature = "std")]
impl Valuable for std::path::PathBuf {
fn as_value(&self) -> Value<'_> {
Value::Path(self)
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_value(Value::Path(self));
}
}
#[cfg(feature = "std")]
impl Valuable for dyn std::error::Error + 'static {
fn as_value(&self) -> Value<'_> {
Value::Error(self)
}
fn visit(&self, visit: &mut dyn Visit) {
visit.visit_value(self.as_value());
}
}
impl fmt::Debug for dyn Valuable + '_ {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let value = self.as_value();
value.fmt(fmt)
}
}
|