compio_io/framed/codec/mod.rs
1//! Traits and implementations for encoding/decoding structured types to/from
2//! bytes.
3
4use std::io;
5
6#[cfg(feature = "codec-serde-json")]
7pub mod serde_json;
8
9/// Trait for types that encode values into bytes.
10pub trait Encoder<Item> {
11 /// The error type that can be returned during encoding operations.
12 type Error: From<io::Error>;
13
14 /// Encodes an item into bytes.
15 ///
16 /// The `buf` is *guaranteed* to have 0 initialized bytes (`len` == 0). At
17 /// the end, all initialized bytes will be treated as valid content to
18 /// be transimitted.
19 fn encode(&mut self, item: Item, buf: &mut Vec<u8>) -> Result<(), Self::Error>;
20}
21
22/// Trait for decoding byte sequences back into structured items.
23pub trait Decoder<Item> {
24 /// Errors happened during the decoding process
25 type Error: From<io::Error>;
26
27 /// Decodes a byte sequence into an item.
28 fn decode(&mut self, buf: &[u8]) -> Result<Item, Self::Error>;
29}