Struct LinesCodec

struct LinesCodec { ... }

A simple Decoder and Encoder implementation that splits up data into lines.

This uses the \n character as the line ending on all platforms.

Implementations

impl LinesCodec

fn new() -> LinesCodec

Returns a LinesCodec for splitting up data into lines.

Note

The returned LinesCodec will not have an upper bound on the length of a buffered line. See the documentation for new_with_max_length for information on why this could be a potential security risk.

fn new_with_max_length(max_length: usize) -> Self

Returns a LinesCodec with a maximum line length limit.

If this is set, calls to LinesCodec::decode will return a LinesCodecError when a line exceeds the length limit. Subsequent calls will discard up to limit bytes from that line until a newline character is reached, returning None until the line over the limit has been fully discarded. After that point, calls to decode will function as normal.

Note

Setting a length limit is highly recommended for any LinesCodec which will be exposed to untrusted input. Otherwise, the size of the buffer that holds the line currently being read is unbounded. An attacker could exploit this unbounded buffer by sending an unbounded amount of input without any \n characters, causing unbounded memory consumption.

fn max_length(self: &Self) -> usize

Returns the maximum line length when decoding.

use std::usize;
use tokio_util::codec::LinesCodec;

let codec = LinesCodec::new();
assert_eq!(codec.max_length(), usize::MAX);
use tokio_util::codec::LinesCodec;

let codec = LinesCodec::new_with_max_length(256);
assert_eq!(codec.max_length(), 256);

impl Clone for LinesCodec

fn clone(self: &Self) -> LinesCodec

impl Debug for LinesCodec

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

impl Decoder for LinesCodec

fn decode(self: &mut Self, buf: &mut BytesMut) -> Result<Option<String>, LinesCodecError>
fn decode_eof(self: &mut Self, buf: &mut BytesMut) -> Result<Option<String>, LinesCodecError>

impl Default for LinesCodec

fn default() -> Self

impl Eq for LinesCodec

impl Freeze for LinesCodec

impl Hash for LinesCodec

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

impl Ord for LinesCodec

fn cmp(self: &Self, other: &LinesCodec) -> Ordering

impl PartialEq for LinesCodec

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

impl PartialOrd for LinesCodec

fn partial_cmp(self: &Self, other: &LinesCodec) -> Option<Ordering>

impl RefUnwindSafe for LinesCodec

impl Send for LinesCodec

impl StructuralPartialEq for LinesCodec

impl Sync for LinesCodec

impl Unpin for LinesCodec

impl UnsafeUnpin for LinesCodec

impl UnwindSafe for LinesCodec

impl<T> Any for LinesCodec

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for LinesCodec

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

impl<T> BorrowMut for LinesCodec

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

impl<T> CloneToUninit for LinesCodec

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

impl<T> Encoder for LinesCodec

fn encode(self: &mut Self, line: T, buf: &mut BytesMut) -> Result<(), LinesCodecError>

impl<T> From for LinesCodec

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for LinesCodec

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

impl<T, U> Into for LinesCodec

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 LinesCodec

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

impl<T, U> TryInto for LinesCodec

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