wincode 0.5.4

Fast bincode de/serialization with placement initialization
Documentation
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use {
    crate::io::{WriteResult, Writer, slice::SliceMutUnchecked, write_size_limit},
    std::io::{BufWriter, Cursor, Write},
};

/// [`Writer`] adapter over any [`std::io::Write`] sink.
///
/// Wraps any `W: std::io::Write` and exposes it as a wincode [`Writer`], allowing
/// serialization into files, network streams, or other I/O sinks.
///
/// # Examples
///
/// Serialize a tuple into a `Vec<u8>` via `WriteAdapter`:
///
/// ```
/// use wincode::{Serialize, io::{Writer, std_write::WriteAdapter}};
///
/// let tuple = (42u32, true, 1234567890i64);
/// let mut buf = Vec::new();
/// let mut writer = WriteAdapter::new(&mut buf);
/// <(u32, bool, i64)>::serialize_into(&mut writer, &tuple).unwrap();
/// writer.finish().unwrap();
/// assert_eq!(buf, wincode::serialize(&tuple).unwrap());
/// ```
#[derive(Debug)]
pub struct WriteAdapter<W: ?Sized>(W);

impl<W: Write> WriteAdapter<W> {
    pub fn new(writer: W) -> Self {
        Self(writer)
    }
}

impl<W: Write + ?Sized> Writer for WriteAdapter<W> {
    fn write(&mut self, src: &[u8]) -> WriteResult<()> {
        Ok(self.0.write_all(src)?)
    }

    fn finish(&mut self) -> WriteResult<()> {
        Ok(self.0.flush()?)
    }
}

impl<W: Write + ?Sized> Writer for BufWriter<W> {
    fn write(&mut self, src: &[u8]) -> WriteResult<()> {
        Ok(self.write_all(src)?)
    }

    fn finish(&mut self) -> WriteResult<()> {
        Ok(self.flush()?)
    }
}

#[inline]
fn cursor_slice_as_trusted_for(
    cursor: &mut Cursor<impl AsMut<[u8]>>,
    n_bytes: usize,
) -> WriteResult<impl Writer> {
    let Ok(pos) = usize::try_from(cursor.position()) else {
        return Err(write_size_limit(usize::MAX));
    };

    let inner = cursor.get_mut().as_mut();
    let next_pos = pos.saturating_add(n_bytes);
    if next_pos > inner.len() {
        return Err(write_size_limit(n_bytes));
    }

    cursor.set_position(next_pos as u64);
    let slice = &mut cursor.get_mut().as_mut()[pos..next_pos];
    // SAFETY: by calling `as_trusted_for`, caller guarantees they
    // will fully initialize `n_bytes` of memory and will not write
    // beyond the bounds of the slice.
    Ok(unsafe { SliceMutUnchecked::new(slice) })
}

impl Writer for Cursor<&mut [u8]> {
    #[inline]
    fn write(&mut self, src: &[u8]) -> WriteResult<()> {
        Ok(self.write_all(src)?)
    }

    #[inline]
    fn finish(&mut self) -> WriteResult<()> {
        Ok(self.flush()?)
    }

    #[inline]
    unsafe fn as_trusted_for(&mut self, n_bytes: usize) -> WriteResult<impl Writer> {
        cursor_slice_as_trusted_for(self, n_bytes)
    }
}

impl<const N: usize> Writer for Cursor<[u8; N]> {
    #[inline]
    fn write(&mut self, src: &[u8]) -> WriteResult<()> {
        Ok(self.write_all(src)?)
    }

    #[inline]
    fn finish(&mut self) -> WriteResult<()> {
        Ok(self.flush()?)
    }

    #[inline]
    unsafe fn as_trusted_for(&mut self, n_bytes: usize) -> WriteResult<impl Writer> {
        cursor_slice_as_trusted_for(self, n_bytes)
    }
}

impl Writer for Cursor<Box<[u8]>> {
    #[inline]
    fn write(&mut self, src: &[u8]) -> WriteResult<()> {
        Ok(self.write_all(src)?)
    }

    #[inline]
    fn finish(&mut self) -> WriteResult<()> {
        Ok(self.flush()?)
    }

    #[inline]
    unsafe fn as_trusted_for(&mut self, n_bytes: usize) -> WriteResult<impl Writer> {
        cursor_slice_as_trusted_for(self, n_bytes)
    }
}

#[inline]
fn cursor_vec_as_trusted_for(
    cursor: &mut Cursor<impl AsMut<Vec<u8>>>,
    n_bytes: usize,
) -> WriteResult<impl Writer> {
    let Ok(pos) = usize::try_from(cursor.position()) else {
        return Err(write_size_limit(usize::MAX));
    };

    let vec = cursor.get_mut().as_mut();
    crate::io::cursor::vec::prepare_write(vec, pos, n_bytes)?;

    // SAFETY: by calling `as_trusted_for`, caller guarantees they
    // will fully initialize `n_bytes` of memory and will not write
    // beyond the bounds of the slice.
    Ok(unsafe { CursorVecUnchecked::new(cursor) })
}

struct CursorVecUnchecked<'a, T> {
    inner: &'a mut Cursor<T>,
}

impl<'a, T> CursorVecUnchecked<'a, T> {
    /// # Safety
    ///
    /// The caller must ensure that `inner.position()` fits within the backing
    /// vector's capacity, that any gap before that position has already been
    /// initialized, and that all writes through this writer stay within the
    /// trusted window reserved by `as_trusted_for`.
    const unsafe fn new(inner: &'a mut Cursor<T>) -> Self {
        Self { inner }
    }
}

impl<'a, T> Writer for CursorVecUnchecked<'a, T>
where
    T: AsMut<Vec<u8>>,
{
    #[inline]
    fn write(&mut self, src: &[u8]) -> WriteResult<()> {
        let cur_pos = self.inner.position();
        let inner = self.inner.get_mut().as_mut();
        let cur_len = inner.len();
        // `cursor_vec_as_trusted_for` checked that the trusted window end fits in
        // `usize`, and the trusted contract requires `next_pos` to stay within that
        // window.
        #[expect(clippy::arithmetic_side_effects)]
        let next_pos = cur_pos + src.len() as u64;

        // SAFETY:
        // - `cursor_vec_as_trusted_for` ensured sufficient capacity for the trusted window before
        //   constructing this writer.
        // - The trusted-writer contract requires all writes through this writer to stay
        //   within that reserved window.
        // - Given Rust's aliasing rules, we can assume that `src` does not overlap with
        //   the internal buffer.
        unsafe {
            core::ptr::copy_nonoverlapping(
                src.as_ptr(),
                // `cursor_vec_as_trusted_for` checked that the initial cursor
                // position plus the trusted window fits in `usize`. The trusted-writer
                // contract requires all writes through this writer to stay within that
                // reserved window, so `cur_pos` also fits in `usize`.
                inner.as_mut_ptr().add(cur_pos as usize),
                src.len(),
            );
        }

        if next_pos > cur_len as u64 {
            // SAFETY: any gap before the trusted window was initialized before
            // constructing this writer, and the copy above initialized `cur_pos..next_pos`.
            unsafe {
                // `cursor_vec_as_trusted_for` checked that the trusted window end fits in
                // `usize`, and the trusted contract requires `next_pos` to stay within that
                // window.
                inner.set_len(next_pos as usize)
            }
        }

        self.inner.set_position(next_pos);

        Ok(())
    }
}

impl Writer for Cursor<Vec<u8>> {
    #[inline]
    fn write(&mut self, src: &[u8]) -> WriteResult<()> {
        Ok(self.write_all(src)?)
    }

    #[inline]
    fn finish(&mut self) -> WriteResult<()> {
        Ok(self.flush()?)
    }

    #[inline]
    unsafe fn as_trusted_for(&mut self, n_bytes: usize) -> WriteResult<impl Writer> {
        cursor_vec_as_trusted_for(self, n_bytes)
    }
}

impl Writer for Cursor<&mut Vec<u8>> {
    #[inline]
    fn write(&mut self, src: &[u8]) -> WriteResult<()> {
        Ok(self.write_all(src)?)
    }

    #[inline]
    fn finish(&mut self) -> WriteResult<()> {
        Ok(self.flush()?)
    }

    #[inline]
    unsafe fn as_trusted_for(&mut self, n_bytes: usize) -> WriteResult<impl Writer> {
        cursor_vec_as_trusted_for(self, n_bytes)
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        crate::{
            io::WriteError,
            serde::{Serialize, serialize, serialized_size},
        },
    };

    const MAGIC: u64 = 0xdeadbeef_cafebabe;
    const DATA: &[(u32, bool, &u64)] = &[
        (1u32, false, &MAGIC),
        (2u32, true, &MAGIC),
        (3u32, false, &MAGIC),
    ];

    fn assert_serializes_data(mut writer: impl Writer) {
        <[(u32, bool, &u64)]>::serialize_into(writer.by_ref(), DATA).unwrap();
        writer.finish().unwrap();
    }

    #[test]
    fn write_adapter_serialize_tuples() {
        let mut buf = Vec::new();
        assert_serializes_data(WriteAdapter::new(&mut buf));
        assert_eq!(buf, serialize(DATA).unwrap());
    }

    #[test]
    fn buf_writer_serialize_tuples() {
        let mut buf = Vec::new();
        assert_serializes_data(BufWriter::new(&mut buf));
        assert_eq!(buf, serialize(DATA).unwrap());
    }

    #[test]
    fn cursor_vec_writer_serialize_tuples() {
        let mut buf = Cursor::new(Vec::new());
        assert_serializes_data(&mut buf);
        assert_eq!(buf.into_inner(), serialize(DATA).unwrap());
    }

    #[test]
    fn cursor_slice_writer_serialize_tuples() {
        let size = serialized_size(DATA).unwrap() as usize;
        let mut buf = Cursor::new(vec![0; size].into_boxed_slice());
        assert_serializes_data(&mut buf);
        assert_eq!(buf.into_inner().as_ref(), serialize(DATA).unwrap());
    }

    fn write_trusted(writer: &mut impl Writer, bytes: &[u8]) {
        let mut trusted = unsafe { writer.as_trusted_for(bytes.len()) }.unwrap();
        trusted.write(bytes).unwrap();
        trusted.finish().unwrap();
    }

    macro_rules! with_vec_cursors {
        ($inner:expr, |$reader: ident| $body: block) => {{
            {
                let mut $reader = Cursor::new($inner.clone());
                $body
            }
            {
                let mut $reader = Cursor::new(&mut $inner);
                $body
            }
        }};
    }

    #[test]
    fn cursor_vec_trusted_append_with_spare_capacity() {
        let mut inner = Vec::with_capacity(8);
        with_vec_cursors!(inner, |cursor| {
            cursor.write_all(&[1, 2, 3]).unwrap();

            write_trusted(&mut cursor, &[4, 5]);
            cursor.finish().unwrap();

            assert_eq!(&*cursor.into_inner(), &vec![1, 2, 3, 4, 5]);
        });
    }

    #[test]
    fn cursor_vec_trusted_overwrite_then_extend() {
        let mut inner = vec![1, 2, 3, 4];
        with_vec_cursors!(inner, |cursor| {
            cursor.set_position(2);

            write_trusted(&mut cursor, &[9, 8, 7, 6]);
            cursor.finish().unwrap();

            assert_eq!(&cursor.into_inner()[..], &vec![1, 2, 9, 8, 7, 6]);
        });
    }

    #[test]
    fn cursor_vec_trusted_overwrite_preserves_tail() {
        let mut inner = vec![1, 2, 3, 4, 5, 6];
        with_vec_cursors!(inner, |cursor| {
            cursor.set_position(2);

            write_trusted(&mut cursor, &[9, 8]);
            cursor.finish().unwrap();

            assert_eq!(&cursor.into_inner()[..], &vec![1, 2, 9, 8, 5, 6]);
        });
    }

    #[test]
    fn cursor_vec_trusted_zero_fills_gap() {
        let mut inner = Vec::with_capacity(16);
        inner.extend_from_slice(&[1, 2, 3]);
        with_vec_cursors!(inner, |cursor| {
            cursor.set_position(6);

            write_trusted(&mut cursor, &[9, 10]);
            cursor.finish().unwrap();

            assert_eq!(&cursor.into_inner()[..], &vec![1, 2, 3, 0, 0, 0, 9, 10]);
        });
    }

    #[test]
    fn cursor_vec_trusted_does_not_extend_len_before_write() {
        let mut inner = Vec::with_capacity(16);
        with_vec_cursors!(inner, |cursor| {
            {
                let _trusted = unsafe { cursor.as_trusted_for(8) }.unwrap();
            }

            cursor.finish().unwrap();

            assert_eq!(cursor.into_inner().len(), 0);
        });
    }

    macro_rules! with_slice_cursors {
        ($inner:expr, |$reader: ident| $body: block) => {{
            {
                let mut $reader = Cursor::new($inner);
                $body
            }
            {
                let mut inner = $inner;
                let mut $reader = Cursor::new(&mut inner[..]);
                $body
            }
            {
                let mut $reader = Cursor::new(Box::from($inner));
                $body
            }
        }};
    }

    #[test]
    fn cursor_mut_slice_trusted_writes_in_bounds() {
        with_slice_cursors!([1, 2, 3, 4, 5], |cursor| {
            let pos = {
                cursor.set_position(1);

                write_trusted(&mut cursor, &[9, 8, 7]);
                cursor.finish().unwrap();
                cursor.position()
            };
            assert_eq!(&cursor.get_ref()[..], &[1, 9, 8, 7, 5]);
            assert_eq!(pos, 4);
        });
    }

    #[test]
    fn cursor_slice_trusted_out_of_bounds_errors() {
        with_slice_cursors!([1, 2, 3], |cursor| {
            cursor.set_position(2);
            let result = unsafe { cursor.as_trusted_for(2) };
            assert!(matches!(result, Err(WriteError::WriteSizeLimit(2))));
        });
    }
}