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

Add BufRead::consume #171

Merged
merged 2 commits into from
Sep 16, 2019
Merged

Add BufRead::consume #171

merged 2 commits into from
Sep 16, 2019

Conversation

yoshuawuyts
Copy link
Contributor

Ref #131. This implements BufReader::consume. Thanks!

Note on fill_buf: I couldn't get the async fn fill_buf() to work, but tracked progress for it here: https://gist.github.com/yoshuawuyts/09bbdc7823225ca96b5e35cd1da5d581. Got some lifetimes isssues. If anyone wants to give this a shot, please do!

@yoshuawuyts
Copy link
Contributor Author

yoshuawuyts commented Sep 10, 2019

I can't use poll_fill_buf manually at all, anywhere so I did some digging, and it seems we may need to do an unchecked projection here. From io::copy

    impl<R, W: Unpin + ?Sized> CopyFuture<'_, R, W> {
        fn project(self: Pin<&mut Self>) -> (Pin<&mut R>, Pin<&mut W>, &mut u64) {
            unsafe {
                let this = self.get_unchecked_mut();
                (
                    Pin::new_unchecked(&mut this.reader),
                    Pin::new(&mut *this.writer),
                    &mut this.amt,
                )
            }
        }
    }

///

                let buffer = futures_core::ready!(reader.as_mut().poll_fill_buf(cx))?;

@yoshuawuyts
Copy link
Contributor Author

yoshuawuyts commented Sep 10, 2019

Nope, that doesn't work either. If someone who's good at lifetimes could take a shot at this that'd be fantastic! I'm currently stuck with not being able to use any of the BufReader variants (from both async-std and futures-rs) because I can't call fill_buf.

@yoshuawuyts yoshuawuyts requested a review from a user September 10, 2019 00:20
@yoshuawuyts yoshuawuyts changed the title bufreader::consume Add BufRead::consume Sep 10, 2019
@yoshuawuyts yoshuawuyts changed the title Add BufRead::consume Add BufRead::consume Sep 10, 2019
@yoshuawuyts
Copy link
Contributor Author

Conversation with @tirr-c on whether to remove the Pin<&mut self> bounds for consume:

12:55 PM
]
Tirr
:
@yoshuawuyts the other methods in BufRead receives &mut self but this one is Pin<&mut Self>; is this because it's lower-level method?
[
12:55 PM
]
yoshuawuyts
:
oh shit
[
12:55 PM
]
yoshuawuyts
:
uhh
[
12:56 PM
]
yoshuawuyts
:
yeah good point :sweat_smile:
[
12:57 PM
]
yoshuawuyts
:
okay, this makes things more difficult
[
12:57 PM
]
yoshuawuyts
:
because the way I'm viewing it, fill_buf and consume very much seem like methods that should be called from userland; I have a legit use for it right now
[
12:58 PM
]
yoshuawuyts
:
but methods with Pin are not very user-friendly
[
12:58 PM
]
yoshuawuyts
:
as evidenced by the question of "is it safe to transmute a lifetime here"
[
12:58 PM
]
yoshuawuyts
:
so hmmm, I'm not sure what the answer is here?
[
1:02 PM
]
Tirr
:
well, the lifetime thing isn't the issue of Pin, it's because of the double reference caused by the wrapper type
[
1:03 PM
]
Tirr
:
anyway, Pin is more flexible but requires pinning before use (for Unpin types this is trivial though)
[
1:04 PM
]
yoshuawuyts
:
@Tirr could you say more about "pin is more flexible"?
[
1:07 PM
]
Tirr
:
umm, I meant Pin can receive already pinned value, but I noticed we can also use Pin to &mut self just now :smile:
[
1:08 PM
]
Tirr
:
Pin is unpin and AsyncBufRead if T::Target: AsyncBufRead
[
1:08 PM
]
yoshuawuyts
:
yeah, that's what I was wondering about
[
1:09 PM
]
Tirr
:
so &mut self is easier to use, yeah
[
1:09 PM
]
Tirr
:
hmm
[
1:09 PM
]
yoshuawuyts
:
so if consume(&mut self, amt: usize) where Self: Unpin then it shoudl always work right?
[
1:09 PM
]
yoshuawuyts
:
Not sure if the Self: Unpin bound is needed?
[
1:10 PM
]
yoshuawuyts
:
(probably not)
[
1:13 PM
]
Tirr
:
I'm not sure if it's necessary, but it's good to have to avoid the use of unsafe
[
1:14 PM
]
Tirr
:
without Unpin we need to use unsafe version of pinning before calling AsyncBufRead::consume
[
1:14 PM
]
yoshuawuyts
:
nods
[
1:14 PM
]
yoshuawuyts
:
that makes sense
[
1:15 PM
]
Tirr
:
Pin is always Unpin so that shouldn't bother users
[
1:15 PM
]
yoshuawuyts
:
ahh yeah
[
1:15 PM
]
yoshuawuyts
:
cool cool
[
1:16 PM
]
Tirr
:
well, it's Unpin for practical use cases; I checked the bounds, Pin

: Unpin where P: Unpin but we almost always use reference-like for P [ 1:17 PM ] yoshuawuyts : updated!

@yoshuawuyts
Copy link
Contributor Author

Related: #176!

ghost
ghost previously requested changes Sep 11, 2019
@yoshuawuyts yoshuawuyts requested a review from a user September 13, 2019 00:20
@yoshuawuyts yoshuawuyts dismissed ghost ’s stale review September 13, 2019 18:14

feedback implemented!

@yoshuawuyts
Copy link
Contributor Author

Implemented all feedback; bors r+

@ghost
Copy link

ghost commented Sep 16, 2019

bors r+

bors bot added a commit that referenced this pull request Sep 16, 2019
171: Add BufRead::consume r=stjepang a=yoshuawuyts

Ref #131. This implements `BufReader::consume`. Thanks!


Note on `fill_buf`: I couldn't get the `async fn fill_buf()` to work, but tracked progress for it here: https://gist.github.com/yoshuawuyts/09bbdc7823225ca96b5e35cd1da5d581. Got some lifetimes isssues. If anyone wants to give this a shot, please do!

Co-authored-by: Yoshua Wuyts <[email protected]>
@bors
Copy link
Contributor

bors bot commented Sep 16, 2019

Build succeeded

  • continuous-integration/travis-ci/push

@bors bors bot merged commit 10fedfe into master Sep 16, 2019
@yoshuawuyts yoshuawuyts deleted the bufreader-methods branch September 16, 2019 10:54
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.

1 participant