karyon_net/codec/mod.rs
1//! Message encoding and decoding.
2//!
3//! The codec trait is generic over the wire type (`W`):
4//! - `ByteBuffer` for byte-stream transports (TCP, TLS, Unix)
5//! - `ws::Message` for WebSocket
6//!
7//! Built-in codecs:
8//! - `LengthCodec` - length-prefixed (u32) framing for byte streams
9//! - `BytesCodec` - raw bytes with configurable max size
10
11mod bytes_codec;
12mod length_codec;
13
14use std::result;
15
16pub use bytes_codec::BytesCodec;
17pub use length_codec::LengthCodec;
18
19/// Encodes and decodes messages from a wire-level type.
20///
21/// `W` is the wire type (e.g. `ByteBuffer`, `ws::Message`).
22/// `Message` is the application-level type.
23pub trait Codec<W>: Send + Sync {
24 type Message: Send + Sync;
25 type Error;
26
27 /// Encode a message into the wire buffer.
28 fn encode(&self, src: &Self::Message, dst: &mut W) -> result::Result<usize, Self::Error>;
29
30 /// Decode a message from the wire buffer.
31 /// Returns `Some((bytes_consumed, message))` on success,
32 /// `None` if more data is needed.
33 fn decode(&self, src: &mut W) -> result::Result<Option<(usize, Self::Message)>, Self::Error>;
34}