pack-io 2.1.0

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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! `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,
    depth: usize,
    /// Bytes consumed from `reader` so far (see `Decode::bytes_read`).
    read: u64,
    /// Running `Decode::unbacked` total for the value being decoded.
    unbacked: usize,
}

impl<R: Read> IoDecoder<R> {
    /// Wrap `reader` with the default [`Config`] (1 GiB `max_alloc`,
    /// depth limit 128).
    ///
    /// For input from a peer, use [`IoDecoder::with_config`] with
    /// [`Config::network`] (or tighter) instead. Whatever the cap, the decoder
    /// only grows a buffer as bytes actually arrive from `reader`, so a
    /// large declared length on a short or stalled stream does not reserve
    /// memory up front.
    ///
    /// # 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::new(),
            depth: 0,
            read: 0,
            unbacked: 0,
        }
    }

    /// Wrap `reader` with the supplied configuration.
    ///
    /// # Errors
    ///
    /// - [`SerialError::InvalidConfig`] if `config.max_alloc == 0`,
    ///   `config.max_depth == 0`, or
    ///   `config.canonical` is set: checking canonical map order needs the
    ///   raw input, which a streaming decoder does not keep. Read the frame
    ///   into memory and use [`crate::decode_with_config`] (or
    ///   [`decode_from_with_config`]) instead.
    ///
    /// # 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> {
        let config = config.validate()?;
        if config.canonical {
            return Err(SerialError::InvalidConfig {
                reason: "canonical mode is not supported by IoDecoder; buffer the frame and use decode_with_config",
            });
        }
        Ok(Self {
            reader,
            config,
            depth: 0,
            read: 0,
            unbacked: 0,
        })
    }

    /// The configuration this decoder enforces.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use pack_io::{Config, IoDecoder};
    ///
    /// let cfg = Config::new().with_max_alloc(4096);
    /// let dec = IoDecoder::with_config(Cursor::new(Vec::new()), cfg).unwrap();
    /// assert_eq!(dec.config().max_alloc, 4096);
    /// ```
    #[must_use]
    pub fn config(&self) -> Config {
        self.config
    }

    /// 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.
    ///
    /// Each top-level call starts a fresh [`Config::total_alloc_limit`]
    /// budget, so a decoder that reads one message after another from a
    /// socket gives every message the same limit.
    ///
    /// # 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> {
        if self.depth == 0 {
            self.unbacked = 0;
        }
        T::deserialize(self)
    }
}

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

    #[inline]
    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)
            }
        })?;
        self.read = self.read.wrapping_add(out.len() as u64);
        Ok(())
    }

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

    fn config(&self) -> Config {
        self.config
    }

    fn depth(&self) -> usize {
        self.depth
    }

    fn enter_nested(&mut self) -> Result<()> {
        if self.depth >= self.config.max_depth {
            return Err(SerialError::DepthLimitExceeded {
                max_depth: self.config.max_depth,
            });
        }
        self.depth += 1;
        Ok(())
    }

    fn leave_nested(&mut self) {
        self.depth = self.depth.saturating_sub(1);
    }

    #[inline]
    fn bytes_read(&self) -> Option<u64> {
        Some(self.read)
    }

    #[inline]
    fn unbacked(&self) -> usize {
        self.unbacked
    }

    fn charge_unbacked(&mut self, bytes: usize) -> Result<()> {
        let total = self.unbacked.saturating_add(bytes);
        if total > self.config.total_alloc_limit() {
            return Err(crate::codec::unbacked_limit_error(total, None));
        }
        self.unbacked = total;
        Ok(())
    }

    #[inline]
    fn reset_unbacked(&mut self) {
        self.unbacked = 0;
    }
}

/// 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`, under the default [`Config`].
///
/// 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.
///
/// The reader is drained into memory first, but never past
/// [`Config::max_alloc`] bytes: an endless or oversized stream is cut off
/// and rejected instead of buffered without limit. Use
/// [`decode_from_with_config`] to set a tighter cap.
///
/// # Errors
///
/// - [`SerialError::InvalidLength`] if the reader yields more than
///   `max_alloc` bytes (`declared` reports how many were read before the
///   cut-off).
/// - [`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,
{
    decode_from_with_config(reader, Config::new())
}

/// [`decode_from`] under an explicit [`Config`].
///
/// At most `config.max_alloc` bytes are read from `reader` (one more is
/// requested to detect an oversized stream); the buffered frame is then
/// decoded with an in-memory [`crate::Decoder`], so every `Config` knob —
/// including canonical mode — applies.
///
/// # Errors
///
/// - Any error [`crate::Decoder::with_config`] reports for an invalid
///   `config`.
/// - [`SerialError::InvalidLength`] if the reader yields more than
///   `config.max_alloc` bytes.
/// - [`SerialError::TrailingBytes`], decode errors and I/O errors as for
///   [`decode_from`].
///
/// # Examples
///
/// ```
/// use std::io::Cursor;
/// use pack_io::{Config, SerialError, decode_from_with_config};
///
/// // A 1 MiB stream against a 4 KiB cap is cut off after 4 KiB + 1 bytes.
/// let mut endless = Cursor::new(vec![0u8; 1 << 20]);
/// let cfg = Config::new().with_max_alloc(4096);
/// let err = decode_from_with_config::<u8, _>(&mut endless, cfg).unwrap_err();
/// assert!(matches!(err, SerialError::InvalidLength { declared: 4097, .. }));
/// ```
pub fn decode_from_with_config<T, R>(reader: &mut R, config: Config) -> Result<T>
where
    T: Deserialize,
    R: Read,
{
    let config = config.validate()?;
    let cap = config.max_alloc as u64;
    let mut buf = alloc::vec::Vec::new();
    let _ = reader
        .take(cap.saturating_add(1))
        .read_to_end(&mut buf)
        .map_err(map_io_error)?;
    if buf.len() as u64 > cap {
        return Err(SerialError::InvalidLength {
            declared: buf.len() as u64,
            remaining: buf.len(),
        });
    }
    crate::decode_with_config(&buf, config)
}

/// 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::InvalidConfig { .. }));
    }

    #[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 { .. }));
    }
}