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
//! The [`Encode`] trait: serialising typed messages into raw bytes.
//!
//! `Encode` is the sink-side counterpart to [`crate::Decoder`]. Where a
//! decoder converts raw bytes from a [`crate::Source`] into a typed
//! [`super::Message`], an encoder converts a typed message into bytes
//! suitable for storage or transmission.
//!
//! ## Who implements this?
//!
//! The supplier's concrete message type. The framework never implements
//! `Encode` on behalf of user types; it only calls it from sinks that
//! need to persist or forward messages as bytes (e.g. the shared-memory
//! sink, a file sink).
//!
//! ## Symmetry with `Decoder`
//!
//! For a message type that also has a paired `Decoder`, and for types
//! that implement `PartialEq`:
//!
//! ```text
//! decoder.decode(encode(m)) == Ok(Some(m))
//! ```
//!
//! This round-trip property is the recommended test for both impls.
//!
//! ## Buffer sizing
//!
//! Callers must not mutate the message between `encoded_len` and
//! `encode_into`. Undersized buffers must return
//! [`crate::ErrorKind::Encode`].
use Result;
/// Serialises a typed message into a raw byte buffer.
///
/// Implementations must be deterministic: the same message must always
/// produce the same byte sequence. Not every [`super::Message`] needs
/// `Encode`; sinks that write bytes require both bounds.