summaryrefslogtreecommitdiffstats
path: root/vendor/valuable/src/tuplable.rs
blob: 8c7169ff255477e36efb2c91cdc76346998d0940 (plain)
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::{Valuable, Value, Visit};

use core::fmt;

/// A tuple-like [`Valuable`] sub-type.
///
/// Implemented by [`Valuable`] types that have a tuple-like shape. Fields are
/// always unnamed. Values that implement `Tuplable` must return
/// [`Value::Tuplable`] from their [`Valuable::as_value`] implementation.
///
/// It is uncommon for users to implement this type as the crate provides
/// implementations of `Tuplable` for Rust tuples.
///
/// # Inspecting
///
/// Inspecting fields contained by a `Tuplable` instance is done by visiting the
/// tuple. When visiting a `Tuple`, the `visit_unnamed_fields()` method is
/// called. When the tuple is statically defined, `visit_unnamed_fields()` is
/// called once with the values of all the fields. A dynamic tuple
/// implementation may call `visit_unnamed_fields()` multiple times.
pub trait Tuplable: Valuable {
    /// Returns the tuple's definition.
    ///
    /// See [`TupleDef`] documentation for more details.
    ///
    /// # Examples
    ///
    /// ```
    /// use valuable::{Tuplable, TupleDef};
    ///
    /// let tuple = (123, "hello");
    ///
    /// if let TupleDef::Static { fields, .. } = tuple.definition() {
    ///     assert_eq!(2, fields);
    /// }
    /// ```
    fn definition(&self) -> TupleDef;
}

/// The number of fields and other tuple-level information.
///
/// Returned by [`Tuplable::definition()`], `TupleDef` provides the caller with
/// information about the tuple's definition.
///
/// This includes the number of fields contained by the tuple.
#[derive(Debug)]
#[non_exhaustive]
pub enum TupleDef {
    /// The tuple is statically-defined, all fields are known ahead of time.
    ///
    /// Static tuple implementations are provided by the crate.
    ///
    /// # Examples
    ///
    /// A statically defined tuple.
    ///
    /// ```
    /// use valuable::{Tuplable, TupleDef};
    ///
    /// let tuple = (123, "hello");
    ///
    /// match tuple.definition() {
    ///     TupleDef::Static { fields, .. } => {
    ///         assert_eq!(2, fields);
    ///     }
    ///     _ => unreachable!(),
    /// };
    /// ```
    #[non_exhaustive]
    Static {
        /// The number of fields contained by the tuple.
        fields: usize,
    },
    /// The tuple is dynamically-defined, not all fields are known ahead of
    /// time.
    ///
    /// # Examples
    ///
    /// ```
    /// use valuable::{Tuplable, TupleDef, Valuable, Value, Visit};
    ///
    /// struct MyTuple;
    ///
    /// impl Valuable for MyTuple {
    ///     fn as_value(&self) -> Value<'_> {
    ///         Value::Tuplable(self)
    ///     }
    ///
    ///     fn visit(&self, visit: &mut dyn Visit) {
    ///         visit.visit_unnamed_fields(&[Value::I32(123)]);
    ///         visit.visit_unnamed_fields(&[Value::String("hello world")]);
    ///     }
    /// }
    ///
    /// impl Tuplable for MyTuple {
    ///     fn definition(&self) -> TupleDef {
    ///         TupleDef::new_dynamic((1, Some(3)))
    ///     }
    /// }
    /// ```
    #[non_exhaustive]
    Dynamic {
        /// Returns the bounds on the number of tuple fields.
        ///
        /// Specifically, the first element is the lower bound, and the second
        /// element is the upper bound.
        fields: (usize, Option<usize>),
    },
}

macro_rules! deref {
    (
        $(
            $(#[$attrs:meta])*
            $ty:ty,
        )*
    ) => {
        $(
            $(#[$attrs])*
            impl<T: ?Sized + Tuplable> Tuplable for $ty {
                fn definition(&self) -> TupleDef {
                    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>,
}

impl Tuplable for () {
    fn definition(&self) -> TupleDef {
        TupleDef::Static { fields: 0 }
    }
}

macro_rules! tuple_impls {
    (
        $( $len:expr => ( $($n:tt $name:ident)+ ) )+
    ) => {
        $(
            impl<$($name),+> Valuable for ($($name,)+)
            where
                $($name: Valuable,)+
            {
                fn as_value(&self) -> Value<'_> {
                    Value::Tuplable(self)
                }

                fn visit(&self, visit: &mut dyn Visit) {
                    visit.visit_unnamed_fields(&[
                        $(
                            self.$n.as_value(),
                        )+
                    ]);
                }
            }

            impl<$($name),+> Tuplable for ($($name,)+)
            where
                $($name: Valuable,)+
            {
                fn definition(&self) -> TupleDef {
                    TupleDef::Static { fields: $len }
                }
            }
        )+
    }
}

tuple_impls! {
    1 => (0 T0)
    2 => (0 T0 1 T1)
    3 => (0 T0 1 T1 2 T2)
    4 => (0 T0 1 T1 2 T2 3 T3)
    5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
    6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
    7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
    8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
    9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
    10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
    11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
    12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
    13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
    14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
    15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
    16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
}

impl fmt::Debug for dyn Tuplable + '_ {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.definition().is_unit() {
            ().fmt(fmt)
        } else {
            struct DebugTuple<'a, 'b> {
                fmt: fmt::DebugTuple<'a, 'b>,
            }

            impl Visit for DebugTuple<'_, '_> {
                fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
                    for value in values {
                        self.fmt.field(value);
                    }
                }

                fn visit_value(&mut self, _: Value<'_>) {
                    unimplemented!()
                }
            }

            let mut debug = DebugTuple {
                fmt: fmt.debug_tuple(""),
            };

            self.visit(&mut debug);
            debug.fmt.finish()
        }
    }
}

impl TupleDef {
    /// Create a new [`TupleDef::Static`] instance
    ///
    /// This should be used when the tuple's fields are fixed and known ahead of time.
    ///
    /// # Examples
    ///
    /// ```
    /// use valuable::TupleDef;
    ///
    /// let def = TupleDef::new_static(2);
    /// ```
    pub const fn new_static(fields: usize) -> TupleDef {
        TupleDef::Static { fields }
    }

    /// Create a new [`TupleDef::Dynamic`] instance.
    ///
    /// This is used when the tuple's fields may vary at runtime.
    ///
    /// # Examples
    ///
    /// ```
    /// use valuable::TupleDef;
    ///
    /// let def = TupleDef::new_dynamic((2, Some(10)));
    /// ```
    pub const fn new_dynamic(fields: (usize, Option<usize>)) -> TupleDef {
        TupleDef::Dynamic { fields }
    }

    /// Returns `true` if `self` represents the [unit][primitive@unit] tuple.
    ///
    /// # Examples
    ///
    /// With the unit tuple
    ///
    /// ```
    /// use valuable::Tuplable;
    ///
    /// let tuple: &dyn Tuplable = &();
    /// assert!(tuple.definition().is_unit());
    /// ```
    ///
    /// When not the unit tuple.
    ///
    /// ```
    /// use valuable::Tuplable;
    ///
    /// let tuple: &dyn Tuplable = &(123,456);
    /// assert!(!tuple.definition().is_unit());
    /// ```
    pub fn is_unit(&self) -> bool {
        match *self {
            TupleDef::Static { fields } => fields == 0,
            TupleDef::Dynamic { fields } => fields == (0, Some(0)),
        }
    }

    /// Returns `true` if the tuple is [statically defined](TupleDef::Static).
    ///
    /// # Examples
    ///
    /// With a static tuple
    ///
    /// ```
    /// use valuable::TupleDef;
    ///
    /// let def = TupleDef::new_static(2);
    /// assert!(def.is_static());
    /// ```
    ///
    /// With a dynamic tuple
    ///
    /// ```
    /// use valuable::TupleDef;
    ///
    /// let def = TupleDef::new_dynamic((2, None));
    /// assert!(!def.is_static());
    /// ```
    pub fn is_static(&self) -> bool {
        matches!(self, TupleDef::Static { .. })
    }

    /// Returns `true` if the tuple is [dynamically defined](TupleDef::Dynamic).
    ///
    /// # Examples
    ///
    /// With a static tuple
    ///
    /// ```
    /// use valuable::TupleDef;
    ///
    /// let def = TupleDef::new_static(2);
    /// assert!(!def.is_dynamic());
    /// ```
    ///
    /// With a dynamic tuple
    ///
    /// ```
    /// use valuable::TupleDef;
    ///
    /// let def = TupleDef::new_dynamic((2, None));
    /// assert!(def.is_dynamic());
    /// ```
    pub fn is_dynamic(&self) -> bool {
        matches!(self, TupleDef::Dynamic { .. })
    }
}