use std::{
pin::Pin,
task::{Context, Poll, ready},
};
use bytes::Bytes;
use futures_core::Stream;
use tokio_util::io::ReaderStream;
use crate::BoxError;
pub struct File(ReaderStream<tokio::fs::File>);
impl File {
pub async fn open(path: &std::path::Path) -> std::io::Result<Self> {
let file = tokio::fs::File::open(path).await?;
Ok(Self(ReaderStream::new(file)))
}
}
impl Stream for File {
type Item = Result<Bytes, BoxError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let inner = Pin::new(&mut self.0);
match ready!(inner.poll_next(cx)) {
Some(Ok(bytes)) => Some(Ok(bytes)),
Some(Err(err)) => Some(Err(err.into())),
None => None,
}
.into()
}
}