mirror of
https://github.com/deuxfleurs-org/garage.git
synced 2026-08-19 17:46:17 +00:00
[refactor-block] move read_stream_to_end to garage_net
This commit is contained in:
@@ -3,6 +3,8 @@ use std::collections::VecDeque;
|
||||
|
||||
use bytes::BytesMut;
|
||||
|
||||
use crate::stream::ByteStream;
|
||||
|
||||
pub use bytes::Bytes;
|
||||
|
||||
/// A circular buffer of bytes, internally represented as a list of Bytes
|
||||
@@ -119,6 +121,17 @@ impl BytesBuf {
|
||||
pub fn into_slices(self) -> VecDeque<Bytes> {
|
||||
self.buf
|
||||
}
|
||||
|
||||
/// Return the entire buffer concatenated into a single big Bytes
|
||||
pub fn into_bytes(mut self) -> Bytes {
|
||||
self.take_all()
|
||||
}
|
||||
|
||||
/// Return the content as a stream of individual chunks
|
||||
pub fn into_stream(self) -> ByteStream {
|
||||
use futures::stream::StreamExt;
|
||||
Box::pin(futures::stream::iter(self.buf).map(|x| Ok(x)))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BytesBuf {
|
||||
|
||||
@@ -200,3 +200,14 @@ pub fn asyncread_stream<R: AsyncRead + Send + Sync + 'static>(reader: R) -> Byte
|
||||
pub fn stream_asyncread(stream: ByteStream) -> impl AsyncRead + Send + Sync + 'static {
|
||||
tokio_util::io::StreamReader::new(stream)
|
||||
}
|
||||
|
||||
/// Reads all of the content of a `ByteStream` into a BytesBuf
|
||||
/// that contains everything
|
||||
pub async fn read_stream_to_end(mut stream: ByteStream) -> Result<BytesBuf, std::io::Error> {
|
||||
let mut buf = BytesBuf::new();
|
||||
while let Some(part) = stream.next().await {
|
||||
buf.extend(part?);
|
||||
}
|
||||
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user