0

I'm trying to implement a PUT request that transmits some bits to a web service:

extern crate reqwest;

fn put(buf: &[u8]) {
    let v = Vec::from(buf);
    let body = ::reqwest::Body::from(v);
    // execute the request
}

Is there a way to avoid the memory copy (which, if I understand correctly, occurs when constructing the vector) when constructing the Body?

I'm potentially sending large buffers and I'd prefer to avoid unnecessary copies (even if they're negligible compared to the network IO)

Shmoopy
  • 5,334
  • 4
  • 36
  • 72
  • 1
    You probably meant `Body::from(v)`, since `Body` implements `From>`. The brief answer to the question is "you can't, `Body` needs to own the content`". – E_net4 Jan 07 '18 at 17:27
  • 1
    It may still be worth mentioning how you obtained the `&[u8]` in the first place. Can't the original source be moved to the request body? – E_net4 Jan 07 '18 at 18:05
  • @E_net4 This is actually an `impl Write` that writes the buffer to an external web service – Shmoopy Jan 08 '18 at 08:55

0 Answers0