Struct LinesCodec

pub struct LinesCodec { /* private fields */ }

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

Trait Implementations

impl Clone for LinesCodec

fn clone(&self) -> LinesCodec

impl Debug for LinesCodec

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

impl Decoder for LinesCodec

type Item = String;
type Error = LinesCodecError;
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<String>, LinesCodecError>
fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<String>, LinesCodecError>

impl Default for LinesCodec

fn default() -> Self

impl Eq for LinesCodec

impl Hash for LinesCodec

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

impl Ord for LinesCodec

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

impl PartialEq for LinesCodec

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

impl PartialOrd for LinesCodec

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

impl StructuralPartialEq for LinesCodec

impl<T> Encoder<T> for LinesCodec where T: AsRef<str>,

type Error = LinesCodecError;
fn encode(&mut self, line: T, buf: &mut BytesMut) -> Result<(), LinesCodecError>

Auto Trait Implementations

impl Freeze for LinesCodec

impl RefUnwindSafe for LinesCodec

impl Send for LinesCodec

impl Sync for LinesCodec

impl Unpin for LinesCodec

impl UnsafeUnpin for LinesCodec

impl UnwindSafe for LinesCodec

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> From<T> for LinesCodec

fn from(t: T) -> T

Returns the argument unchanged.

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

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

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

fn into(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<U> for LinesCodec where U: Into<T>,

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

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

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