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

Remove boxed futures from TcpStream and UnixStream #15

Merged
merged 1 commit into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ keywords = ["networking", "uds", "mio", "reactor", "std"]
categories = ["asynchronous", "network-programming", "os"]

[dependencies]
async-io = "1.0.0"
async-io = "1.5.0"
blocking = "1.0.0"
fastrand = "1.3.5"
futures-lite = "1.11.0"
24 changes: 10 additions & 14 deletions src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ impl fmt::Debug for Incoming<'_> {
/// ```
pub struct TcpStream {
inner: Arc<Async<std::net::TcpStream>>,
readable: Option<Pin<Box<dyn Future<Output = io::Result<()>> + Send + Sync>>>,
writable: Option<Pin<Box<dyn Future<Output = io::Result<()>> + Send + Sync>>>,
readable: Option<async_io::Readable>,
writable: Option<async_io::Writable>,
}

impl UnwindSafe for TcpStream {}
Expand Down Expand Up @@ -599,13 +599,12 @@ impl AsyncRead for TcpStream {

// Initialize the future to wait for readiness.
if self.readable.is_none() {
let inner = self.inner.clone();
self.readable = Some(Box::pin(async move { inner.readable().await }));
self.readable = Some(self.inner.readable());
}

// Poll the future for readiness.
if let Some(f) = &mut self.readable {
let res = ready!(f.as_mut().poll(cx));
let res = ready!(Pin::new(f).poll(cx));
self.readable = None;
res?;
}
Expand All @@ -631,13 +630,12 @@ impl AsyncWrite for TcpStream {

// Initialize the future to wait for readiness.
if self.writable.is_none() {
let inner = self.inner.clone();
self.writable = Some(Box::pin(async move { inner.writable().await }));
self.writable = Some(self.inner.writable());
}

// Poll the future for readiness.
if let Some(f) = &mut self.writable {
let res = ready!(f.as_mut().poll(cx));
let res = ready!(Pin::new(f).poll(cx));
self.writable = None;
res?;
}
Expand All @@ -657,13 +655,12 @@ impl AsyncWrite for TcpStream {

// Initialize the future to wait for readiness.
if self.writable.is_none() {
let inner = self.inner.clone();
self.writable = Some(Box::pin(async move { inner.writable().await }));
self.writable = Some(self.inner.writable());
}

// Poll the future for readiness.
if let Some(f) = &mut self.writable {
let res = ready!(f.as_mut().poll(cx));
let res = ready!(Pin::new(f).poll(cx));
self.writable = None;
res?;
}
Expand Down Expand Up @@ -691,13 +688,12 @@ impl AsyncWrite for TcpStream {

// Initialize the future to wait for readiness.
if self.writable.is_none() {
let inner = self.inner.clone();
self.writable = Some(Box::pin(async move { inner.writable().await }));
self.writable = Some(self.inner.writable());
}

// Poll the future for readiness.
if let Some(f) = &mut self.writable {
let res = ready!(f.as_mut().poll(cx));
let res = ready!(Pin::new(f).poll(cx));
self.writable = None;
res?;
}
Expand Down
19 changes: 8 additions & 11 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ impl fmt::Debug for Incoming<'_> {
/// ```
pub struct UnixStream {
inner: Arc<Async<std::os::unix::net::UnixStream>>,
readable: Option<Pin<Box<dyn Future<Output = io::Result<()>> + Send + Sync>>>,
writable: Option<Pin<Box<dyn Future<Output = io::Result<()>> + Send + Sync>>>,
readable: Option<async_io::Readable>,
writable: Option<async_io::Writable>,
}

impl UnwindSafe for UnixStream {}
Expand Down Expand Up @@ -396,13 +396,12 @@ impl AsyncRead for UnixStream {

// Initialize the future to wait for readiness.
if self.readable.is_none() {
let inner = self.inner.clone();
self.readable = Some(Box::pin(async move { inner.readable().await }));
self.readable = Some(self.inner.readable());
}

// Poll the future for readiness.
if let Some(f) = &mut self.readable {
let res = ready!(f.as_mut().poll(cx));
let res = ready!(Pin::new(f).poll(cx));
self.readable = None;
res?;
}
Expand All @@ -428,13 +427,12 @@ impl AsyncWrite for UnixStream {

// Initialize the future to wait for readiness.
if self.writable.is_none() {
let inner = self.inner.clone();
self.writable = Some(Box::pin(async move { inner.writable().await }));
self.writable = Some(self.inner.writable());
}

// Poll the future for readiness.
if let Some(f) = &mut self.writable {
let res = ready!(f.as_mut().poll(cx));
let res = ready!(Pin::new(f).poll(cx));
self.writable = None;
res?;
}
Expand All @@ -454,13 +452,12 @@ impl AsyncWrite for UnixStream {

// Initialize the future to wait for readiness.
if self.writable.is_none() {
let inner = self.inner.clone();
self.writable = Some(Box::pin(async move { inner.writable().await }));
self.writable = Some(self.inner.writable());
}

// Poll the future for readiness.
if let Some(f) = &mut self.writable {
let res = ready!(f.as_mut().poll(cx));
let res = ready!(Pin::new(f).poll(cx));
self.writable = None;
res?;
}
Expand Down