Skip to main content

FrameCodec

Struct FrameCodec 

Source
pub struct FrameCodec<F> { /* private fields */ }
Available on crate feature 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>

Source

pub const fn new(framer: F) -> Self

Wrap framer.

Source

pub const fn framer(&self) -> &F

Borrow the wrapped framer.

Source

pub fn into_inner(self) -> F

Consume the codec and return the wrapped framer.

Source

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");
Source

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.

Source

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.

Source

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>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug> Debug for FrameCodec<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: Framer> Decoder for FrameCodec<F>

Available on crate feature tokio-util only.
Source§

type Item = BytesMut

The type of decoded frames.
Source§

type Error = Error

The type of unrecoverable frame decoding errors. Read more
Source§

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<BytesMut>>

Attempts to decode a frame from the provided buffer of bytes. Read more
Source§

fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<BytesMut>>

A default method available to be called when there are no more bytes available to be read from the underlying I/O. Read more
Source§

fn framed<T>(self, io: T) -> Framed<T, Self>
where T: AsyncRead + AsyncWrite, Self: Sized,

Provides a Stream and Sink interface for reading and writing to this Io object, using Decode and Encode to read and write the raw data. Read more
Source§

impl<F: Framer> Decoder for FrameCodec<F>

Available on crate feature asynchronous-codec only.
Source§

type Item = BytesMut

The type of items returned by decode
Source§

type Error = Error

The type of decoding errors.
Source§

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<BytesMut>>

Decode an item from the src BytesMut into an item
Source§

fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<BytesMut>>

Called when the input stream reaches EOF, signaling a last attempt to decode Read more
Source§

impl<F: Default> Default for FrameCodec<F>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<F: Framer> Encoder for FrameCodec<F>

Available on crate feature asynchronous-codec only.
Source§

type Item<'a> = &'a [u8]

The type of items consumed by encode
Source§

type Error = Error

The type of encoding errors.
Source§

fn encode(&mut self, item: &[u8], dst: &mut BytesMut) -> Result<()>

Encodes an item into the BytesMut provided by dst.
Source§

impl<F: Framer> Encoder<&[u8]> for FrameCodec<F>

Available on crate feature tokio-util only.
Source§

type Error = Error

The type of encoding errors. Read more
Source§

fn encode(&mut self, item: &[u8], dst: &mut BytesMut) -> Result<()>

Encodes a frame into the buffer provided. Read more
Source§

impl<F: Framer> Encoder<Bytes> for FrameCodec<F>

Available on crate feature tokio-util only.
Source§

type Error = Error

The type of encoding errors. Read more
Source§

fn encode(&mut self, item: Bytes, dst: &mut BytesMut) -> Result<()>

Encodes a frame into the buffer provided. Read more
Source§

impl<F: Framer> Encoder<BytesMut> for FrameCodec<F>

Available on crate feature tokio-util only.
Source§

type Error = Error

The type of encoding errors. Read more
Source§

fn encode(&mut self, item: BytesMut, dst: &mut BytesMut) -> Result<()>

Encodes a frame into the buffer provided. Read more
Source§

impl<F: Eq> Eq for FrameCodec<F>

Source§

impl<F: Hash> Hash for FrameCodec<F>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<F: PartialEq> PartialEq for FrameCodec<F>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<F: PartialEq> StructuralPartialEq for FrameCodec<F>

Auto Trait Implementations§

§

impl<F> Forget for FrameCodec<F>

§

impl<F> Freeze for FrameCodec<F>

§

impl<F> Move for FrameCodec<F>

§

impl<F> RefUnwindSafe for FrameCodec<F>

§

impl<F> Send for FrameCodec<F>
where StreamDecoder<F>: Send,

§

impl<F> Sync for FrameCodec<F>
where StreamDecoder<F>: Sync,

§

impl<F> Unpin for FrameCodec<F>
where StreamDecoder<F>: Unpin,

§

impl<F> UnsafeUnpin for FrameCodec<F>

§

impl<F> UnwindSafe for FrameCodec<F>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.