Struct Builder

struct Builder { ... }

Configure length delimited LengthDelimitedCodecs.

Builder enables constructing configured length delimited codecs. Note that not all configuration settings apply to both encoding and decoding. See the documentation for specific methods for more detail.

Note that the if the value of Builder::max_frame_length becomes larger than what can actually fit in Builder::length_field_length, it will be clipped to the maximum value that can fit.

Implementations

impl Builder

fn new() -> Builder

Creates a new length delimited codec builder with default configuration values.

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .length_field_offset(0)
    .length_field_type::<u16>()
    .length_adjustment(0)
    .num_skip(0)
    .new_read(io);
# }
# pub fn main() {}
fn big_endian(self: &mut Self) -> &mut Self

Read the length field as a big endian integer

This is the default setting.

This configuration option applies to both encoding and decoding.

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .big_endian()
    .new_read(io);
# }
# pub fn main() {}
fn little_endian(self: &mut Self) -> &mut Self

Read the length field as a little endian integer

The default setting is big endian.

This configuration option applies to both encoding and decoding.

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .little_endian()
    .new_read(io);
# }
# pub fn main() {}
fn native_endian(self: &mut Self) -> &mut Self

Read the length field as a native endian integer

The default setting is big endian.

This configuration option applies to both encoding and decoding.

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .native_endian()
    .new_read(io);
# }
# pub fn main() {}
fn max_frame_length(self: &mut Self, val: usize) -> &mut Self

Sets the max frame length in bytes

This configuration option applies to both encoding and decoding. The default value is 8MB.

When decoding, the length field read from the byte stream is checked against this setting before any adjustments are applied. When encoding, the length of the submitted payload is checked against this setting.

When frames exceed the max length, an io::Error with the custom value of the LengthDelimitedCodecError type will be returned.

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .max_frame_length(8 * 1024 * 1024)
    .new_read(io);
# }
# pub fn main() {}
fn length_field_type<T: builder::LengthFieldType>(self: &mut Self) -> &mut Self

Sets the unsigned integer type used to represent the length field.

The default type is u32. The max type is u64 (or usize on 64-bit targets).

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .length_field_type::<u32>()
    .new_read(io);
# }
# pub fn main() {}

Unlike Builder::length_field_length, this does not fail at runtime and instead produces a compile error:

# use tokio::io::AsyncRead;
# use tokio_util::codec::LengthDelimitedCodec;
# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .length_field_type::<u128>()
    .new_read(io);
# }
# pub fn main() {}
fn length_field_length(self: &mut Self, val: usize) -> &mut Self

Sets the number of bytes used to represent the length field

The default value is 4. The max value is 8.

This configuration option applies to both encoding and decoding.

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .length_field_length(4)
    .new_read(io);
# }
# pub fn main() {}
fn length_field_offset(self: &mut Self, val: usize) -> &mut Self

Sets the number of bytes in the header before the length field

This configuration option only applies to decoding.

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .length_field_offset(1)
    .new_read(io);
# }
# pub fn main() {}
fn length_adjustment(self: &mut Self, val: isize) -> &mut Self

Delta between the payload length specified in the header and the real payload length

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .length_adjustment(-2)
    .new_read(io);
# }
# pub fn main() {}
fn num_skip(self: &mut Self, val: usize) -> &mut Self

Sets the number of bytes to skip before reading the payload

Default value is length_field_len + length_field_offset

This configuration option only applies to decoding

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .num_skip(4)
    .new_read(io);
# }
# pub fn main() {}
fn new_codec(self: &Self) -> LengthDelimitedCodec

Create a configured length delimited LengthDelimitedCodec

Examples

use tokio_util::codec::LengthDelimitedCodec;
# pub fn main() {
LengthDelimitedCodec::builder()
    .length_field_offset(0)
    .length_field_type::<u16>()
    .length_adjustment(0)
    .num_skip(0)
    .new_codec();
# }
fn new_read<T>(self: &Self, upstream: T) -> FramedRead<T, LengthDelimitedCodec>
where
    T: AsyncRead

Create a configured length delimited FramedRead

Examples

# use tokio::io::AsyncRead;
use tokio_util::codec::LengthDelimitedCodec;

# fn bind_read<T: AsyncRead>(io: T) {
LengthDelimitedCodec::builder()
    .length_field_offset(0)
    .length_field_type::<u16>()
    .length_adjustment(0)
    .num_skip(0)
    .new_read(io);
# }
# pub fn main() {}
fn new_write<T>(self: &Self, inner: T) -> FramedWrite<T, LengthDelimitedCodec>
where
    T: AsyncWrite

Create a configured length delimited FramedWrite

Examples

# use tokio::io::AsyncWrite;
# use tokio_util::codec::LengthDelimitedCodec;
# fn write_frame<T: AsyncWrite>(io: T) {
LengthDelimitedCodec::builder()
    .length_field_type::<u16>()
    .new_write(io);
# }
# pub fn main() {}
fn new_framed<T>(self: &Self, inner: T) -> Framed<T, LengthDelimitedCodec>
where
    T: AsyncRead + AsyncWrite

Create a configured length delimited Framed

Examples

# use tokio::io::{AsyncRead, AsyncWrite};
# use tokio_util::codec::LengthDelimitedCodec;
# fn write_frame<T: AsyncRead + AsyncWrite>(io: T) {
# let _ =
LengthDelimitedCodec::builder()
    .length_field_type::<u16>()
    .new_framed(io);
# }
# pub fn main() {}

impl Clone for Builder

fn clone(self: &Self) -> Builder

impl Copy for Builder

impl Debug for Builder

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

impl Default for Builder

fn default() -> Self

impl Freeze for Builder

impl RefUnwindSafe for Builder

impl Send for Builder

impl Sync for Builder

impl Unpin for Builder

impl UnsafeUnpin for Builder

impl UnwindSafe for Builder

impl<T> Any for Builder

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Builder

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Builder

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

impl<T> CloneToUninit for Builder

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

impl<T> From for Builder

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Builder

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for Builder

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Builder

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Builder

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