pub struct Builder { /* private fields */ }
codec
only.Expand description
Configure length delimited LengthDelimitedCodec
s.
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§
Source§impl Builder
impl Builder
Sourcepub fn new() -> Builder
pub fn new() -> Builder
Creates a new length delimited codec builder with default configuration values.
§Examples
use tokio_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.length_field_offset(0)
.length_field_type::<u16>()
.length_adjustment(0)
.num_skip(0)
.new_read(io);
Sourcepub fn big_endian(&mut self) -> &mut Self
pub fn big_endian(&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_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.big_endian()
.new_read(io);
Sourcepub fn little_endian(&mut self) -> &mut Self
pub fn little_endian(&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_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.little_endian()
.new_read(io);
Sourcepub fn native_endian(&mut self) -> &mut Self
pub fn native_endian(&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_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.native_endian()
.new_read(io);
Sourcepub fn max_frame_length(&mut self, val: usize) -> &mut Self
pub fn max_frame_length(&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_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.max_frame_length(8 * 1024 * 1024)
.new_read(io);
Sourcepub fn length_field_type<T: LengthFieldType>(&mut self) -> &mut Self
pub fn length_field_type<T: LengthFieldType>(&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_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.length_field_type::<u32>()
.new_read(io);
Unlike Builder::length_field_length
, this does not fail at runtime
and instead produces a compile error:
LengthDelimitedCodec::builder()
.length_field_type::<u128>()
.new_read(io);
Sourcepub fn length_field_length(&mut self, val: usize) -> &mut Self
pub fn length_field_length(&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_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.length_field_length(4)
.new_read(io);
Sourcepub fn length_field_offset(&mut self, val: usize) -> &mut Self
pub fn length_field_offset(&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_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.length_field_offset(1)
.new_read(io);
Sourcepub fn length_adjustment(&mut self, val: isize) -> &mut Self
pub fn length_adjustment(&mut self, val: isize) -> &mut Self
Delta between the payload length specified in the header and the real payload length
§Examples
use tokio_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.length_adjustment(-2)
.new_read(io);
Sourcepub fn num_skip(&mut self, val: usize) -> &mut Self
pub fn num_skip(&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_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.num_skip(4)
.new_read(io);
Sourcepub fn new_codec(&self) -> LengthDelimitedCodec
pub fn new_codec(&self) -> LengthDelimitedCodec
Create a configured length delimited LengthDelimitedCodec
§Examples
use tokio_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.length_field_offset(0)
.length_field_type::<u16>()
.length_adjustment(0)
.num_skip(0)
.new_codec();
Sourcepub fn new_read<T>(&self, upstream: T) -> FramedRead<T, LengthDelimitedCodec>where
T: AsyncRead,
pub fn new_read<T>(&self, upstream: T) -> FramedRead<T, LengthDelimitedCodec>where
T: AsyncRead,
Create a configured length delimited FramedRead
§Examples
use tokio_util::codec::LengthDelimitedCodec;
LengthDelimitedCodec::builder()
.length_field_offset(0)
.length_field_type::<u16>()
.length_adjustment(0)
.num_skip(0)
.new_read(io);
Sourcepub fn new_write<T>(&self, inner: T) -> FramedWrite<T, LengthDelimitedCodec>where
T: AsyncWrite,
pub fn new_write<T>(&self, inner: T) -> FramedWrite<T, LengthDelimitedCodec>where
T: AsyncWrite,
Create a configured length delimited FramedWrite
§Examples
LengthDelimitedCodec::builder()
.length_field_type::<u16>()
.new_write(io);
Sourcepub fn new_framed<T>(&self, inner: T) -> Framed<T, LengthDelimitedCodec>where
T: AsyncRead + AsyncWrite,
pub fn new_framed<T>(&self, inner: T) -> Framed<T, LengthDelimitedCodec>where
T: AsyncRead + AsyncWrite,
Create a configured length delimited Framed
§Examples
LengthDelimitedCodec::builder()
.length_field_type::<u16>()
.new_framed(io);
Trait Implementations§
impl Copy for Builder
Auto Trait Implementations§
impl Freeze for Builder
impl RefUnwindSafe for Builder
impl Send for Builder
impl Sync for Builder
impl Unpin for Builder
impl UnwindSafe for Builder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
Source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
Source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 56 bytes