pub struct FrameCodec<F> { /* private fields */ }bytes only.Expand description
A Framer driven over a BytesMut buffer.
§Example
use bytes::BytesMut;
use wire_codec::codec::FrameCodec;
use wire_codec::framing::VarintPrefixed;
let mut codec = FrameCodec::new(VarintPrefixed::new());
let mut wire = BytesMut::new();
codec.encode_frame(b"hello", &mut wire).unwrap();
codec.encode_frame(b"world", &mut wire).unwrap();
assert_eq!(&codec.decode_frame(&mut wire).unwrap().unwrap()[..], b"hello");
assert_eq!(&codec.decode_frame(&mut wire).unwrap().unwrap()[..], b"world");
assert_eq!(codec.decode_frame(&mut wire).unwrap(), None);
assert!(wire.is_empty());Implementations§
Source§impl<F: Framer> FrameCodec<F>
impl<F: Framer> FrameCodec<F>
Sourcepub fn into_inner(self) -> F
pub fn into_inner(self) -> F
Consume the codec and return the wrapped framer.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Forget the partial-scan progress kept between
FrameCodec::decode_frame calls.
The codec assumes the receive buffer only grows at the back between
calls, apart from the frames it splits off itself. Call reset when
you clear, truncate, or replace the buffer (for example to
resynchronise, after Framed::from_parts with a fresh read buffer, or
when reusing the codec for a new connection). Progress is also cleared
automatically whenever decoding returns an error.
§Example
use bytes::BytesMut;
use wire_codec::codec::FrameCodec;
use wire_codec::framing::Delimited;
let mut codec = FrameCodec::new(Delimited::bounded(b"\n").unwrap());
let mut src = BytesMut::from(&b"partial line"[..]);
assert_eq!(codec.decode_frame(&mut src).unwrap(), None);
// Switch to a different stream: drop the buffer and the progress.
src.clear();
codec.reset();
src.extend_from_slice(b"a\nb\n");
assert_eq!(&codec.decode_frame(&mut src).unwrap().unwrap()[..], b"a");Sourcepub fn decode_frame(&mut self, src: &mut BytesMut) -> Result<Option<BytesMut>>
pub fn decode_frame(&mut self, src: &mut BytesMut) -> Result<Option<BytesMut>>
Remove the next complete frame from the front of src and return its
payload, or Ok(None) if src does not hold a full frame yet.
§Errors
Propagates the framer’s errors. Returns Error::InvalidEncoding if
a custom framer violates the Frame
contract (zero bytes consumed, or more than the input). src is left
untouched on error, and the scan progress is cleared so the caller
may discard the buffer and resynchronise.
Sourcepub fn decode_frame_eof(
&mut self,
src: &mut BytesMut,
) -> Result<Option<BytesMut>>
pub fn decode_frame_eof( &mut self, src: &mut BytesMut, ) -> Result<Option<BytesMut>>
Like FrameCodec::decode_frame, for the end of the stream: once no
more input will arrive, leftover bytes that do not form a frame are an
error.
§Errors
Everything FrameCodec::decode_frame returns, plus
Error::UnexpectedEof when src holds a partial frame.
Sourcepub fn encode_frame(&mut self, payload: &[u8], dst: &mut BytesMut) -> Result<()>
pub fn encode_frame(&mut self, payload: &[u8], dst: &mut BytesMut) -> Result<()>
Append payload as one frame to dst, growing it as needed.
Space is reserved in one step using Framer::max_frame_len. For a
custom framer that does not implement it, the codec retries with a
doubling scratch region, up to four times the payload size plus
4 KiB.
§Errors
Propagates the framer’s errors, or returns Error::BufferFull if a
custom framer needs more room than the retry limit. dst is left
untouched on error.
Trait Implementations§
Source§impl<F: Clone> Clone for FrameCodec<F>
impl<F: Clone> Clone for FrameCodec<F>
Source§impl<F: Debug> Debug for FrameCodec<F>
impl<F: Debug> Debug for FrameCodec<F>
Source§impl<F: Framer> Decoder for FrameCodec<F>
Available on crate feature tokio-util only.
impl<F: Framer> Decoder for FrameCodec<F>
tokio-util only.Source§fn decode(&mut self, src: &mut BytesMut) -> Result<Option<BytesMut>>
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<BytesMut>>
Source§impl<F: Framer> Decoder for FrameCodec<F>
Available on crate feature asynchronous-codec only.
impl<F: Framer> Decoder for FrameCodec<F>
asynchronous-codec only.Source§impl<F: Default> Default for FrameCodec<F>
impl<F: Default> Default for FrameCodec<F>
Source§impl<F: Framer> Encoder for FrameCodec<F>
Available on crate feature asynchronous-codec only.
impl<F: Framer> Encoder for FrameCodec<F>
asynchronous-codec only.Source§impl<F: Framer> Encoder<BytesMut> for FrameCodec<F>
Available on crate feature tokio-util only.
impl<F: Framer> Encoder<BytesMut> for FrameCodec<F>
tokio-util only.