-
Notifications
You must be signed in to change notification settings - Fork 322
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
Response builder and related changes #580
Conversation
74dc6f3
to
3f06463
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have much critique on builders in general, though I do think if we do this we should also do RequestBuilder
, for api conformity.
The code certainly seems fine to me, it's nice that ResponseBuilder
is essentially a tuple and probably zero-ish-cost.
Re: other changes - I think From<Body> for Response
is probably useful enough to already take out and merge. I literally already thought that existed!
|
||
pub struct ResponseBuilder(Response); | ||
|
||
impl ResponseBuilder { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this include methods for cookie
and ext
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially added those, but I don't think they make a lot of sense on a builder. The purpose of the builder is to make expressing a one-off response easy. I think it's pretty reasonable to say if they need to make cookie or ext changes, that it's not a quick one-liner type response (before rustfmt). It's easy enough to do:
let mut res = Response::build().status(203).body("hello").unwrap();
res.add_cookie(…);
res.insert_ext(…);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not exactly seeing the advantage of that to writing this if it is only partial?
The same example with less characters:
let mut res = Response::new(203);
res.set_body("hello");
res.add_cookie(…);
res.insert_ext(…);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, in that circumstance there isn't any advantage to using the builder
@Fishrock123 I agree surf might want a RequestBuilder, but why would tide? App authors aren't ever constructing requests |
Good point, I think for some reason I was thinking this was in |
Thanks so much for kicking this off @jbr! I'm not quite convinced on builders yet, but think this is def worth exploring. With a bit of luck this indeed turns out to be a much more convenient way to create Responses and that would be neat. I do have a few design notes though:
|
Addressed 1, 2, 3, and 5. Added one example of 4, but not convinced it's particularly convenient to type in most circumstances |
Co-authored-by: Jeremiah Senkpiel <fishrock123@rocketmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
This PR does several things that are related, but could be distinct PRs. Please let me know if you'd prefer I break them out for independent review.
mut self
to taking&mut self
to conform to the rest of the response interfaceTryInto<StatusCode>
likeResponse::new()
doesprovides aExtracted to provide a From<Body> for Response #584From<Body> for Response
and uses that conversion in other Body -> Response conversions that already existed.adds a response builder: