pub trait ReadUntilIndexFound: BufRead {
// Required method
fn read_until_index_found<'a, F>(
&'a mut self,
predicate_found_index: &'a mut F,
buf: &'a mut Vec<u8>,
) -> ReadChunkUntilIndexFoundFuture<'a, Self, F> ⓘ
where F: FnMut(&str) -> Option<usize>;
}Required Methods§
fn read_until_index_found<'a, F>( &'a mut self, predicate_found_index: &'a mut F, buf: &'a mut Vec<u8>, ) -> ReadChunkUntilIndexFoundFuture<'a, Self, F> ⓘ
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<T> ReadUntilIndexFound for BufReader<T>where
T: AsyncRead,
Allows reading from a async buffered byte stream.
Include all of async buffer utils methods by including the prelude:
impl<T> ReadUntilIndexFound for BufReader<T>where
T: AsyncRead,
Allows reading from a async buffered byte stream. Include all of async buffer utils methods by including the prelude:
use async_buf_reader_utils::prelude::*;Or you can include this given util self method individually:
use async_buf_reader_utils::read_until_index_found::*;Source§fn read_until_index_found<'a, F>(
&'a mut self,
predicate_found_index: &'a mut F,
buf: &'a mut Vec<u8>,
) -> ReadChunkUntilIndexFoundFuture<'a, Self, F> ⓘ
fn read_until_index_found<'a, F>( &'a mut self, predicate_found_index: &'a mut F, buf: &'a mut Vec<u8>, ) -> ReadChunkUntilIndexFoundFuture<'a, Self, F> ⓘ
Reads all bytes into buf until the result of calling the provided
predicate_found_index closure returns Some(found_index: i32).
If predicate_found_index returns None, predicate_found_index will
continue to be called repeatedly until Some(found_index: i32)
or EOF is reached.
This function will read bytes from the underlying stream until the
predicate_found_index is Some(found_index: i32) or EOF is found.
Once found, all bytes up to, and including, the delimiter (if found)
will be appended to buf.
If successful, this function will return the total number of bytes read.
#let _result: Result<(), String> = async_std::task::block_on(async {
use async_buf_reader_utils::prelude::*;
use async_std::{fs::File, io::BufReader};
// Path to file you want to read asynchronously
let str_path = "./data/file.txt";
// Async load file (you can optionally use method with_capacity to limit how
// much async-std BufReader bytes are loaded while reading which is useful in memory sensative environments)
let mut buf = BufReader::with_capacity(12, File::open(&str_path).await.expect("should have opened file"));
let mut comma_count: usize = 0;
let mut last_comma_idx = None;
let mut fillable = vec![];
// Our predicate closure is our business logic,
// where we are trying to capture all characters up to
// the 4th comma, remembering that a regex won't be as useful here
// because our predicate will be called for every loaded chunk
// of data returned by our async BufReader, so a desired match
// might stretch across two different strings provided to the predicate.
// Once we've reached the 4th comma, return the index via Some(index) and reset
// our counters, otherwise return None to indicate we are still searching.
let mut predicate = |str_data: &str|->Option<usize>{
// println!("in predicate {:?}", str_data);
last_comma_idx = str_data.bytes().enumerate().find_map(|(i, x)| {
if x == b','{
comma_count += 1;
}
if comma_count == 4 {
Some(i)
}
else{
None
}
});
// println!("cur comma idx {:?}", last_comma_idx);
if let Some(idx) = last_comma_idx {
last_comma_idx = None;
comma_count = 0;
Some(idx)
}else{
None
}
};
// Start our async while loop passing our predicate and buffer to read_until_index_found,
// so our loop will continue returning the chunks of strings that are resulting
// from the returned indices in our predicate.
while let Ok(Some(_)) = buf.read_until_index_found(&mut predicate, &mut fillable).await {
println!("in while await loop {:?}", str::from_utf8(&fillable).unwrap());
fillable.clear();
}
#});