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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//! Compile fail tests.
/// ```compile_fail
/// use wincode::{SchemaRead, SchemaWrite, deserialize, serialize};
///
/// #[derive(SchemaRead, SchemaWrite)]
/// struct StaticStr {
/// value: Option<&'static str>,
/// }
///
/// let serialized = serialize(&StaticStr {
/// value: Some("actually static"),
/// })
/// .unwrap();
///
/// let _: StaticStr = deserialize(&serialized).unwrap();
/// ```
///
/// ```
/// use wincode::{SchemaRead, SchemaWrite, deserialize};
///
/// #[derive(SchemaRead, SchemaWrite)]
/// struct StaticStr {
/// value: Option<&'static str>,
/// }
///
/// let serialized = b"\x00".as_slice();
///
/// let _: StaticStr = deserialize(serialized).unwrap();
/// ```
/// ```compile_fail
/// use wincode::{SchemaRead, SchemaWrite};
///
/// #[derive(SchemaRead, SchemaWrite)]
/// enum MyEnum {
/// #[wincode(tag = 1)]
/// Foo,
/// #[wincode(tag = 1)]
/// Bar,
/// }
/// ```
/// ```compile_fail
/// use wincode::{SchemaRead, SchemaWrite};
///
/// #[derive(SchemaRead, SchemaWrite)]
/// enum MyEnum {
/// #[wincode(tag = 1)]
/// Foo,
/// #[wincode(tag = 0x1)]
/// Bar,
/// }
/// ```
/// ```compile_fail
/// use wincode::{SchemaRead, SchemaWrite};
///
/// #[derive(SchemaRead, SchemaWrite)]
/// enum MyEnum {
/// Foo,
/// #[wincode(tag = 0)]
/// Bar,
/// }
/// ```
/// A field-level context requires the derive's top-level context type.
///
/// ```compile_fail
/// use wincode::SchemaRead;
///
/// #[derive(SchemaRead)]
/// struct MissingContext {
/// #[wincode(context)]
/// value: u8,
/// }
/// ```
/// `UninitBuilder` does not generate contextual field readers.
///
/// ```compile_fail
/// #[derive(wincode::UninitBuilder)]
/// #[wincode(context = "()")]
/// struct Contextual {
/// value: u8,
/// }
/// ```
/// A lifetime supplied by the context still has to be tied to the input when an ordinary
/// field uses that same lifetime. Consequently, the derived value below cannot outlive the
/// serialized bytes borrowed by `borrowed`.
///
/// ```compile_fail
/// use {
/// bumpalo::{Bump, collections::String},
/// wincode::{SchemaRead, deserialize_with_context},
/// };
///
/// #[derive(SchemaRead)]
/// #[wincode(context = "&'bump Bump")]
/// struct Mixed<'bump> {
/// #[wincode(context)]
/// owned: String<'bump>,
/// borrowed: &'bump u8,
/// }
///
/// fn deserialize_from_short_input<'bump>(bump: &'bump Bump) -> Mixed<'bump> {
/// let bytes = [0u8];
/// deserialize_with_context(bump, &bytes).unwrap()
/// }
/// ```
/// A lifetime distinct from the context lifetime remains tied to the serialized input.
/// The context-backed generic field may outlive `bytes`, but the complete value cannot because
/// `borrowed` borrows from them.
///
/// ```compile_fail
/// use {
/// bumpalo::{Bump, collections::Vec},
/// wincode::{SchemaRead, SchemaWrite, deserialize_with_context, serialize},
/// };
///
/// #[derive(SchemaRead, SchemaWrite)]
/// #[wincode(context = "&'bump Bump")]
/// struct Foo<'bump, 'input, T> {
/// #[wincode(context)]
/// values: Vec<'bump, T>,
/// borrowed: &'input u8,
/// }
///
/// let bump = Bump::new();
/// let borrowed = 42;
/// let foo = Foo {
/// values: bumpalo::vec![in ≎ 1_u16, 2, 3],
/// borrowed: &borrowed,
/// };
/// let deserialized: Foo<u16> = {
/// let serialized = serialize(&foo).unwrap();
/// let deserialized = deserialize_with_context(&bump, &serialized).unwrap();
/// deserialized
/// };
///
/// let _borrowed = deserialized.borrowed;
/// ```
/// A contextual adapter must produce exactly the field type expected by the derived struct.
/// Otherwise, the struct's raw placement initialization could initialize the wrong type.
///
/// ```compile_fail
/// use {
/// core::mem::MaybeUninit,
/// wincode::{
/// ReadResult, SchemaRead, SchemaReadContext, config::ConfigCore, io::Reader,
/// },
/// };
///
/// struct Context;
/// struct Adapter;
///
/// unsafe impl<'de, C: ConfigCore> SchemaReadContext<'de, C, Context> for Adapter {
/// type Dst = u16;
///
/// fn read_with_context(
/// _ctx: Context,
/// _reader: impl Reader<'de>,
/// dst: &mut MaybeUninit<Self::Dst>,
/// ) -> ReadResult<()> {
/// dst.write(0);
/// Ok(())
/// }
/// }
///
/// #[derive(SchemaRead)]
/// #[wincode(context = "Context")]
/// struct Contextual {
/// #[wincode(context, with = "Adapter")]
/// value: u8,
/// }
/// ```
/// Rewriting an input-backed field from `'ctx` to `'de` is only valid when the resulting value
/// can be safely shortened back to the field's declared type. An outlives bound alone is not
/// enough for contravariant or invariant types.
///
/// ```compile_fail
/// use {
/// core::mem::MaybeUninit,
/// wincode::{ReadResult, SchemaRead, config::ConfigCore, io::Reader},
/// };
///
/// struct Callback;
///
/// fn ignore(_: &u8) {}
///
/// unsafe impl<'de, C: ConfigCore> SchemaRead<'de, C> for Callback {
/// type Dst = fn(&'de u8);
///
/// fn read(
/// _reader: impl Reader<'de>,
/// dst: &mut MaybeUninit<Self::Dst>,
/// ) -> ReadResult<()> {
/// dst.write(ignore);
/// Ok(())
/// }
/// }
///
/// #[derive(SchemaRead)]
/// #[wincode(context = "&'ctx ()")]
/// struct Contextual<'ctx> {
/// #[wincode(with = "Callback")]
/// callback: fn(&'ctx u8),
/// }
/// ```
/// A contextual outer field cannot hide an ordinary input borrow made by its nested derived
/// reader. The nested reader's exact `Dst` bound carries the required `'de: 'ctx` relationship
/// even though the outer field itself is marked `context`.
///
/// ```compile_fail
/// use wincode::{SchemaRead, deserialize_with_context};
///
/// #[derive(SchemaRead)]
/// #[wincode(context = "&'ctx ()")]
/// struct Inner<'ctx> {
/// borrowed: &'ctx u8,
/// }
///
/// #[derive(SchemaRead)]
/// #[wincode(context = "&'ctx ()")]
/// struct Outer<'ctx> {
/// #[wincode(context)]
/// inner: Inner<'ctx>,
/// }
///
/// fn deserialize_from_short_input<'ctx>(ctx: &'ctx ()) -> Outer<'ctx> {
/// let bytes = [0u8];
/// deserialize_with_context(ctx, &bytes).unwrap()
/// }
/// ```
/// An ordinary input borrow hidden behind an associated type cannot evade the destination-type
/// constraint merely because the derive's syntactic lifetime scan cannot see it.
///
/// ```compile_fail
/// use {
/// core::marker::PhantomData,
/// wincode::{SchemaRead, deserialize_with_context},
/// };
///
/// trait HasValue {
/// type Value;
/// }
///
/// struct Borrowed<'a>(PhantomData<&'a ()>);
///
/// impl<'a> HasValue for Borrowed<'a> {
/// type Value = &'a u8;
/// }
///
/// #[derive(SchemaRead)]
/// #[wincode(context = "&'ctx ()")]
/// struct Associated<'ctx, T: HasValue> {
/// value: T::Value,
/// #[wincode(skip)]
/// lifetime: PhantomData<&'ctx ()>,
/// }
///
/// fn deserialize_from_short_input<'ctx>(ctx: &'ctx ()) -> Associated<'ctx, Borrowed<'ctx>> {
/// let bytes = [0u8];
/// deserialize_with_context(ctx, &bytes).unwrap()
/// }
/// ```
/// A contextual derive cannot request the ordinary `ZeroCopy` marker because that marker does
/// not identify the context or destination type.
///
/// ```compile_fail
/// use wincode::SchemaRead;
///
/// #[derive(SchemaRead)]
/// #[wincode(context = "()", assert_zero_copy)]
/// #[repr(transparent)]
/// struct Contextual(u8);
/// ```
/// A `read_<field>` on an `UninitBuilder` must not launder the reader lifetime: reading
/// from a short-lived reader into a longer-lived borrowed field (here `'static`) would
/// leave a dangling reference after `assume_init`.
///
/// ```compile_fail
/// use {core::mem::MaybeUninit, wincode::config::DefaultConfig};
///
/// #[derive(wincode::UninitBuilder)]
/// struct Borrowed<'a> {
/// data: &'a [u8],
/// }
///
/// fn launder() -> Borrowed<'static> {
/// let mut uninit = MaybeUninit::<Borrowed<'static>>::uninit();
/// {
/// let short_lived: Vec<u8> = vec![3, 0, 0, 0, 0, 0, 0, 0, 0xAA, 0xBB, 0xCC];
/// let mut builder =
/// BorrowedUninitBuilder::<DefaultConfig>::from_maybe_uninit_mut(&mut uninit);
/// builder.read_data(short_lived.as_slice()).unwrap();
/// builder.finish();
/// }
/// unsafe { uninit.assume_init() }
/// }
/// ```
/// An `UninitBuilder` reader must write exactly the field type, even when the field's
/// `SchemaRead` implementation declares a different `Dst`.
///
/// ```compile_fail
/// use {
/// core::mem::MaybeUninit,
/// wincode::{
/// ReadResult, SchemaRead,
/// config::{ConfigCore, DefaultConfig},
/// io::Reader,
/// },
/// };
///
/// struct Field;
/// struct DifferentDst(u64);
///
/// unsafe impl<'de, C: ConfigCore> SchemaRead<'de, C> for Field {
/// type Dst = DifferentDst;
///
/// fn read(
/// _reader: impl Reader<'de>,
/// dst: &mut MaybeUninit<Self::Dst>,
/// ) -> ReadResult<()> {
/// dst.write(DifferentDst(0));
/// Ok(())
/// }
/// }
///
/// #[derive(wincode::UninitBuilder)]
/// struct HasField {
/// field: Field,
/// }
///
/// let mut uninit = MaybeUninit::<HasField>::uninit();
/// let mut builder =
/// HasFieldUninitBuilder::<DefaultConfig>::from_maybe_uninit_mut(&mut uninit);
/// builder.read_field([].as_slice()).unwrap();
/// ```