-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Enable buffering both reads and writes #1558
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8269b9a
Enable buffering both reads and writes
jonhoo bb3d010
nit
jonhoo e9e6678
Make io::Buf* types accessible through Ext traits
jonhoo 8b20c84
Move BufStream to newtype
jonhoo 01c9595
Merge branch 'master' into jonhoo/asyncbufstream
jonhoo 7f4303e
Move buffered() to AsyncReadExt
jonhoo 6b6e384
Remove extension trait methods for Buf*
jonhoo 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
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
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
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
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,71 @@ | ||
use crate::io::{BufReader, BufWriter}; | ||
use crate::{AsyncBufRead, AsyncRead, AsyncWrite}; | ||
use pin_project::pin_project; | ||
use std::io::{self}; | ||
use std::{ | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
/// Wraps a type that is [`AsyncWrite`] and [`AsyncRead`], and buffers its input and output. | ||
/// | ||
/// It can be excessively inefficient to work directly with something that implements [`AsyncWrite`] | ||
/// and [`AsyncRead`]. For example, every `write`, however small, has to traverse the syscall | ||
/// interface, and similarly, every read has to do the same. The [`BufWriter`] and [`BufReader`] | ||
/// types aid with these problems respectively, but do so in only one direction. `BufStream` wraps | ||
/// one in the other so that both directions are buffered. See their documentation for details. | ||
#[pin_project] | ||
#[derive(Debug)] | ||
pub struct BufStream<RW: AsyncRead + AsyncWrite>(#[pin] BufReader<BufWriter<RW>>); | ||
|
||
impl<RW: AsyncRead + AsyncWrite> BufStream<RW> { | ||
/// Wrap a type in both [`BufWriter`] and [`BufReader`]. | ||
/// | ||
/// See the documentation for those types and [`BufStream`] for details. | ||
pub fn new(stream: RW) -> BufStream<RW> { | ||
BufStream(BufReader::new(BufWriter::new(stream))) | ||
} | ||
} | ||
|
||
impl<RW: AsyncRead + AsyncWrite> AsyncWrite for BufStream<RW> { | ||
fn poll_write( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<io::Result<usize>> { | ||
self.project().0.poll_write(cx, buf) | ||
} | ||
|
||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
self.project().0.poll_flush(cx) | ||
} | ||
|
||
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
self.project().0.poll_shutdown(cx) | ||
} | ||
} | ||
|
||
impl<RW: AsyncRead + AsyncWrite> AsyncRead for BufStream<RW> { | ||
fn poll_read( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut [u8], | ||
) -> Poll<io::Result<usize>> { | ||
self.project().0.poll_read(cx, buf) | ||
} | ||
|
||
// we can't skip unconditionally because of the large buffer case in read. | ||
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool { | ||
self.0.prepare_uninitialized_buffer(buf) | ||
} | ||
} | ||
|
||
impl<RW: AsyncBufRead + AsyncRead + AsyncWrite> AsyncBufRead for BufStream<RW> { | ||
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> { | ||
self.project_into().0.poll_fill_buf(cx) | ||
} | ||
|
||
fn consume(mut self: Pin<&mut Self>, amt: usize) { | ||
self.project().0.consume(amt) | ||
} | ||
} |
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
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
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.
In the past, fns that require both
AsyncRead + AsyncWrite
have been defined on the read half.