mirror of
https://git.asonix.dog/asonix/pict-rs.git
synced 2025-02-24 17:36:19 +00:00
BytesReader: implement in terms of BufReader
This commit is contained in:
parent
939cd7955c
commit
c8817db997
1 changed files with 10 additions and 17 deletions
|
@ -1,3 +1,5 @@
|
|||
use std::io::BufRead;
|
||||
|
||||
use actix_web::web::Bytes;
|
||||
|
||||
pub(crate) struct BytesReader {
|
||||
|
@ -83,29 +85,20 @@ impl std::io::Read for BytesReader {
|
|||
let mut written = 0;
|
||||
|
||||
loop {
|
||||
let mut position = self.position;
|
||||
|
||||
for bytes in &self.buf {
|
||||
if position < bytes.len() {
|
||||
let copy_len = (bytes.len() - position).min(buf.len() - written);
|
||||
|
||||
buf[written..written + copy_len]
|
||||
.copy_from_slice(&bytes[position..position + copy_len]);
|
||||
position = 0;
|
||||
self.position += copy_len;
|
||||
match self.fill_buf() {
|
||||
Ok([]) => return Ok(written),
|
||||
Ok(b) => {
|
||||
let copy_len = buf[written..].len().min(b.len());
|
||||
buf[written..written + copy_len].copy_from_slice(&b[..copy_len]);
|
||||
self.consume(copy_len);
|
||||
written += copy_len;
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
|
||||
if written == buf.len() {
|
||||
return Ok(written);
|
||||
}
|
||||
} else {
|
||||
position -= bytes.len();
|
||||
}
|
||||
}
|
||||
|
||||
if !self.blocking_recv()? {
|
||||
return Ok(written);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue