Skip to content
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

Build a blocking body from bytes::Bytes to allow copy-free chained network calls. #1114

Merged

Conversation

varoonp123
Copy link
Contributor

@varoonp123 varoonp123 commented Dec 15, 2020

Currently, if I want to use the reqwest::blocking::Client, the response can give me a bytes::Bytes but afaik there is no easy/cheap way to turn that into an owned contiguous bytes type needed to build a blocking body. As of 0.10.10, a blocking body can be constructed from a file, &'static [u8], String, &'static str, and Vec.

The async API DOES allow a body to be built from Bytes which is extremely convenient for getting bytes from one url and putting them at another url. My application would benefit greatly from minimizing copying bytes in these get-put chains and this trait implementation seems to be a natural way to address what I imagine is not an extremely uncommon use case. This trait implementation in the blocking API IMO makes sense with the design philosophy of the library, makes the blocking API more consistent with the async one, and allows greater composability within and between the clients.

While this seems like the simplest/minimal change that will effectively address this pattern of guiding bytes from a get response to a put request, other alternatives to this trait implementation include:

  1. Having a more direct blocking return type (blocking::Response.vec() or something) (I dont want to use std::String as an intermediate step)
  2. impl<T: AsRef<[u8]>> From<T> for blocking::Body
  3. Some other way that I dont know of to transform bytes::Bytes into a Vec<u8> without copying the bytes?
  4. Just clone the bytes

…tes to allow copy-free chained network calls.
Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I think this was just an oversight.

@seanmonstar seanmonstar merged commit 73990a7 into seanmonstar:master Dec 15, 2020
@varoonp123 varoonp123 deleted the feature/blocking_body_from_bytes branch December 15, 2020 15:39
pfernie pushed a commit to pfernie/reqwest that referenced this pull request Jan 5, 2021
pfernie pushed a commit to pfernie/reqwest that referenced this pull request Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants