-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bytes no longer implements Extend<u8> #324
Comments
I think it was accidental. @seanmonstar ? |
Partly intentional, partly just punted. It was intentionally removed in the pass that removed all mutation of a Another idea is to encourage using some |
If Bytes doesn't implement body.try_concat().await where body is a hyper Unless you can think of a suitable replacement for that use of |
without this i have- let mut response = request.await?.into_body();
let mut v = Vec::default();
while let Some(bytes_result) = response.next().await {
let bytes = bytes_result?;
v.extend(bytes)
} or maybe let bytes = response_body
.try_fold(Vec::default(), |mut v, bytes| async {
v.extend(bytes);
Ok(v)
})
.await?; or worst of all, let mut response = request.await?.into_body();
let chunks: Vec<Vec<u8>> = response.collect().await?;
let bytes: Vec<u8> = chunks.into_iter().flatten().collect(); none of which i love |
you could implement it directly on /// Returns a future of the concatenated raw bytes from the [`Body`](Body).
///
/// # Example
/// ```
/// # async {
/// # let body = hyper::Body::empty()
/// let bytes = body.concat().await?;
/// # Ok(())
/// # };
/// ```
#[cfg(feature = "stream")]
pub async fn concat(mut self) -> crate::Result<Bytes> {
let mut buf = BytesMut::new();
while let Some(bytes) = self.next().await {
buf.extend(bytes?)
}
Ok(buf.freeze())
} |
Trying to update my code and I am hitting this bug: The workaround I had was as follows: let bytes = FileStream::new("Cargo.toml").try_fold(BytesMut::new(), |mut buf, bytes| {
buf.extend_from_slice(&bytes);
future::ready(Ok(buf))
})
.await?; I can submit a PR for this if needed? I think it makes things a lot more ergonomic if we can use let bytes = FileStream::new("Cargo.toml")
.try_concat()
.await?; |
Tokio will be providing a version of collect that supports Bytes. It is on master but not released yet. |
Tokio 0.3 removed this impl again. |
tokio 0.3 removed |
This means that in particular you can't use
StreamExt::concat
on a stream ofBytes
. Is that change intentional or something that got lost in the rewrites?The text was updated successfully, but these errors were encountered: