How to create a vector of a specific number of Results?

Viewed 85

I am trying to send a stream of data with a specific size without the content length header. I believe hyper::body::Body::wrap_stream is what I'm looking for. However, I'm not sure how to extend the example of creating a vector of results given in documentation to incorporate a custom size.

I'm looking for something like this, but I don't know the best way to resolve issues with vec![Ok(0); bytes].

let chunks: Vec<Result<_, std::io::Error>> = vec![Ok(0); bytes];

let stream = futures_util::stream::iter(chunks);

let body = Body::wrap_stream(stream);
1 Answers

The problem here is that Result does not implement Clone so you can't use this syntax to initialize the Vec.

You can make it work by changing it as follow:

let mut chunks: Vec<Result<_, std::io::Error>> = Vec::new();
chunks.resize_with(bytes, || Ok(0));

This way, you initialize independent Result values.

Related