Struct AnyDelimiterCodec

struct AnyDelimiterCodec { ... }

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

impl Clone for AnyDelimiterCodec

fn clone(self: &Self) -> AnyDelimiterCodec

impl Debug for AnyDelimiterCodec

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

impl Decoder for AnyDelimiterCodec

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

impl Default for AnyDelimiterCodec

fn default() -> Self

impl Eq for AnyDelimiterCodec

impl Freeze for AnyDelimiterCodec

impl Hash for AnyDelimiterCodec

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

impl Ord for AnyDelimiterCodec

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

impl PartialEq for AnyDelimiterCodec

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

impl PartialOrd for AnyDelimiterCodec

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

impl RefUnwindSafe for AnyDelimiterCodec

impl Send for AnyDelimiterCodec

impl StructuralPartialEq for AnyDelimiterCodec

impl Sync for AnyDelimiterCodec

impl Unpin for AnyDelimiterCodec

impl UnsafeUnpin for AnyDelimiterCodec

impl UnwindSafe for AnyDelimiterCodec

impl<T> Any for AnyDelimiterCodec

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for AnyDelimiterCodec

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

impl<T> BorrowMut for AnyDelimiterCodec

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

impl<T> CloneToUninit for AnyDelimiterCodec

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

impl<T> Encoder for AnyDelimiterCodec

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

impl<T> From for AnyDelimiterCodec

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for AnyDelimiterCodec

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

impl<T, U> Into for AnyDelimiterCodec

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 AnyDelimiterCodec

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

impl<T, U> TryInto for AnyDelimiterCodec

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