http
http copied to clipboard
add Uri#into_builder() to easily modify a part of Uri
It is messy to modify a Uri instance with Uri::Builder. For example, if one wants to modify path_and_query, they should do something like this:
let uri: &Uri = req.uri();
let path_and_query: String = "...";
let parts = uri.clone().into_parts();
let modified_uri = Uri::builder()
.scheme(parts.scheme.unwrap())
.authority(parts.authority.unwrap())
.path_and_query(path_and_query.as_str())
.build()
.unwrap();
With this PR, it will become:
let uri: &Uri = req.uri();
let path_and_query: String = "...";
let modified_uri = uri.into_builder()
.path_and_query(path_and_query.as_str())
.build()
.unwrap();
It will let us focus on the concern.
What do you think of it?
Unofficial review: this looks good to me; I think it's solving a real problem with a nicely compact API.
Alternative design:
Builder::from_parts() / (impl From<Parts> for Builder)
construction becomes Builder::from_parts(uri.to_parts()) / (Builder::from(uri.to_parts())).
Pros:
- equally comprehensible
- mirrors
Uri::from_partsmethod - (permits
parts.into()where Builder is passed to fn)
Cons:
- uglier when chained
- requires
Builderimport
Came looking for this! Any chance we can get this reviewed by a maintainer?