// Copyright 2021-2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 use bytes::Bytes; use futures::Stream; use std::io; use std::pin::Pin; use std::task::{Context, Poll}; use tokio::io::AsyncRead; // note, min_capacity doesn't mean we're going to always read at least this amount of data, // it defines the smallest allowed (by yours truly) upper bound const MIN_CAPACITY: usize = 16 * 1024; const DEFAULT_CAPACITY: usize = 64 * 1024; pub struct AvailableReader { inner: tokio_util::io::ReaderStream, } impl AvailableReader { pub fn new(reader: R, capacity: Option) -> Self { let capacity = capacity.unwrap_or(DEFAULT_CAPACITY).max(MIN_CAPACITY); AvailableReader { inner: tokio_util::io::ReaderStream::with_capacity(reader, capacity), } } } impl Stream for AvailableReader { type Item = io::Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { Pin::new(&mut self.inner).poll_next(cx) } }