Patching operation buf-reader impl started

Fortune for rematch's current commit: Future small blessing − 末小吉
repatch
Avril 2 days ago
parent 9c1a627ee0
commit 937e3f6579
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -68,7 +68,7 @@ pub trait Operation: Send + Sync
///
/// The type must be `Send`, as it is possible for the operation instance to be moved between threads between sequential runs.
/// The state is only reused for input streams from the same input. As processing unrelated streams (different inputs) can happen concurrently, each new input has a new state object created for it (see `create_state_for()`.)
type State: Any + Send + 'static;
type State: Any + Send + 'static /* = NoState */;
/// Create a state for this input stream.
///
@ -134,3 +134,78 @@ fn _assert_obj_safe(_: &(dyn OperationDyn + Send + Sync + 'static), _: BoxOperat
/// A boxed operation. Operations are generated dynamically and so are dispatched on dynamically.
pub type BoxOperation<'bx> = Box<dyn OperationDyn + Send + Sync + 'bx>;
/// Generic processing functions
pub mod proc {
use super::*;
use io::BufRead;
pub fn proc_split_stream<Op: Operation,
I: io::Read,
O: io::Write,
P: AsRef<str>>
(op: &Op,
mut input: I,
pa: P,
mut output: O) -> io::Result<()>
{
let pa = pa.as_ref();
let mut buf = io::BufReader::new(input);
//TODO: Turn this into an iterator type that takes a stream, reads into a growing buffer until `P` is found, then returns the buffer up to, but not including, `P`, removes that part from the buffer, and continues.
todo!()
}
/// An iterator that grows an internal buffer from an input stream until a substring of bytes in that buffer is found.
/// The iterator then yeilds the part of the buffer containing the pre-substring section, removes that section and the subsrtring from itself, and continues.
#[derive(Debug)]
pub struct StreamSplitIter<'a, T: ?Sized>
{
stream: &'a mut T,
buffer: Vec<u8>,
orig_cap: usize,
split_on: &'a str,
}
impl<'a, T: ?Sized + 'a> StreamSplitIter<'a, T>
where T: io::Read
{
fn try_extend_buffer_once(&mut self) -> io::Result<&[u8]>
{
let i = self.buffer.len();
self.buffer.resize(i + self.orig_cap, 0);
let sl = {
let sl = &mut self.buffer[i..];
match self.stream.read(sl) {
Err(err) => {
// Reset buffer size
self.buffer.resize(i, 0);
return Err(err);
},
Ok(0) => {
self.buffer.resize(i, 0);
return Ok(&[]);
},
Ok(n) if n != sl.len() => {
self.buffer.resize(n, 0);
&self.buffer[i..(i+n)]
}
Ok(n) => &self.buffer[i..(i+n)],
}
};
Ok(sl)
}
}
impl<'a, T: ?Sized + 'a> Iterator for StreamSplitIter<'a, T>
where T: io::Read
{
type Item = io::Result<Vec<u8>>;
fn next(&mut self) -> Option<Self::Item>
{
todo!()
}
}
}

Loading…
Cancel
Save