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
//! Thin wrappers around `bincode`'s free (de)serialization functions that avoid
//! [`bincode::serialize`]'s built-in serialized-size pre-pass.
//!
//! `bincode::serialize` (and `Options::serialize`, which it's built on) always calls
//! `serialized_size` — a full `Serialize::serialize` pass against a `SizeChecker` — before encoding
//! for real into a correctly pre-sized `Vec` (see `bincode`'s internal `serialize` function). For
//! most `#[derive(Serialize)]` types that's cheap: both passes just walk fields, and the size pass
//! does no real work. But `Serialize` impls that build their output eagerly regardless of which
//! serializer is asking — most notably `HeapArray`'s (the [`InPlaceCodec`] fast path), whose
//! `serialize` calls `self.to_inplace_bytes()` unconditionally — end up doing that work twice: once
//! to be measured and thrown away by the `SizeChecker` pass, once for the real encode.
//!
//! [`serialize`] encodes exactly once, writing directly into a growable `Vec` via
//! `bincode::serialize_into` (the same fix already used by `network`'s `IoSink::send`).
//! [`deserialize`] is a plain passthrough to `bincode::deserialize` — bincode's decode path has no
//! equivalent double pass — kept alongside [`serialize`] so callers use one consistent
//! (de)serialization entry point instead of mixing direct `bincode::*` calls with this module.
//!
//! [`InPlaceCodec`]: super::InPlaceCodec
use ;
/// Serialize `value` with `bincode`, without `bincode::serialize`'s serialized-size pre-pass.
Sized + Serialize>
/// Deserialize a `T` with `bincode`. A thin passthrough to `bincode::deserialize`, kept alongside
/// [`serialize`] so callers use one consistent (de)serialization entry point.