update filereader/writer todo

This commit is contained in:
weisd
2025-06-06 18:04:51 +08:00
parent 15a3012d05
commit b51ee48699
22 changed files with 178 additions and 202 deletions
+1 -1
View File
@@ -312,7 +312,7 @@ impl FileInfo {
/// Check if the object is remote (transitioned to another tier)
pub fn is_remote(&self) -> bool {
!self.transition_tier.as_ref().map_or(true, |s| s.is_empty())
!self.transition_tier.as_ref().is_none_or(|s| s.is_empty())
}
/// Get the data directory for this object
+1
View File
@@ -21,6 +21,7 @@ pub use hash_reader::*;
pub mod compress;
pub mod reader;
pub use reader::WarpReader;
mod writer;
use tokio::io::{AsyncRead, BufReader};
+26
View File
@@ -1 +1,27 @@
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, ReadBuf};
use crate::{EtagResolvable, HashReaderDetector, Reader};
pub struct WarpReader<R> {
inner: R,
}
impl<R: AsyncRead + Unpin + Send + Sync> WarpReader<R> {
pub fn new(inner: R) -> Self {
Self { inner }
}
}
impl<R: AsyncRead + Unpin + Send + Sync> AsyncRead for WarpReader<R> {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_read(cx, buf)
}
}
impl<R: AsyncRead + Unpin + Send + Sync> HashReaderDetector for WarpReader<R> {}
impl<R: AsyncRead + Unpin + Send + Sync> EtagResolvable for WarpReader<R> {}
impl<R: AsyncRead + Unpin + Send + Sync> Reader for WarpReader<R> {}
-76
View File
@@ -1,8 +1,6 @@
use std::io::Cursor;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::AsyncWrite;
use tokio::io::AsyncWriteExt;
use crate::HttpWriter;
@@ -92,77 +90,3 @@ impl AsyncWrite for Writer {
}
}
}
/// WriterAll wraps a Writer and ensures each write writes the entire buffer (like write_all).
pub struct WriterAll<W: AsyncWrite + Unpin> {
inner: W,
}
impl<W: AsyncWrite + Unpin> WriterAll<W> {
pub fn new(inner: W) -> Self {
Self { inner }
}
/// Write the entire buffer, like write_all.
pub async fn write_all(&mut self, mut buf: &[u8]) -> std::io::Result<()> {
while !buf.is_empty() {
let n = self.inner.write(buf).await?;
if n == 0 {
return Err(std::io::Error::new(std::io::ErrorKind::WriteZero, "failed to write whole buffer"));
}
buf = &buf[n..];
}
Ok(())
}
/// Get a mutable reference to the inner writer.
pub fn get_mut(&mut self) -> &mut W {
&mut self.inner
}
}
impl<W: AsyncWrite + Unpin> AsyncWrite for WriterAll<W> {
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, mut buf: &[u8]) -> Poll<std::io::Result<usize>> {
let mut total_written = 0;
while !buf.is_empty() {
// Safety: W: Unpin
let inner_pin = Pin::new(&mut self.inner);
match inner_pin.poll_write(cx, buf) {
Poll::Ready(Ok(0)) => {
if total_written == 0 {
return Poll::Ready(Ok(0));
} else {
return Poll::Ready(Ok(total_written));
}
}
Poll::Ready(Ok(n)) => {
total_written += n;
buf = &buf[n..];
}
Poll::Ready(Err(e)) => {
if total_written == 0 {
return Poll::Ready(Err(e));
} else {
return Poll::Ready(Ok(total_written));
}
}
Poll::Pending => {
if total_written == 0 {
return Poll::Pending;
} else {
return Poll::Ready(Ok(total_written));
}
}
}
}
Poll::Ready(Ok(total_written))
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_shutdown(cx)
}
}