read_until_slice

Crates.ioread_until_slice
lib.rsread_until_slice
version0.1.7
sourcesrc
created_at2024-02-18 15:03:39.685306
updated_at2024-09-22 17:55:14.421543
descriptionAsyncBufRead::read_until extension to take a slice as a delimiter instead of a single u8.
homepage
repositoryhttps://github.com/programingjd/read_until_slice
max_upload_size
id1144129
size26,850
DAVID, Jerome (programingjd)

documentation

README

read_until_slice  LICENSE crates.io Version Documentation

The tokio io-util feature provides the method:

pub async fn read_until(&mut self, delimiter: u8, buf: Vec<u8>) -> Result<usize>

on impl AsyncBufRead + Unpin.

This reads from an async buffered reader until either EOF or the delimiter is reached.

While useful, it is limited to a single byte delimiter.

This crate extends this by taking a slice as a delimiter instead of a single byte.

pub async fn read_until_slice(&mut self, delimiter: &[u8], buf: Vec<u8>) -> Result<usize>

on the same impl AsyncBufRead + Unpin.

Example

// Open socket
let stream = TcpStream::connect(addr)
    .await
    .expect("could not connect to remote address");
// Split stream into reader and writer halves
let (reader, mut writer) = split(stream);
// Buffer read stream
let mut reader = BufReader::new(reader);
...
// Read until new line delimiter into buffer
let mut buffer = vec![];
let delimiter = b"\r\n";
let n = reader.read_until(delimiter, &mut buffer)
    .await
    .expect("could not read from socket");
assert_eq!(n, buffer.len());
if buffer.ends_with(delimiter) {
    println!("end of line delimiter reached");
} else {
    println!("end of stream reached");
}
Commit count: 16

cargo fmt