Skip to content
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

feat: implement Body::size_hint for MapFrame #137

Closed

Conversation

zino23
Copy link

@zino23 zino23 commented Dec 20, 2024

@seanmonstar
Copy link
Member

A problem is that map_frame() can change the frames, perhaps adding or removing from the data, so the size hint is no longer accurate.

@zino23
Copy link
Author

zino23 commented Dec 23, 2024

A problem is that map_frame() can change the frames, perhaps adding or removing from the data, so the size hint is no longer accurate.

Yes indeed. One rational for this implementation, though, is the construction of a MapFrame does not change size of the body immediately. The transformation of the frame occurs when the body is poll_frame-ed.

impl<B, F, B2> Body for MapFrame<B, F>
where
    B: Body,
    F: FnMut(Frame<B::Data>) -> Frame<B2>,
    B2: Buf,
{
    type Data = B2;
    type Error = B::Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
        let this = self.project();
        match this.inner.poll_frame(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Ready(Some(Ok(frame))) => Poll::Ready(Some(Ok((this.f)(frame)))), // <- Transformation of the frame.
            Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(err))),
        }
    }
}

So whether to relay the size hint of a MapFrame to the inner body depends on how we perceive the size hint. If it means the size hint before transformation, then this implementation is ok. If it means the real size hint after transformation, then there is no way to know it and we simply return SizeHint::default().

@zino23 zino23 force-pushed the feat-implement-size-hint-for-mapframe branch from 9bb4acc to 9ece8a1 Compare December 23, 2024 12:51
@seanmonstar
Copy link
Member

Right, and there's no way for us to know what the closure is doing, so we have to toss the hint.

@zino23
Copy link
Author

zino23 commented Dec 23, 2024

So I guess I'll close this PR?

Also you can close this as it is as expected: seanmonstar/reqwest#2497

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

After converting reqwest::Response to http::Response the size hint is lost
2 participants