Skip to content

Commit

Permalink
fix: default Read::read_to_end panic on seeked
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Sep 30, 2024
1 parent 560f89b commit ea14c30
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
4 changes: 4 additions & 0 deletions fusio/src/dynamic/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl<'read> Read for Box<dyn DynFile + 'read> {
Ok(unsafe { B::recover_from_buf_mut(buf) })
}

async fn read_to_end(&mut self, buf: Vec<u8>) -> Result<Vec<u8>, Error> {
DynRead::read_to_end(self.as_mut(), buf).await
}

async fn size(&self) -> Result<u64, Error> {
DynRead::size(self.as_ref()).await
}
Expand Down
33 changes: 24 additions & 9 deletions fusio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,8 @@ pub trait Read: MaybeSend + MaybeSync {

fn read_to_end(
&mut self,
mut buf: Vec<u8>,
) -> impl Future<Output = Result<Vec<u8>, Error>> + MaybeSend {
async move {
buf.resize(self.size().await? as usize, 0);
let buf = self.read_exact(buf).await?;
Ok(buf)
}
}
buf: Vec<u8>,
) -> impl Future<Output = Result<Vec<u8>, Error>> + MaybeSend;

fn size(&self) -> impl Future<Output = Result<u64, Error>> + MaybeSend;
}
Expand All @@ -93,6 +87,11 @@ where
Ok(buf)
}

async fn read_to_end(&mut self, mut buf: Vec<u8>) -> Result<Vec<u8>, Error> {
let _ = std::io::Read::read_to_end(self, &mut buf)?;
Ok(buf)
}

async fn size(&self) -> Result<u64, Error> {
Ok(self.get_ref().as_ref().len() as u64)
}
Expand Down Expand Up @@ -143,6 +142,13 @@ impl<R: Read> Read for &mut R {
R::read_exact(self, buf)
}

fn read_to_end(
&mut self,
buf: Vec<u8>,
) -> impl Future<Output = Result<Vec<u8>, Error>> + MaybeSend {
R::read_to_end(self, buf)
}

fn size(&self) -> impl Future<Output = Result<u64, Error>> + MaybeSend {
R::size(self)
}
Expand Down Expand Up @@ -171,7 +177,9 @@ impl<W: Write> 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)]
Expand Down Expand Up @@ -233,6 +241,13 @@ mod tests {
.inspect(|buf| self.cnt += buf.bytes_init())
}

async fn read_to_end(&mut self, buf: Vec<u8>) -> Result<Vec<u8>, Error> {
self.r
.read_to_end(buf)
.await
.inspect(|buf| self.cnt += buf.bytes_init())
}

async fn size(&self) -> Result<u64, Error> {
self.r.size().await
}
Expand Down
10 changes: 9 additions & 1 deletion fusio/src/local/monoio/mod.rs
Original file line number Diff line number Diff line change
@@ -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<B> {
Expand Down Expand Up @@ -94,6 +96,12 @@ impl Read for MonoioFile {
Ok(buf.buf)
}

async fn read_to_end(&mut self, mut buf: Vec<u8>) -> Result<Vec<u8>, Error> {
buf.resize((self.size().await? - self.pos) as usize, 0);

Ok(self.read_exact(buf).await?)
}

async fn size(&self) -> Result<u64, Error> {
let metadata = File::metadata(self.file.as_ref().expect("read file after closed")).await?;
Ok(metadata.len())
Expand Down
5 changes: 5 additions & 0 deletions fusio/src/local/tokio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ impl Read for File {
Ok(buf)
}

async fn read_to_end(&mut self, mut buf: Vec<u8>) -> Result<Vec<u8>, Error> {
let _ = AsyncReadExt::read_to_end(self, &mut buf).await?;
Ok(buf)
}

async fn size(&self) -> Result<u64, Error> {
Ok(self.metadata().await?.len())
}
Expand Down
10 changes: 9 additions & 1 deletion fusio/src/local/tokio_uring/mod.rs
Original file line number Diff line number Diff line change
@@ -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<B> {
Expand Down Expand Up @@ -94,6 +96,12 @@ impl Read for TokioUringFile {
Ok(buf.buf)
}

async fn read_to_end(&mut self, mut buf: Vec<u8>) -> Result<Vec<u8>, Error> {
buf.resize((self.size().await? - self.pos) as usize, 0);

Ok(self.read_exact(buf).await?)
}

async fn size(&self) -> Result<u64, Error> {
let stat = self
.file
Expand Down

0 comments on commit ea14c30

Please sign in to comment.