toasty_core/stmt/
value_record.rs

1use super::Value;
2
3use std::{hash::Hash, ops};
4
5#[derive(Debug, Default, Clone, Eq)]
6pub struct ValueRecord {
7    pub fields: Vec<Value>,
8}
9
10impl ValueRecord {
11    pub fn from_vec(fields: Vec<Value>) -> Self {
12        Self { fields }
13    }
14
15    pub fn as_slice(&self) -> &[Value] {
16        &self[..]
17    }
18}
19
20impl ops::Deref for ValueRecord {
21    type Target = [Value];
22
23    fn deref(&self) -> &Self::Target {
24        &self.fields[..]
25    }
26}
27
28impl ops::DerefMut for ValueRecord {
29    fn deref_mut(&mut self) -> &mut Self::Target {
30        &mut self.fields[..]
31    }
32}
33
34impl IntoIterator for ValueRecord {
35    type Item = Value;
36    type IntoIter = std::vec::IntoIter<Value>;
37
38    fn into_iter(self) -> Self::IntoIter {
39        self.fields.into_iter()
40    }
41}
42
43impl<'a> IntoIterator for &'a ValueRecord {
44    type Item = &'a Value;
45    type IntoIter = std::slice::Iter<'a, Value>;
46
47    fn into_iter(self) -> Self::IntoIter {
48        self.iter()
49    }
50}
51
52impl<'a> IntoIterator for &'a mut ValueRecord {
53    type Item = &'a mut Value;
54    type IntoIter = std::slice::IterMut<'a, Value>;
55
56    fn into_iter(self) -> Self::IntoIter {
57        self.iter_mut()
58    }
59}
60
61// This implementation delegates the PartialEq implementation to the [Value]
62// (slice) implementation of PartialEq
63impl PartialEq for ValueRecord {
64    fn eq(&self, other: &Self) -> bool {
65        **self == **other
66    }
67}
68
69// had to impl hash for value record because conflicting implementations of hash trait
70impl Hash for ValueRecord {
71    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
72        (**self).hash(state);
73    }
74}
75
76macro_rules! impl_value_eq_tuple {
77    ( $len:literal; $(($T:ident, $idx:tt)),+ ) => {
78        impl<$($T),+> PartialEq<($($T,)+)> for Value
79        where
80            $(Value: PartialEq<$T>,)+
81        {
82            fn eq(&self, other: &($($T,)+)) -> bool {
83                match self {
84                    Value::Record(v) => {
85                        v.fields.len() == $len
86                            $(&& v.fields[$idx].eq(&other.$idx))+
87                    }
88                    _ => false,
89                }
90            }
91        }
92
93        impl<$($T),+> PartialEq<Value> for ($($T,)+)
94        where
95            $($T: PartialEq<Value>,)+
96        {
97            fn eq(&self, other: &Value) -> bool {
98                match other {
99                    Value::Record(v) => {
100                        v.fields.len() == $len
101                            $(&& self.$idx.eq(&v.fields[$idx]))+
102                    }
103                    _ => false,
104                }
105            }
106        }
107    };
108}
109
110impl_value_eq_tuple!(1; (T0, 0));
111impl_value_eq_tuple!(2; (T0, 0), (T1, 1));
112impl_value_eq_tuple!(3; (T0, 0), (T1, 1), (T2, 2));
113impl_value_eq_tuple!(4; (T0, 0), (T1, 1), (T2, 2), (T3, 3));
114impl_value_eq_tuple!(5; (T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4));
115impl_value_eq_tuple!(6; (T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4), (T5, 5));
116impl_value_eq_tuple!(7; (T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4), (T5, 5), (T6, 6));
117impl_value_eq_tuple!(8; (T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4), (T5, 5), (T6, 6), (T7, 7));
118impl_value_eq_tuple!(9; (T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4), (T5, 5), (T6, 6), (T7, 7), (T8, 8));
119impl_value_eq_tuple!(10; (T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4), (T5, 5), (T6, 6), (T7, 7), (T8, 8), (T9, 9));
120impl_value_eq_tuple!(11; (T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4), (T5, 5), (T6, 6), (T7, 7), (T8, 8), (T9, 9), (T10, 10));
121impl_value_eq_tuple!(12; (T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4), (T5, 5), (T6, 6), (T7, 7), (T8, 8), (T9, 9), (T10, 10), (T11, 11));