Can reference, but not use, a shared http request builder instance

I'm trying to write some code that will interact with a web service that talks protobuf. So far I'm using prost to translate protobuf definitions into Rust structs and hyper as an HTTP stack, but I've run into a basic Rust road-block right out of the gate; I'm sure this is basic borrow checker stuff, just not sure how to resolve it.

Here's some sample code on the playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=501c1c850909fecc8b9b821082a9f684

The idea is to create a request builder and configure it with what doesn't change from request to request; then to re-use it to build each individual request; in actual use this code will also supply a request body, but I'm still trying to work out how to do that too :slight_smile: For now, I just need to figure out how to fix this problem...

Any help appreciated!

You can just clone() the builder I suppose.

Sorry, forget what I said. I looked at the wrong Builder type.

I wonder if http::request::Builder not implementing Clone is an oversight or whether there is a good reason for that.

If you take a look at the documentation for Builder::uri, you'll see that it takes self by-value as there is no ampersand. This means you need ownership to call it. Unfortunately Builder does not implement clone, so you can't just clone it to create a new object.

I suggest you provide a function that creates a builder instead of reusing one.

1 Like

Hmm, I must have misunderstood something somewhere, as I thought I read that the builder was specifically intended to be reused this way -- I can't find where I saw that now, though. Your suggestion would seem to be the way to go.

I was also curious about this. It turns out that http::request::Builder can't be Clone because it contains Extensions, which stores non-clonable boxed trait objects.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.