rapace_core/encoding.rs
1//! Encoding and decoding context traits.
2//!
3//! These traits define the transport-specific encoding interface.
4//! Actual facet-based serialization happens at a higher level (in codegen or rapace-codec).
5
6use crate::{DecodeError, EncodeError, Frame};
7
8/// Context for encoding values into frames.
9///
10/// Each transport provides its own implementation that knows how to
11/// best represent data for that transport.
12///
13/// Note: Type-aware encoding (via facet) is handled by the RPC layer,
14/// not directly by EncodeCtx. This trait handles raw byte encoding.
15pub trait EncodeCtx: Send {
16 /// Encode raw bytes into the frame.
17 ///
18 /// For SHM: checks if bytes are already in SHM and references them zero-copy.
19 /// For stream: copies into the output buffer.
20 fn encode_bytes(&mut self, bytes: &[u8]) -> Result<(), EncodeError>;
21
22 /// Finish encoding and return the frame.
23 fn finish(self: Box<Self>) -> Result<Frame, EncodeError>;
24}
25
26/// Context for decoding frames into values.
27///
28/// Note: Type-aware decoding (via facet) is handled by the RPC layer,
29/// not directly by DecodeCtx. This trait handles raw byte access.
30pub trait DecodeCtx<'a>: Send {
31 /// Borrow raw bytes from the frame.
32 ///
33 /// Lifetime is tied to the frame — caller must copy if needed longer.
34 fn decode_bytes(&mut self) -> Result<&'a [u8], DecodeError>;
35
36 /// Get remaining bytes without consuming them.
37 fn remaining(&self) -> &'a [u8];
38}