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
//! Support for peeking into DynamicValue types like `facet_value::Value`
use facet_core::{DynDateTimeKind, DynValueKind, DynamicValueDef};
use super::Peek;
/// Lets you read from a dynamic value (implements read-only operations for DynamicValue types)
///
/// This is used for types like `facet_value::Value` that can hold any of:
/// null, bool, number, string, bytes, array, or object - determined at runtime.
#[derive(Clone, Copy)]
pub struct PeekDynamicValue<'mem, 'facet> {
/// the underlying peek value
pub(crate) value: Peek<'mem, 'facet>,
/// the definition of the dynamic value
pub(crate) def: DynamicValueDef,
}
impl<'mem, 'facet> PeekDynamicValue<'mem, 'facet> {
/// Returns the dynamic value definition
#[inline(always)]
pub const fn def(&self) -> DynamicValueDef {
self.def
}
/// Returns the underlying peek value
#[inline(always)]
pub const fn peek(&self) -> Peek<'mem, 'facet> {
self.value
}
/// Returns the kind of value stored
#[inline]
pub fn kind(&self) -> DynValueKind {
unsafe { (self.def.vtable.get_kind)(self.value.data()) }
}
/// Returns true if the value is null
#[inline]
pub fn is_null(&self) -> bool {
self.kind() == DynValueKind::Null
}
/// Returns the boolean value if this is a bool, None otherwise
#[inline]
pub fn as_bool(&self) -> Option<bool> {
unsafe { (self.def.vtable.get_bool)(self.value.data()) }
}
/// Returns the i64 value if representable, None otherwise
#[inline]
pub fn as_i64(&self) -> Option<i64> {
unsafe { (self.def.vtable.get_i64)(self.value.data()) }
}
/// Returns the u64 value if representable, None otherwise
#[inline]
pub fn as_u64(&self) -> Option<u64> {
unsafe { (self.def.vtable.get_u64)(self.value.data()) }
}
/// Returns the f64 value if this is a number, None otherwise
#[inline]
pub fn as_f64(&self) -> Option<f64> {
unsafe { (self.def.vtable.get_f64)(self.value.data()) }
}
/// Returns the string value if this is a string, None otherwise
#[inline]
pub fn as_str(&self) -> Option<&'mem str> {
unsafe { (self.def.vtable.get_str)(self.value.data()) }
}
/// Returns the bytes value if this is bytes, None otherwise
#[inline]
pub fn as_bytes(&self) -> Option<&'mem [u8]> {
self.def
.vtable
.get_bytes
.and_then(|f| unsafe { f(self.value.data()) })
}
/// Returns the datetime components if this is a datetime, None otherwise
///
/// Returns `(year, month, day, hour, minute, second, nanos, kind)`.
#[inline]
#[allow(clippy::type_complexity)]
pub fn as_datetime(&self) -> Option<(i32, u8, u8, u8, u8, u8, u32, DynDateTimeKind)> {
self.def
.vtable
.get_datetime
.and_then(|f| unsafe { f(self.value.data()) })
}
/// Returns the length of the array if this is an array, None otherwise
#[inline]
pub fn array_len(&self) -> Option<usize> {
unsafe { (self.def.vtable.array_len)(self.value.data()) }
}
/// Returns an element from the array by index, None if not an array or index out of bounds
#[inline]
pub fn array_get(&self, index: usize) -> Option<Peek<'mem, 'facet>> {
let ptr = unsafe { (self.def.vtable.array_get)(self.value.data(), index)? };
// The element is also a DynamicValue with the same shape
Some(unsafe { Peek::unchecked_new(ptr, self.value.shape()) })
}
/// Returns the length of the object if this is an object, None otherwise
#[inline]
pub fn object_len(&self) -> Option<usize> {
unsafe { (self.def.vtable.object_len)(self.value.data()) }
}
/// Returns a key-value pair from the object by index, None if not an object or index out of bounds
#[inline]
pub fn object_get_entry(&self, index: usize) -> Option<(&'mem str, Peek<'mem, 'facet>)> {
let (key, value_ptr) =
unsafe { (self.def.vtable.object_get_entry)(self.value.data(), index)? };
// The value is also a DynamicValue with the same shape
Some((key, unsafe {
Peek::unchecked_new(value_ptr, self.value.shape())
}))
}
/// Returns a value from the object by key, None if not an object or key not found
#[inline]
pub fn object_get(&self, key: &str) -> Option<Peek<'mem, 'facet>> {
let ptr = unsafe { (self.def.vtable.object_get)(self.value.data(), key)? };
// The value is also a DynamicValue with the same shape
Some(unsafe { Peek::unchecked_new(ptr, self.value.shape()) })
}
/// Returns an iterator over array elements if this is an array
#[inline]
pub fn array_iter(&self) -> Option<PeekDynamicValueArrayIter<'mem, 'facet>> {
let len = self.array_len()?;
Some(PeekDynamicValueArrayIter {
dyn_value: *self,
index: 0,
len,
})
}
/// Returns an iterator over object entries if this is an object
#[inline]
pub fn object_iter(&self) -> Option<PeekDynamicValueObjectIter<'mem, 'facet>> {
let len = self.object_len()?;
Some(PeekDynamicValueObjectIter {
dyn_value: *self,
index: 0,
len,
})
}
/// Structurally hash the dynamic value's contents.
///
/// This is called by `Peek::structural_hash` for dynamic values.
pub fn structural_hash_inner<H: core::hash::Hasher>(&self, hasher: &mut H) {
use core::hash::Hash;
// Hash the kind discriminant
let kind = self.kind();
core::mem::discriminant(&kind).hash(hasher);
match kind {
DynValueKind::Null => {
// Nothing more to hash
}
DynValueKind::Bool => {
if let Some(b) = self.as_bool() {
b.hash(hasher);
}
}
DynValueKind::Number => {
// Try to get as various number types and hash
if let Some(n) = self.as_i64() {
0u8.hash(hasher); // discriminant for i64
n.hash(hasher);
} else if let Some(n) = self.as_u64() {
1u8.hash(hasher); // discriminant for u64
n.hash(hasher);
} else if let Some(n) = self.as_f64() {
2u8.hash(hasher); // discriminant for f64
n.to_bits().hash(hasher);
}
}
DynValueKind::String => {
if let Some(s) = self.as_str() {
s.hash(hasher);
}
}
DynValueKind::Bytes => {
if let Some(b) = self.as_bytes() {
b.hash(hasher);
}
}
DynValueKind::Array => {
if let Some(len) = self.array_len() {
len.hash(hasher);
if let Some(iter) = self.array_iter() {
for elem in iter {
elem.structural_hash(hasher);
}
}
}
}
DynValueKind::Object => {
if let Some(len) = self.object_len() {
len.hash(hasher);
if let Some(iter) = self.object_iter() {
for (key, value) in iter {
key.hash(hasher);
value.structural_hash(hasher);
}
}
}
}
DynValueKind::DateTime | DynValueKind::QName | DynValueKind::Uuid => {
// Hash the string representation
if let Some(s) = self.as_str() {
s.hash(hasher);
}
}
_ => {
// Unknown future kind: the discriminant is already hashed above;
// fall back to hashing its string representation if available,
// mirroring the DateTime/QName/Uuid arm.
if let Some(s) = self.as_str() {
s.hash(hasher);
}
}
}
}
}
/// Iterator over array elements in a dynamic value
pub struct PeekDynamicValueArrayIter<'mem, 'facet> {
dyn_value: PeekDynamicValue<'mem, 'facet>,
index: usize,
len: usize,
}
impl<'mem, 'facet> Iterator for PeekDynamicValueArrayIter<'mem, 'facet> {
type Item = Peek<'mem, 'facet>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.len {
return None;
}
let item = self.dyn_value.array_get(self.index)?;
self.index += 1;
Some(item)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.len.saturating_sub(self.index);
(remaining, Some(remaining))
}
}
impl ExactSizeIterator for PeekDynamicValueArrayIter<'_, '_> {}
/// Iterator over object entries in a dynamic value
pub struct PeekDynamicValueObjectIter<'mem, 'facet> {
dyn_value: PeekDynamicValue<'mem, 'facet>,
index: usize,
len: usize,
}
impl<'mem, 'facet> Iterator for PeekDynamicValueObjectIter<'mem, 'facet> {
type Item = (&'mem str, Peek<'mem, 'facet>);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.len {
return None;
}
let entry = self.dyn_value.object_get_entry(self.index)?;
self.index += 1;
Some(entry)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.len.saturating_sub(self.index);
(remaining, Some(remaining))
}
}
impl ExactSizeIterator for PeekDynamicValueObjectIter<'_, '_> {}
impl core::fmt::Debug for PeekDynamicValue<'_, '_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("PeekDynamicValue")
.field("kind", &self.kind())
.finish_non_exhaustive()
}
}