-
Notifications
You must be signed in to change notification settings - Fork 62
feat(util): add data() method #127
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
Open
zhangli-pear
wants to merge
1
commit into
hyperium:master
Choose a base branch
from
zhangli-pear:add_data_method
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use http_body::Body; | ||
|
||
use core::future::Future; | ||
use core::pin::Pin; | ||
use core::task; | ||
|
||
#[must_use = "futures don't do anything unless polled"] | ||
#[derive(Debug)] | ||
/// Future that resolves to the next data chunk from `Body` | ||
pub struct Data<'a, T: ?Sized>(pub(crate) &'a mut T); | ||
|
||
impl<'a, T: Body + Unpin + ?Sized> Future for Data<'a, T> { | ||
type Output = Option<Result<T::Data, T::Error>>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> { | ||
match Pin::new(&mut self.0).poll_frame(ctx) { | ||
task::Poll::Ready(Some(Ok(frame))) => { | ||
if let Ok(data) = frame.into_data() { | ||
task::Poll::Ready(Some(Ok(data))) | ||
} else { | ||
task::Poll::Pending | ||
} | ||
}, | ||
task::Poll::Ready(Some(Err(e))) => task::Poll::Ready(Some(Err(e))), | ||
task::Poll::Ready(None) => task::Poll::Ready(None), | ||
task::Poll::Pending => task::Poll::Pending, | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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'm not sure this is correct. now that bodies yield
Frame<T>
's, a body can yield either a data frame or a trailers frame, it seems like there is not a way to expose an equivalentdata()
method.previously,
Body::data()
andBody::trailers()
could delegate downwards toBody::poll_data()
andBody::poll_trailers()
, respectively. now, the trait provides a singleBody::poll_frame()
trait method, so there is not a clear poll function which thisData<'a, T>
future can invoke.this conditional block would discard a trailers frame, and additionally, it could cause a task to become deadlocked. see
Poll::Pending
, which states:so, not only would the data yielded by the body not be accurately represented when this function is used, but additionally, it leaves the task in a state that violates the contract outlined by
std
.i am sympathetic about the fact that the frame-oriented 1.0 interface causes some significant churn (see our work in linkerd/linkerd2#8733 for an example), but it is unfortunately incompatible with a
data()
method like this. i can't speak for the project myself, but i feel somewhat strongly as an onlooker that this change should not be pursued.