read_until_pattern_async

Function read_until_pattern_async 

Source
pub async fn read_until_pattern_async<R: AsyncBufRead + Unpin>(
    reader: &mut R,
    pattern: &str,
    to: &mut Vec<u8>,
) -> Result<(Vec<u8>, usize)>
Expand description

Async version of read_until_pattern for streaming data.

This function provides the same functionality as read_until_pattern but works with async streams, making it perfect for processing network data, file streams, or any other async I/O operations.

§Arguments

  • reader - An AsyncBufRead stream to read from
  • pattern - A regex pattern string to search for
  • to - Buffer to store the read data

§Returns

Returns a tuple containing:

  • The matched substring as bytes
  • The total number of bytes read

§Examples

use async_regex::read_until_pattern_async;
use futures::io::Cursor;
use tokio::runtime::Runtime;
 
let rt = Runtime::new().unwrap();
rt.block_on(async {
    let mut reader = Cursor::new(b"hello world test pattern");
    let mut buffer = Vec::new();
 
    let (matched, size) = read_until_pattern_async(&mut reader, r"\w+", &mut buffer).await.unwrap();
    assert_eq!(matched, b"hello");
    assert_eq!(buffer, b"hello");
});