|
1 | 1 | use bytes::Bytes; |
2 | | -use futures::io::AsyncRead; |
3 | | -use futures::stream::Stream; |
4 | | -use futures::task::Poll; |
| 2 | +use dyn_clone::DynClone; |
| 3 | +use futures::{io::AsyncRead, stream::Stream, task::Poll}; |
| 4 | +use std::{pin::Pin, task::Context}; |
| 5 | + |
| 6 | +/// Amount of the stream to buffer in memory during streaming uploads |
| 7 | +pub(crate) const DEFAULT_BUFFER_SIZE: usize = 1024 * 64; |
5 | 8 |
|
6 | 9 | /// Enable a type implementing `AsyncRead` to be consumed as if it were |
7 | 10 | /// a `Stream` of `Bytes`. |
8 | 11 | #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] |
9 | 12 | #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] |
10 | | -pub trait SeekableStream: |
11 | | - AsyncRead + Unpin + std::fmt::Debug + Send + Sync + dyn_clone::DynClone |
12 | | -{ |
| 13 | +pub trait SeekableStream: AsyncRead + Unpin + std::fmt::Debug + Send + Sync + DynClone { |
13 | 14 | async fn reset(&mut self) -> crate::error::Result<()>; |
14 | 15 | fn len(&self) -> usize; |
15 | 16 |
|
16 | 17 | fn is_empty(&self) -> bool { |
17 | 18 | self.len() == 0 |
18 | 19 | } |
| 20 | + |
| 21 | + fn buffer_size(&self) -> usize { |
| 22 | + DEFAULT_BUFFER_SIZE |
| 23 | + } |
19 | 24 | } |
20 | 25 |
|
21 | 26 | dyn_clone::clone_trait_object!(SeekableStream); |
22 | 27 |
|
23 | 28 | impl Stream for dyn SeekableStream { |
24 | 29 | type Item = crate::error::Result<Bytes>; |
25 | 30 |
|
26 | | - fn poll_next( |
27 | | - self: std::pin::Pin<&mut Self>, |
28 | | - cx: &mut std::task::Context<'_>, |
29 | | - ) -> std::task::Poll<Option<Self::Item>> { |
30 | | - let mut buffer = vec![0_u8; 1024 * 64]; |
| 31 | + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 32 | + let mut buffer = vec![0_u8; self.buffer_size()]; |
31 | 33 |
|
32 | 34 | match self.poll_read(cx, &mut buffer) { |
33 | 35 | Poll::Ready(Ok(0)) => Poll::Ready(None), |
|
0 commit comments