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
extern crate alloc;
use facet_core::Def;
use facet_reflect::Partial;
use crate::{
DeserializeError, FormatDeserializer, FormatParser, ParseEvent, ScalarTypeHint, ScalarValue,
};
impl<'input, const BORROW: bool, P> FormatDeserializer<'input, BORROW, P>
where
P: FormatParser<'input>,
{
pub(crate) fn deserialize_pointer(
&mut self,
mut wip: Partial<'input, BORROW>,
) -> Result<Partial<'input, BORROW>, DeserializeError<P::Error>> {
use facet_core::KnownPointer;
let shape = wip.shape();
let is_cow = if let Def::Pointer(ptr_def) = shape.def {
matches!(ptr_def.known, Some(KnownPointer::Cow))
} else {
false
};
if is_cow {
// Cow<str> - handle specially to preserve borrowing
if let Def::Pointer(ptr_def) = shape.def
&& let Some(pointee) = ptr_def.pointee()
&& pointee.type_identifier == "str"
{
// Hint to non-self-describing parsers that a string is expected
self.parser.hint_scalar_type(ScalarTypeHint::String);
let event = self.expect_event("string for Cow<str>")?;
match event {
ParseEvent::Scalar(ScalarValue::Str(s)) => {
// Pass through the Cow as-is to preserve borrowing
wip = wip.set(s).map_err(DeserializeError::reflect)?;
return Ok(wip);
}
_ => {
return Err(DeserializeError::TypeMismatch {
expected: "string for Cow<str>",
got: format!("{event:?}"),
span: self.last_span,
path: None,
});
}
}
}
// Cow<[u8]> - handle specially to preserve borrowing
if let Def::Pointer(ptr_def) = shape.def
&& let Some(pointee) = ptr_def.pointee()
&& let Def::Slice(slice_def) = pointee.def
&& slice_def.t.type_identifier == "u8"
{
// Hint to non-self-describing parsers that bytes are expected
self.parser.hint_scalar_type(ScalarTypeHint::Bytes);
let event = self.expect_event("bytes for Cow<[u8]>")?;
if let ParseEvent::Scalar(ScalarValue::Bytes(b)) = event {
// Pass through the Cow as-is to preserve borrowing
wip = wip.set(b).map_err(DeserializeError::reflect)?;
return Ok(wip);
} else {
return Err(DeserializeError::TypeMismatch {
expected: "bytes for Cow<[u8]>",
got: format!("{event:?}"),
span: self.last_span,
path: None,
});
}
}
// Other Cow types - use begin_inner
wip = wip.begin_inner().map_err(DeserializeError::reflect)?;
wip = self.deserialize_into(wip)?;
wip = wip.end().map_err(DeserializeError::reflect)?;
return Ok(wip);
}
// &str - handle specially for zero-copy borrowing
if let Def::Pointer(ptr_def) = shape.def
&& matches!(ptr_def.known, Some(KnownPointer::SharedReference))
&& ptr_def
.pointee()
.is_some_and(|p| p.type_identifier == "str")
{
// Hint to non-self-describing parsers that a string is expected
self.parser.hint_scalar_type(ScalarTypeHint::String);
let event = self.expect_event("string for &str")?;
match event {
ParseEvent::Scalar(ScalarValue::Str(s)) => {
return self.set_string_value(wip, s);
}
_ => {
return Err(DeserializeError::TypeMismatch {
expected: "string for &str",
got: format!("{event:?}"),
span: self.last_span,
path: None,
});
}
}
}
// &[u8] - handle specially for zero-copy borrowing
if let Def::Pointer(ptr_def) = shape.def
&& matches!(ptr_def.known, Some(KnownPointer::SharedReference))
&& let Some(pointee) = ptr_def.pointee()
&& let Def::Slice(slice_def) = pointee.def
&& slice_def.t.type_identifier == "u8"
{
// Hint to non-self-describing parsers that bytes are expected
self.parser.hint_scalar_type(ScalarTypeHint::Bytes);
let event = self.expect_event("bytes for &[u8]")?;
if let ParseEvent::Scalar(ScalarValue::Bytes(b)) = event {
return self.set_bytes_value(wip, b);
} else {
return Err(DeserializeError::TypeMismatch {
expected: "bytes for &[u8]",
got: format!("{event:?}"),
span: self.last_span,
path: None,
});
}
}
// Regular smart pointer (Box, Arc, Rc)
wip = wip.begin_smart_ptr().map_err(DeserializeError::reflect)?;
// Check if begin_smart_ptr set up a slice builder (for Arc<[T]>, Rc<[T]>, Box<[T]>)
// In this case, we need to deserialize as a list manually
let is_slice_builder = wip.is_building_smart_ptr_slice();
if is_slice_builder {
// Deserialize the list elements into the slice builder
// We can't use deserialize_list() because it calls begin_list() which interferes
// Hint to non-self-describing parsers that a sequence is expected
self.parser.hint_sequence();
let event = self.expect_event("value")?;
match event {
ParseEvent::SequenceStart(_) => {}
ParseEvent::StructStart(kind) => {
return Err(DeserializeError::TypeMismatch {
expected: "array",
got: kind.name().into(),
span: self.last_span,
path: None,
});
}
_ => {
return Err(DeserializeError::TypeMismatch {
expected: "sequence start for Arc<[T]>/Rc<[T]>/Box<[T]>",
got: format!("{event:?}"),
span: self.last_span,
path: None,
});
}
};
loop {
let event = self.expect_peek("value")?;
// Check for end of sequence
if matches!(event, ParseEvent::SequenceEnd) {
self.expect_event("value")?;
break;
}
wip = wip.begin_list_item().map_err(DeserializeError::reflect)?;
wip = self.deserialize_into(wip)?;
wip = wip.end().map_err(DeserializeError::reflect)?;
}
// Convert the slice builder to Arc/Rc/Box and mark as initialized
wip = wip.end().map_err(DeserializeError::reflect)?;
// DON'T call end() again - the caller (deserialize_struct) will do that
} else {
// Regular smart pointer with sized pointee
wip = self.deserialize_into(wip)?;
wip = wip.end().map_err(DeserializeError::reflect)?;
}
Ok(wip)
}
}