1use alloc::borrow::Cow;
3use alloc::vec::Vec;
4
5use crate::State;
6use crate::de::{Deserialize, SinkHandle, atom_into_handle, borrowed_atom_into_handle, update};
7use crate::error::Error;
8use crate::event::{Atom, ContainerShape};
9use crate::ser::{Begin, Describe, Emit, PlainSink, Serialize};
10
11#[cfg_attr(
61 feature = "derive",
62 doc = "[container-adapters]: crate::derive#container-adapters"
63)]
64#[cfg_attr(
65 not(feature = "derive"),
66 doc = "[container-adapters]: https://docs.rs/deser/latest/deser/derive/index.html#container-adapters"
67)]
68pub struct Derived;
69
70#[doc(hidden)]
76pub trait DerivedDeserialize<'de>: Sized + Send {
77 fn deserialize_into<'out>(
78 out: &'out mut Option<Self>,
79 state: &mut State,
80 ) -> SinkHandle<'out, 'de>;
81
82 fn initial_value() -> Option<Self> {
83 None
84 }
85
86 fn expecting() -> Cow<'static, str> {
87 crate::de::slot::short_type_name(core::any::type_name::<Self>())
88 }
89
90 fn describe_type(d: &mut dyn Describe) {
91 let _ = d;
92 }
93
94 fn deserialize_update<'out>(value: &'out mut Self, state: &mut State) -> SinkHandle<'out, 'de> {
95 update::replace_handle_with(
96 value,
97 <Self as DerivedDeserialize<'de>>::deserialize_into,
98 state,
99 )
100 }
101
102 fn __private_atom_into(
103 out: &mut Option<Self>,
104 atom: Atom,
105 state: &mut State,
106 ) -> Result<(), Error> {
107 atom_into_handle(
108 <Self as DerivedDeserialize<'de>>::deserialize_into(out, state),
109 atom,
110 state,
111 )
112 }
113
114 fn __private_borrowed_atom_into(
115 out: &mut Option<Self>,
116 atom: Atom<'de>,
117 state: &mut State,
118 ) -> Result<(), Error> {
119 borrowed_atom_into_handle(
120 <Self as DerivedDeserialize<'de>>::deserialize_into(out, state),
121 atom,
122 state,
123 )
124 }
125
126 fn __private_is_bytes() -> bool {
127 false
128 }
129
130 fn __private_vec_from_bytes(bytes: Vec<u8>) -> Option<Vec<Self>> {
131 let _ = bytes;
132 None
133 }
134
135 fn __private_array_from_bytes<const N: usize>(bytes: &[u8]) -> Option<[Self; N]> {
136 let _ = bytes;
137 None
138 }
139}
140
141#[doc(hidden)]
147pub trait DerivedSerialize: Sync {
148 fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error>;
149
150 fn finish(value: &Self, state: &mut State) -> Result<(), Error> {
151 let _ = (value, state);
152 Ok(())
153 }
154
155 fn is_optional(value: &Self) -> bool {
156 let _ = value;
157 false
158 }
159
160 fn describe(value: &Self, d: &mut dyn Describe) {
161 let _ = (value, d);
162 }
163
164 fn container_shape(value: &Self) -> ContainerShape {
165 let _ = value;
166 ContainerShape::new()
167 }
168
169 fn __private_begin<'a>(value: &'a Self, state: &mut State) -> Result<Begin<'a>, Error> {
170 let shape = <Self as DerivedSerialize>::container_shape(value);
171 Ok(Begin::emit(
172 <Self as DerivedSerialize>::serialize(value, state)?,
173 shape,
174 true,
175 ))
176 }
177
178 fn __private_is_plain() -> bool {
179 false
180 }
181
182 fn __private_is_plain_value(value: &Self) -> bool {
183 let _ = value;
184 <Self as DerivedSerialize>::__private_is_plain()
185 }
186
187 fn __private_emit_plain(value: &Self, sink: &mut dyn PlainSink) -> Result<(), Error> {
188 let _ = (value, sink);
189 unreachable!("not a plain value")
190 }
191
192 fn __private_plain_cost(value: &Self, budget: usize) -> Option<usize> {
193 let _ = value;
194 budget.checked_sub(1)
195 }
196
197 fn __private_slice_as_bytes(_val: &[Self]) -> Option<Cow<'_, [u8]>>
198 where
199 Self: Sized,
200 {
201 None
202 }
203}
204
205impl<'de, T: DerivedDeserialize<'de>> Deserialize<'de, T> for Derived {
206 #[inline]
207 fn deserialize_into<'out>(
208 out: &'out mut Option<T>,
209 state: &mut State,
210 ) -> SinkHandle<'out, 'de> {
211 T::deserialize_into(out, state)
212 }
213
214 fn expecting() -> Cow<'static, str> {
215 T::expecting()
216 }
217
218 fn describe_type(d: &mut dyn Describe) {
219 T::describe_type(d)
220 }
221
222 #[inline]
223 fn initial_value() -> Option<T> {
224 T::initial_value()
225 }
226
227 #[inline]
228 fn deserialize_update<'out>(value: &'out mut T, state: &mut State) -> SinkHandle<'out, 'de> {
229 T::deserialize_update(value, state)
230 }
231
232 #[inline]
233 fn __private_atom_into(
234 out: &mut Option<T>,
235 atom: Atom,
236 state: &mut State,
237 ) -> Result<(), Error> {
238 T::__private_atom_into(out, atom, state)
239 }
240
241 #[inline]
242 fn __private_borrowed_atom_into(
243 out: &mut Option<T>,
244 atom: Atom<'de>,
245 state: &mut State,
246 ) -> Result<(), Error> {
247 T::__private_borrowed_atom_into(out, atom, state)
248 }
249
250 #[inline]
251 fn __private_is_bytes() -> bool {
252 T::__private_is_bytes()
253 }
254
255 #[inline]
256 fn __private_vec_from_bytes(bytes: Vec<u8>) -> Option<Vec<T>> {
257 T::__private_vec_from_bytes(bytes)
258 }
259
260 #[inline]
261 fn __private_array_from_bytes<const N: usize>(bytes: &[u8]) -> Option<[T; N]> {
262 T::__private_array_from_bytes(bytes)
263 }
264}
265
266impl<T: DerivedSerialize + ?Sized> Serialize<T> for Derived {
267 #[inline]
268 fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error> {
269 T::serialize(value, state)
270 }
271
272 #[inline]
273 fn finish(value: &T, state: &mut State) -> Result<(), Error> {
274 T::finish(value, state)
275 }
276
277 #[inline]
278 fn is_optional(value: &T) -> bool {
279 T::is_optional(value)
280 }
281
282 #[inline]
283 fn container_shape(value: &T) -> ContainerShape {
284 T::container_shape(value)
285 }
286
287 fn describe(value: &T, d: &mut dyn Describe) {
288 T::describe(value, d)
289 }
290
291 #[inline]
292 fn __private_begin<'a>(value: &'a T, state: &mut State) -> Result<Begin<'a>, Error> {
293 T::__private_begin(value, state)
294 }
295
296 #[inline]
297 fn __private_is_plain() -> bool {
298 T::__private_is_plain()
299 }
300
301 #[inline]
302 fn __private_is_plain_value(value: &T) -> bool {
303 T::__private_is_plain_value(value)
304 }
305
306 #[inline]
307 fn __private_emit_plain(value: &T, sink: &mut dyn PlainSink) -> Result<(), Error> {
308 T::__private_emit_plain(value, sink)
309 }
310
311 #[inline]
312 fn __private_plain_cost(value: &T, budget: usize) -> Option<usize> {
313 T::__private_plain_cost(value, budget)
314 }
315
316 #[inline]
317 fn __private_slice_as_bytes(val: &[T]) -> Option<Cow<'_, [u8]>>
318 where
319 T: Sized,
320 {
321 T::__private_slice_as_bytes(val)
322 }
323}