diff --git a/fusio/src/dynamic/fs.rs b/fusio/src/dynamic/fs.rs index c29a7dd..5182c77 100644 --- a/fusio/src/dynamic/fs.rs +++ b/fusio/src/dynamic/fs.rs @@ -26,6 +26,10 @@ impl<'read> Read for Box { Ok(unsafe { B::recover_from_buf_mut(buf) }) } + async fn read_to_end(&mut self, buf: Vec) -> Result, Error> { + DynRead::read_to_end(self.as_mut(), buf).await + } + async fn size(&self) -> Result { DynRead::size(self.as_ref()).await } diff --git a/fusio/src/lib.rs b/fusio/src/lib.rs index 26f455a..afd7e33 100644 --- a/fusio/src/lib.rs +++ b/fusio/src/lib.rs @@ -68,14 +68,8 @@ pub trait Read: MaybeSend + MaybeSync { fn read_to_end( &mut self, - mut buf: Vec, - ) -> impl Future, Error>> + MaybeSend { - async move { - buf.resize(self.size().await? as usize, 0); - let buf = self.read_exact(buf).await?; - Ok(buf) - } - } + buf: Vec, + ) -> impl Future, Error>> + MaybeSend; fn size(&self) -> impl Future> + MaybeSend; } @@ -93,6 +87,11 @@ where Ok(buf) } + async fn read_to_end(&mut self, mut buf: Vec) -> Result, Error> { + let _ = std::io::Read::read_to_end(self, &mut buf)?; + Ok(buf) + } + async fn size(&self) -> Result { Ok(self.get_ref().as_ref().len() as u64) } @@ -143,6 +142,13 @@ impl Read for &mut R { R::read_exact(self, buf) } + fn read_to_end( + &mut self, + buf: Vec, + ) -> impl Future, Error>> + MaybeSend { + R::read_to_end(self, buf) + } + fn size(&self) -> impl Future> + MaybeSend { R::size(self) } @@ -171,7 +177,9 @@ impl Write for &mut W { #[cfg(test)] mod tests { - use super::{Read, Write}; + use std::future::Future; + + use super::{MaybeSend, Read, Write}; use crate::{buf::IoBufMut, Error, IoBuf, Seek}; #[allow(unused)] @@ -233,6 +241,13 @@ mod tests { .inspect(|buf| self.cnt += buf.bytes_init()) } + async fn read_to_end(&mut self, buf: Vec) -> Result, Error> { + self.r + .read_to_end(buf) + .await + .inspect(|buf| self.cnt += buf.bytes_init()) + } + async fn size(&self) -> Result { self.r.size().await } diff --git a/fusio/src/local/monoio/mod.rs b/fusio/src/local/monoio/mod.rs index 938d503..d6c3c92 100644 --- a/fusio/src/local/monoio/mod.rs +++ b/fusio/src/local/monoio/mod.rs @@ -1,9 +1,11 @@ #[cfg(feature = "fs")] pub mod fs; +use std::future::Future; + use monoio::fs::File; -use crate::{buf::IoBufMut, Error, IoBuf, Read, Seek, Write}; +use crate::{buf::IoBufMut, Error, IoBuf, MaybeSend, Read, Seek, Write}; #[repr(transparent)] struct MonoioBuf { @@ -94,6 +96,12 @@ impl Read for MonoioFile { Ok(buf.buf) } + async fn read_to_end(&mut self, mut buf: Vec) -> Result, Error> { + buf.resize((self.size().await? - self.pos) as usize, 0); + + Ok(self.read_exact(buf).await?) + } + async fn size(&self) -> Result { let metadata = File::metadata(self.file.as_ref().expect("read file after closed")).await?; Ok(metadata.len()) diff --git a/fusio/src/local/tokio/mod.rs b/fusio/src/local/tokio/mod.rs index 4f62424..9f8f44c 100644 --- a/fusio/src/local/tokio/mod.rs +++ b/fusio/src/local/tokio/mod.rs @@ -44,6 +44,11 @@ impl Read for File { Ok(buf) } + async fn read_to_end(&mut self, mut buf: Vec) -> Result, Error> { + let _ = AsyncReadExt::read_to_end(self, &mut buf).await?; + Ok(buf) + } + async fn size(&self) -> Result { Ok(self.metadata().await?.len()) } diff --git a/fusio/src/local/tokio_uring/mod.rs b/fusio/src/local/tokio_uring/mod.rs index 783ce86..946a578 100644 --- a/fusio/src/local/tokio_uring/mod.rs +++ b/fusio/src/local/tokio_uring/mod.rs @@ -1,9 +1,11 @@ #[cfg(feature = "fs")] pub mod fs; +use std::future::Future; + use tokio_uring::fs::File; -use crate::{Error, IoBuf, IoBufMut, Read, Seek, Write}; +use crate::{Error, IoBuf, IoBufMut, MaybeSend, Read, Seek, Write}; #[repr(transparent)] struct TokioUringBuf { @@ -94,6 +96,12 @@ impl Read for TokioUringFile { Ok(buf.buf) } + async fn read_to_end(&mut self, mut buf: Vec) -> Result, Error> { + buf.resize((self.size().await? - self.pos) as usize, 0); + + Ok(self.read_exact(buf).await?) + } + async fn size(&self) -> Result { let stat = self .file