-
Notifications
You must be signed in to change notification settings - Fork 340
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::fill_buf #176
Merged
Merged
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 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,32 @@ | ||
use std::pin::Pin; | ||
|
||
use futures_io::AsyncBufRead; | ||
|
||
use crate::future::Future; | ||
use crate::io; | ||
use crate::task::{Context, Poll}; | ||
|
||
#[doc(hidden)] | ||
#[allow(missing_debug_implementations)] | ||
pub struct FillBufFuture<'a, R: ?Sized> { | ||
reader: &'a mut R, | ||
} | ||
|
||
impl<'a, R: ?Sized> FillBufFuture<'a, R> { | ||
pub(crate) fn new(reader: &'a mut R) -> Self { | ||
Self { reader } | ||
} | ||
} | ||
|
||
impl<'a, R: AsyncBufRead + Unpin + ?Sized> Future for FillBufFuture<'a, R> { | ||
type Output = io::Result<&'a [u8]>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'a [u8]>> { | ||
let Self { reader } = &mut *self; | ||
let result = Pin::new(reader).poll_fill_buf(cx); | ||
// This is safe because: | ||
// 1. The buffer is valid for the lifetime of the reader. | ||
// 2. Output is unrelated to the wrapper (Self). | ||
result.map_ok(|buf| unsafe { std::mem::transmute::<&'_ [u8], &'a [u8]>(buf) }) | ||
} | ||
} |
This file contains 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.
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.
Given there is only a single creator of this future, is this
From
impl necessary? If it is, could we perhaps add a comment to explain why?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 wanted to make
reader
field private, but yeah, it could be an associated function, or just makereader
pub(crate)
.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.
@tirr-c This is not a big issue, but given a choice I'd prefer a
pub(crate)
field and a function in order to not leak any unintended APIs publicly. Thisimpl
is currently public in the sense that the user could do.into()
to convert a buffer into the future.