Skip to content

Commit

Permalink
feat: support default Read::read_exact
Browse files Browse the repository at this point in the history
  • Loading branch information
ethe committed Oct 9, 2024
1 parent 919b298 commit fad5377
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions fusio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ pub trait Read: MaybeSend + MaybeSync {
buf: B,
) -> impl Future<Output = (Result<u64, Error>, B)> + MaybeSend;

fn read_exact<B: IoBufMut>(
&mut self,
mut buf: B,
) -> impl Future<Output = (Result<(), Error>, B)> + MaybeSend {
async move {
let len = buf.bytes_init() as u64;
let mut read = 0;
while read < len {
let (result, buf_mut) = self.read(unsafe { buf.to_buf_mut_nocopy() }).await;
buf = unsafe { B::recover_from_buf_mut(buf_mut) };
match result {
Ok(0) => {
return (
Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
)
.into()),
buf,
)
}
Ok(n) => {
read += n;
}
Err(Error::Io(ref e)) if e.kind() == std::io::ErrorKind::Interrupted => {}
Err(e) => return (Err(e), buf),
}
}
(Ok(()), buf)
}
}

fn read_to_end(
&mut self,
buf: Vec<u8>,
Expand Down

0 comments on commit fad5377

Please sign in to comment.