Struct AnyDelimiterCodec

pub struct AnyDelimiterCodec { /* private fields */ }

A simple Decoder and Encoder implementation that splits up data into chunks based on any character in the given delimiter string.

Example

Decode string of bytes containing various different delimiters.

use tokio_util::codec::{AnyDelimiterCodec, Decoder};
use bytes::{BufMut, BytesMut};

#
# #[tokio::main(flavor = "current_thread")]
# async fn main() -> Result<(), std::io::Error> {
let mut codec = AnyDelimiterCodec::new(b",;\r\n".to_vec(),b";".to_vec());
let buf = &mut BytesMut::new();
buf.reserve(200);
buf.put_slice(b"chunk 1,chunk 2;chunk 3\n\r");
assert_eq!("chunk 1", codec.decode(buf).unwrap().unwrap());
assert_eq!("chunk 2", codec.decode(buf).unwrap().unwrap());
assert_eq!("chunk 3", codec.decode(buf).unwrap().unwrap());
assert_eq!("", codec.decode(buf).unwrap().unwrap());
assert_eq!(None, codec.decode(buf).unwrap());
# Ok(())
# }

Implementations

impl AnyDelimiterCodec

fn new(seek_delimiters: Vec<u8>, sequence_writer: Vec<u8>) -> AnyDelimiterCodec

Returns a AnyDelimiterCodec for splitting up data into chunks.

Note

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

fn new_with_max_length(seek_delimiters: Vec<u8>, sequence_writer: Vec<u8>, max_length: usize) -> Self

Returns a AnyDelimiterCodec with a maximum chunk length limit.

If this is set, calls to AnyDelimiterCodec::decode will return a AnyDelimiterCodecError when a chunk exceeds the length limit. Subsequent calls will discard up to limit bytes from that chunk until a delimiter character is reached, returning None until the delimiter 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 AnyDelimiterCodec which will be exposed to untrusted input. Otherwise, the size of the buffer that holds the chunk currently being read is unbounded. An attacker could exploit this unbounded buffer by sending an unbounded amount of input without any delimiter characters, causing unbounded memory consumption.

fn max_length(&self) -> usize

Returns the maximum chunk length when decoding.

use std::usize;
use tokio_util::codec::AnyDelimiterCodec;

let codec = AnyDelimiterCodec::new(b",;\n".to_vec(), b";".to_vec());
assert_eq!(codec.max_length(), usize::MAX);
use tokio_util::codec::AnyDelimiterCodec;

let codec = AnyDelimiterCodec::new_with_max_length(b",;\n".to_vec(), b";".to_vec(), 256);
assert_eq!(codec.max_length(), 256);

Trait Implementations

impl Clone for AnyDelimiterCodec

fn clone(&self) -> AnyDelimiterCodec

impl Debug for AnyDelimiterCodec

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

impl Decoder for AnyDelimiterCodec

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

impl Default for AnyDelimiterCodec

fn default() -> Self

impl Eq for AnyDelimiterCodec

impl Hash for AnyDelimiterCodec

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

impl Ord for AnyDelimiterCodec

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

impl PartialEq for AnyDelimiterCodec

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

impl PartialOrd for AnyDelimiterCodec

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

impl StructuralPartialEq for AnyDelimiterCodec

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

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

Auto Trait Implementations

impl Freeze for AnyDelimiterCodec

impl RefUnwindSafe for AnyDelimiterCodec

impl Send for AnyDelimiterCodec

impl Sync for AnyDelimiterCodec

impl Unpin for AnyDelimiterCodec

impl UnsafeUnpin for AnyDelimiterCodec

impl UnwindSafe for AnyDelimiterCodec

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> From<T> for AnyDelimiterCodec

fn from(t: T) -> T

Returns the argument unchanged.

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

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

impl<T, U> Into<U> for AnyDelimiterCodec 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 AnyDelimiterCodec 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 AnyDelimiterCodec where U: TryFrom<T>,

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