pack-io 1.0.1

Compact binary wire format with schema evolution and zero-copy deserialization for Rust. The serialization substrate under network-protocol and Hive DB.
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
426
427
428
429
430
431
432
433
434
//! `std::io::Read` / `std::io::Write` integration: the streaming Tier-2
//! encoder and decoder pair, plus convenience free functions.
//!
//! This module is gated on the `std` feature (on by default). With `std` off,
//! the crate compiles for `no_std` targets using `core` + `alloc` only and
//! none of this module is reachable.
//!
//! ## When to use which entry point
//!
//! - For one-shot send / receive of a single value, prefer [`encode_into`] /
//!   [`decode_from`]: they take any `Write` / `Read` and handle the
//!   buffering.
//! - For interleaved writes / reads across many values without per-value
//!   allocation, instantiate an [`IoEncoder`] / [`IoDecoder`] and call
//!   `write` / `read` repeatedly.
//!
//! ## Errors
//!
//! Both directions surface I/O failure through the codec's
//! [`crate::SerialError`] type via [`SerialError::Io`]. The `Io` variant
//! captures `std::io::ErrorKind` and a message string — enough to surface
//! the original cause without taking on a non-`Clone` payload.

use std::io::{Read, Write};

use crate::codec::{Config, Decode, Encode};
use crate::error::{Result, SerialError};
use crate::traits::{Deserialize, Serialize};

/// Streaming encoder that writes directly into any [`Write`]-shaped sink.
///
/// Each [`IoEncoder::write`] call calls into the underlying writer for the
/// bytes the type produces. The encoder does **not** buffer; if you wrap a
/// raw socket / file, wrap it in a [`std::io::BufWriter`] first.
///
/// # Examples
///
/// ```
/// use pack_io::IoEncoder;
///
/// let mut sink: Vec<u8> = Vec::new();
/// let mut enc = IoEncoder::new(&mut sink);
/// enc.write(&42_u64).unwrap();
/// enc.write(&"hello").unwrap();
/// assert!(!sink.is_empty());
/// ```
#[derive(Debug)]
pub struct IoEncoder<W: Write> {
    writer: W,
}

impl<W: Write> IoEncoder<W> {
    /// Wrap `writer` in an encoder.
    ///
    /// The encoder does not buffer; wrap raw sockets / files in a
    /// [`std::io::BufWriter`] first if syscall amplification is a concern.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::IoEncoder;
    ///
    /// let mut sink: Vec<u8> = Vec::new();
    /// let _enc = IoEncoder::new(&mut sink);
    /// ```
    #[must_use]
    pub fn new(writer: W) -> Self {
        Self { writer }
    }

    /// Borrow the underlying writer.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::IoEncoder;
    ///
    /// let mut sink: Vec<u8> = Vec::new();
    /// let enc = IoEncoder::new(&mut sink);
    /// let _: &&mut Vec<u8> = &enc.writer();
    /// ```
    #[must_use]
    pub fn writer(&self) -> &W {
        &self.writer
    }

    /// Borrow the underlying writer mutably.
    ///
    /// Useful when downstream code needs `&mut W` to call writer-specific
    /// methods (e.g. `flush`) without consuming the encoder.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::{BufWriter, Write};
    /// use pack_io::IoEncoder;
    ///
    /// let mut sink: Vec<u8> = Vec::new();
    /// let buffered = BufWriter::new(&mut sink);
    /// let mut enc = IoEncoder::new(buffered);
    /// enc.write(&7_u64).unwrap();
    /// enc.writer_mut().flush().unwrap();
    /// ```
    #[must_use]
    pub fn writer_mut(&mut self) -> &mut W {
        &mut self.writer
    }

    /// Consume the encoder and return the underlying writer.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::IoEncoder;
    ///
    /// let sink: Vec<u8> = Vec::new();
    /// let mut enc = IoEncoder::new(sink);
    /// enc.write(&42_u64).unwrap();
    /// let written: Vec<u8> = enc.into_inner();
    /// assert_eq!(written, &[0x2a]);
    /// ```
    #[must_use]
    pub fn into_inner(self) -> W {
        self.writer
    }

    /// Encode `value` straight into the underlying writer.
    ///
    /// # Errors
    ///
    /// - Propagates any [`crate::SerialError`] from the type's [`Serialize`].
    /// - Maps any `std::io::Error` from the writer into [`SerialError::Io`].
    #[inline]
    pub fn write<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<()> {
        value.serialize(self)
    }
}

impl<W: Write> Encode for IoEncoder<W> {
    #[inline]
    fn write_byte(&mut self, byte: u8) -> Result<()> {
        self.writer.write_all(&[byte]).map_err(map_io_error)
    }

    #[inline]
    fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> {
        self.writer.write_all(bytes).map_err(map_io_error)
    }
}

/// Streaming decoder that reads directly from any [`Read`]-shaped source.
///
/// Each [`IoDecoder::read`] call may issue many small reads against the
/// underlying source. Wrap raw sockets / files in [`std::io::BufReader`]
/// first if read-syscall amplification is a concern.
///
/// # Examples
///
/// ```
/// use pack_io::{IoEncoder, IoDecoder};
/// use std::io::Cursor;
///
/// let mut buf: Vec<u8> = Vec::new();
/// {
///     let mut enc = IoEncoder::new(&mut buf);
///     enc.write(&42_u64).unwrap();
///     enc.write(&"hi").unwrap();
/// }
///
/// let mut dec = IoDecoder::new(Cursor::new(buf));
/// let n: u64 = dec.read().unwrap();
/// let s: String = dec.read().unwrap();
/// assert_eq!((n, s.as_str()), (42, "hi"));
/// ```
#[derive(Debug)]
pub struct IoDecoder<R: Read> {
    reader: R,
    config: Config,
}

impl<R: Read> IoDecoder<R> {
    /// Wrap `reader` with the default [`Config`] (1 GiB `max_alloc`).
    ///
    /// For tighter allocation caps on untrusted input, use
    /// [`IoDecoder::with_config`] instead.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use pack_io::IoDecoder;
    ///
    /// let bytes = pack_io::encode(&42_u64).unwrap();
    /// let mut dec = IoDecoder::new(Cursor::new(bytes));
    /// let n: u64 = dec.read().unwrap();
    /// assert_eq!(n, 42);
    /// ```
    #[must_use]
    pub fn new(reader: R) -> Self {
        Self {
            reader,
            config: Config::default(),
        }
    }

    /// Wrap `reader` with the supplied configuration.
    ///
    /// # Errors
    ///
    /// Returns [`SerialError::InvalidLength`] if `config.max_alloc == 0`.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use pack_io::{Config, IoDecoder};
    ///
    /// let cfg = Config::new().with_max_alloc(16 * 1024);
    /// let bytes = pack_io::encode(&"hello").unwrap();
    /// let mut dec = IoDecoder::with_config(Cursor::new(bytes), cfg).unwrap();
    /// let s: String = dec.read().unwrap();
    /// assert_eq!(s, "hello");
    /// ```
    pub fn with_config(reader: R, config: Config) -> Result<Self> {
        Ok(Self {
            reader,
            config: config.validate()?,
        })
    }

    /// Borrow the underlying reader.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use pack_io::IoDecoder;
    ///
    /// let dec = IoDecoder::new(Cursor::new(vec![0u8; 4]));
    /// assert_eq!(dec.reader().get_ref().len(), 4);
    /// ```
    #[must_use]
    pub fn reader(&self) -> &R {
        &self.reader
    }

    /// Consume the decoder and return the underlying reader.
    ///
    /// Useful when the caller wants to take back ownership of the source
    /// (e.g. to drop the reader, return it to a pool, or feed it to a
    /// different consumer) after the decoded prefix has been processed.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use pack_io::IoDecoder;
    ///
    /// let bytes = pack_io::encode(&42_u64).unwrap();
    /// let mut dec = IoDecoder::new(Cursor::new(bytes));
    /// let _n: u64 = dec.read().unwrap();
    /// let reader: Cursor<Vec<u8>> = dec.into_inner();
    /// assert_eq!(reader.position(), 1); // one byte consumed for u64=42
    /// ```
    #[must_use]
    pub fn into_inner(self) -> R {
        self.reader
    }

    /// Decode the next value from the underlying reader.
    ///
    /// # Errors
    ///
    /// - Propagates any [`crate::SerialError`] from the type's
    ///   [`Deserialize`].
    /// - Maps any `std::io::Error` from the reader into [`SerialError::Io`].
    #[inline]
    pub fn read<T: Deserialize>(&mut self) -> Result<T> {
        T::deserialize(self)
    }
}

impl<R: Read> Decode for IoDecoder<R> {
    fn read_byte(&mut self) -> Result<u8> {
        let mut buf = [0u8; 1];
        self.read_into(&mut buf)?;
        Ok(buf[0])
    }

    fn read_into(&mut self, out: &mut [u8]) -> Result<()> {
        self.reader.read_exact(out).map_err(|e| {
            if e.kind() == std::io::ErrorKind::UnexpectedEof {
                SerialError::UnexpectedEof {
                    needed: out.len(),
                    remaining: 0,
                }
            } else {
                map_io_error(e)
            }
        })
    }

    fn max_alloc(&self) -> usize {
        self.config.max_alloc
    }
}

/// Encode `value` and write the result into `writer` in a single call.
///
/// # Errors
///
/// - Propagates any [`crate::SerialError`] from the type's [`Serialize`].
/// - Maps any `std::io::Error` from the writer into [`SerialError::Io`].
///
/// # Examples
///
/// ```
/// use pack_io::encode_into;
///
/// let mut buf: Vec<u8> = Vec::new();
/// encode_into(&(7_u64, "hello"), &mut buf).unwrap();
/// assert!(!buf.is_empty());
/// ```
#[inline]
pub fn encode_into<T, W>(value: &T, writer: &mut W) -> Result<()>
where
    T: Serialize + ?Sized,
    W: Write,
{
    let mut enc = IoEncoder::new(writer);
    enc.write(value)
}

/// Read all remaining bytes from `reader` and decode them as a single value
/// of type `T`.
///
/// Use this for whole-buffer reads (a length-prefixed message you have
/// already extracted from the transport, a small config file, …). For
/// length-framed protocols where the producer wrote one value and then more
/// bytes for something else, prefer [`IoDecoder`] directly.
///
/// # Errors
///
/// - Returns [`SerialError::TrailingBytes`] if the reader yielded extra
///   bytes after the value was decoded.
/// - Propagates any [`crate::SerialError`] from the type's [`Deserialize`].
/// - Maps any `std::io::Error` from the reader into [`SerialError::Io`].
///
/// # Examples
///
/// ```
/// use pack_io::{encode, decode_from};
/// use std::io::Cursor;
///
/// let bytes = encode(&42_u64).unwrap();
/// let n: u64 = decode_from(&mut Cursor::new(bytes)).unwrap();
/// assert_eq!(n, 42);
/// ```
pub fn decode_from<T, R>(reader: &mut R) -> Result<T>
where
    T: Deserialize,
    R: Read,
{
    let mut buf = alloc::vec::Vec::new();
    let _ = reader.read_to_end(&mut buf).map_err(map_io_error)?;
    crate::decode(&buf)
}

/// Map a `std::io::Error` into [`SerialError::Io`].
#[inline]
fn map_io_error(err: std::io::Error) -> SerialError {
    use alloc::string::ToString;
    SerialError::Io {
        kind: err.kind(),
        message: err.to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::encode;
    use alloc::vec::Vec;
    use std::io::Cursor;

    #[test]
    fn io_encoder_decoder_round_trip() {
        let mut buf: Vec<u8> = Vec::new();
        {
            let mut enc = IoEncoder::new(&mut buf);
            enc.write(&42_u64).unwrap();
            enc.write(&"hello").unwrap();
            enc.write(&true).unwrap();
        }
        let mut dec = IoDecoder::new(Cursor::new(buf));
        let n: u64 = dec.read().unwrap();
        let s: String = dec.read().unwrap();
        let b: bool = dec.read().unwrap();
        assert_eq!((n, s.as_str(), b), (42, "hello", true));
    }

    #[test]
    fn encode_into_writes_same_bytes_as_encode() {
        let value = (1u32, String::from("hi"), -2i32);
        let from_fn = encode(&value).unwrap();
        let mut from_io: Vec<u8> = Vec::new();
        encode_into(&value, &mut from_io).unwrap();
        assert_eq!(from_fn, from_io);
    }

    #[test]
    fn decode_from_reads_same_value_as_decode() {
        let bytes = encode(&(7u64, true)).unwrap();
        let value: (u64, bool) = decode_from(&mut Cursor::new(bytes)).unwrap();
        assert_eq!(value, (7, true));
    }

    #[test]
    fn io_decoder_with_zero_cap_is_rejected() {
        let cfg = Config::new().with_max_alloc(0);
        let bytes: Vec<u8> = Vec::new();
        let err = IoDecoder::with_config(Cursor::new(bytes), cfg).expect_err("zero cap");
        assert!(matches!(err, SerialError::InvalidLength { .. }));
    }

    #[test]
    fn io_decoder_short_read_surfaces_unexpected_eof() {
        // Two-byte varint that says "more coming" but there's nothing after.
        let bytes = alloc::vec![0x80];
        let mut dec = IoDecoder::new(Cursor::new(bytes));
        let err = dec.read::<u64>().expect_err("truncated");
        assert!(matches!(err, SerialError::UnexpectedEof { .. }));
    }
}