1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use futures_util::stream::Stream;
use hyper::body::{Body, Bytes};
use std::io::Error as IoError;
use std::mem::MaybeUninit;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::fs::File;
use tokio::io::{AsyncRead, ReadBuf};
const BUF_SIZE: usize = 8 * 1024;
pub struct FileBytesStream {
file: File,
buf: Box<[MaybeUninit<u8>; BUF_SIZE]>,
}
impl FileBytesStream {
pub fn new(file: File) -> FileBytesStream {
let buf = Box::new([MaybeUninit::uninit(); BUF_SIZE]);
FileBytesStream { file, buf }
}
}
impl Stream for FileBytesStream {
type Item = Result<Bytes, IoError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let Self {
ref mut file,
ref mut buf,
} = *self;
let mut read_buf = ReadBuf::uninit(&mut buf[..]);
match Pin::new(file).poll_read(cx, &mut read_buf) {
Poll::Ready(Ok(())) => {
let filled = read_buf.filled();
if filled.is_empty() {
Poll::Ready(None)
} else {
Poll::Ready(Some(Ok(Bytes::copy_from_slice(filled))))
}
}
Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
Poll::Pending => Poll::Pending,
}
}
}
impl FileBytesStream {
pub fn into_body(self) -> Body {
Body::wrap_stream(self)
}
}