sval_dynamic/
value.rs

1use crate::stream::Stream;
2
3mod private {
4    use crate::stream::Stream;
5
6    pub trait DispatchValue {
7        fn dispatch_stream<'sval>(&'sval self, stream: &mut dyn Stream<'sval>) -> sval::Result;
8        fn dispatch_tag(&self) -> Option<sval::Tag>;
9        fn dispatch_to_bool(&self) -> Option<bool>;
10        fn dispatch_to_f32(&self) -> Option<f32>;
11        fn dispatch_to_f64(&self) -> Option<f64>;
12        fn dispatch_to_i8(&self) -> Option<i8>;
13        fn dispatch_to_i16(&self) -> Option<i16>;
14        fn dispatch_to_i32(&self) -> Option<i32>;
15        fn dispatch_to_i64(&self) -> Option<i64>;
16        fn dispatch_to_i128(&self) -> Option<i128>;
17        fn dispatch_to_u8(&self) -> Option<u8>;
18        fn dispatch_to_u16(&self) -> Option<u16>;
19        fn dispatch_to_u32(&self) -> Option<u32>;
20        fn dispatch_to_u64(&self) -> Option<u64>;
21        fn dispatch_to_u128(&self) -> Option<u128>;
22        fn dispatch_to_text(&self) -> Option<&str>;
23        fn dispatch_to_binary(&self) -> Option<&[u8]>;
24    }
25
26    pub trait EraseValue {
27        fn erase_value(&self) -> crate::private::Erased<&dyn DispatchValue>;
28    }
29}
30
31/**
32An object-safe version of [`sval::Value`].
33*/
34pub trait Value: private::EraseValue {}
35
36impl<T: sval::Value> Value for T {}
37
38impl<T: sval::Value> private::EraseValue for T {
39    fn erase_value(&self) -> crate::private::Erased<&dyn private::DispatchValue> {
40        crate::private::Erased(self)
41    }
42}
43
44impl<T: sval::Value> private::DispatchValue for T {
45    fn dispatch_stream<'sval>(&'sval self, stream: &mut dyn Stream<'sval>) -> sval::Result {
46        self.stream(stream)
47    }
48
49    fn dispatch_tag(&self) -> Option<sval::Tag> {
50        self.tag()
51    }
52
53    fn dispatch_to_bool(&self) -> Option<bool> {
54        self.to_bool()
55    }
56
57    fn dispatch_to_f32(&self) -> Option<f32> {
58        self.to_f32()
59    }
60
61    fn dispatch_to_f64(&self) -> Option<f64> {
62        self.to_f64()
63    }
64
65    fn dispatch_to_i8(&self) -> Option<i8> {
66        self.to_i8()
67    }
68
69    fn dispatch_to_i16(&self) -> Option<i16> {
70        self.to_i16()
71    }
72
73    fn dispatch_to_i32(&self) -> Option<i32> {
74        self.to_i32()
75    }
76
77    fn dispatch_to_i64(&self) -> Option<i64> {
78        self.to_i64()
79    }
80
81    fn dispatch_to_i128(&self) -> Option<i128> {
82        self.to_i128()
83    }
84
85    fn dispatch_to_u8(&self) -> Option<u8> {
86        self.to_u8()
87    }
88
89    fn dispatch_to_u16(&self) -> Option<u16> {
90        self.to_u16()
91    }
92
93    fn dispatch_to_u32(&self) -> Option<u32> {
94        self.to_u32()
95    }
96
97    fn dispatch_to_u64(&self) -> Option<u64> {
98        self.to_u64()
99    }
100
101    fn dispatch_to_u128(&self) -> Option<u128> {
102        self.to_u128()
103    }
104
105    fn dispatch_to_text(&self) -> Option<&str> {
106        self.to_text()
107    }
108
109    fn dispatch_to_binary(&self) -> Option<&[u8]> {
110        self.to_binary()
111    }
112}
113
114macro_rules! impl_value {
115    ($($impl:tt)*) => {
116        $($impl)* {
117            fn stream<'sval, S: sval::Stream<'sval> + ?Sized>(&'sval self, mut stream: &mut S) -> sval::Result {
118                self.erase_value().0.dispatch_stream(&mut stream)
119            }
120
121            fn tag(&self) -> Option<sval::Tag> {
122                self.erase_value().0.dispatch_tag()
123            }
124
125            fn to_bool(&self) -> Option<bool> {
126                self.erase_value().0.dispatch_to_bool()
127            }
128
129            fn to_f32(&self) -> Option<f32> {
130                self.erase_value().0.dispatch_to_f32()
131            }
132
133            fn to_f64(&self) -> Option<f64> {
134                self.erase_value().0.dispatch_to_f64()
135            }
136
137            fn to_i8(&self) -> Option<i8> {
138                self.erase_value().0.dispatch_to_i8()
139            }
140
141            fn to_i16(&self) -> Option<i16> {
142                self.erase_value().0.dispatch_to_i16()
143            }
144
145            fn to_i32(&self) -> Option<i32> {
146                self.erase_value().0.dispatch_to_i32()
147            }
148
149            fn to_i64(&self) -> Option<i64> {
150                self.erase_value().0.dispatch_to_i64()
151            }
152
153            fn to_i128(&self) -> Option<i128> {
154                self.erase_value().0.dispatch_to_i128()
155            }
156
157            fn to_u8(&self) -> Option<u8> {
158                self.erase_value().0.dispatch_to_u8()
159            }
160
161            fn to_u16(&self) -> Option<u16> {
162                self.erase_value().0.dispatch_to_u16()
163            }
164
165            fn to_u32(&self) -> Option<u32> {
166                self.erase_value().0.dispatch_to_u32()
167            }
168
169            fn to_u64(&self) -> Option<u64> {
170                self.erase_value().0.dispatch_to_u64()
171            }
172
173            fn to_u128(&self) -> Option<u128> {
174                self.erase_value().0.dispatch_to_u128()
175            }
176
177            fn to_text(&self) -> Option<&str> {
178                self.erase_value().0.dispatch_to_text()
179            }
180
181            fn to_binary(&self) -> Option<&[u8]> {
182                self.erase_value().0.dispatch_to_binary()
183            }
184        }
185    }
186}
187
188impl_value!(impl<'d> sval::Value for dyn Value + 'd);
189impl_value!(impl<'d> sval::Value for dyn Value + Send + 'd);
190impl_value!(impl<'d> sval::Value for dyn Value + Send + Sync + 'd);