-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Make Response more generic #1164
Conversation
Twey
commented
May 4, 2017
- The commit messages match the guidelines in https://github.com/hyperium/hyper/blob/master/CONTRIBUTING.md#git-commit-guidelines
What was the exact problem you're trying to solve here, so I can have some more context? |
Essentially, I have some code where I transform the response body but want to leave the rest of the response info intact for upstream consumption. Specifically, I'm a) asserting that a particular API call always returns a body, and don't want my callers to always have to |
I've been thinking about adding 1 piece here, that would do what you want, a impl<B: Default> Response<B> {
pub fn split(self) -> (Response<B>, B);
} impl<B> Response<B> {
pub fn with_body<BB>(self, body: BB) -> Response<BB>;
} With just those changes, could you achieve what you want? I think I'd prefer having a |
Yes, this is similar to the method I've called However, that approach alone, keeping the It's a simple translation from |
Make methods on Response more generic over the body type, including methods to extract and modify the body (including changing body type) and moving the optionality of the body into the type parameter, and remove reliance on Body in several places.
I'm not ignoring this, I've come back to re-read a few times now. I'm going to try out the PR locally and fiddle. I'm just afraid of being harder to use, but maybe it's required. |
Ok, thinking about this, the general idea seems like the right way to go. I mostly didn't like hyper's default |
Yes, absolutely! That sounds great. In fact we already have the notion of an ‘empty’ body. |
Is EDIT: Scratch that — I guess you explicitly want to be able to say things like |
After trying out a bunch of impls, I don't think what I said can actually be done properly. I'm now back to thinking that the I could see impl<B: Default> Response<B> {
pub fn split(self) -> (Response<Empty>, B);
}
pub struct Empty(()); Or possibly |
What's wrong with the impls? Unfortunately, that approach doesn't fix the primary issue, which is that there's no information in the type system about whether or not the response has a body, so the user has to check or Re |
The issue I ran into is that libstd already defines a blanket I'm sorry for the long silence. I actually am quite interested in this problem, and you're work in this PR has caused a few (even recently) discussions among some of us. Some of the tricky part is how to define that a message has no body. |
Would we want custom ones? As for If |
The problem is that the blanket We can't do this in hyper 0.11, but while trying to design an I'm going to close the PR cause we can't merge, but I want to state again: thank you! This PR has been a huge help, even though it hasn't been merged. |
I'm still thinking about this! I've actually put it on my work schedule for this/next week, since we currently have an internal Feel free to close, but would you be happy with a similar PR with alternative API suggestions? |
As I mentioned, we're planning to extract these types into a plain struct Request<B> {
// head stuff
body: B,
}
impl<B> Request<B> {
pub fn body(&self) -> &B { &self.body }
pub fn body_mut(&mut self) -> &mut B { &mut self.body }
} Granted, once hyper is using such a type, we probably need some bounds on Did you have other thoughts besides this? (Oh, |
FYI I'm looking forward to the solution for this as well. My use case is a server with plugins. Consider a As far as I can tell, I can't do this direclty in hyper 0.11. I have to instead define my handlers to return a different internal-to-my-crate type that looks like Response, and then after all the plugins, turn it into a hyper Response. |