diff --git a/fusio/src/lib.rs b/fusio/src/lib.rs index 1de65cd..f0e80c8 100644 --- a/fusio/src/lib.rs +++ b/fusio/src/lib.rs @@ -66,6 +66,38 @@ pub trait Read: MaybeSend + MaybeSync { buf: B, ) -> impl Future, B)> + MaybeSend; + fn read_exact( + &mut self, + mut buf: B, + ) -> impl Future, 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,