Skip to content

Commit 8b199ea

Browse files
authored
Merge pull request #15 from smol-rs/taiki-e/box
Remove boxed futures from TcpStream and UnixStream
2 parents fcef0a0 + 1e8799c commit 8b199ea

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ keywords = ["networking", "uds", "mio", "reactor", "std"]
1515
categories = ["asynchronous", "network-programming", "os"]
1616

1717
[dependencies]
18-
async-io = "1.0.0"
18+
async-io = "1.5.0"
1919
blocking = "1.0.0"
2020
fastrand = "1.3.5"
2121
futures-lite = "1.11.0"

src/tcp.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ impl fmt::Debug for Incoming<'_> {
303303
/// ```
304304
pub struct TcpStream {
305305
inner: Arc<Async<std::net::TcpStream>>,
306-
readable: Option<Pin<Box<dyn Future<Output = io::Result<()>> + Send + Sync>>>,
307-
writable: Option<Pin<Box<dyn Future<Output = io::Result<()>> + Send + Sync>>>,
306+
readable: Option<async_io::Readable>,
307+
writable: Option<async_io::Writable>,
308308
}
309309

310310
impl UnwindSafe for TcpStream {}
@@ -599,13 +599,12 @@ impl AsyncRead for TcpStream {
599599

600600
// Initialize the future to wait for readiness.
601601
if self.readable.is_none() {
602-
let inner = self.inner.clone();
603-
self.readable = Some(Box::pin(async move { inner.readable().await }));
602+
self.readable = Some(self.inner.readable());
604603
}
605604

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

632631
// Initialize the future to wait for readiness.
633632
if self.writable.is_none() {
634-
let inner = self.inner.clone();
635-
self.writable = Some(Box::pin(async move { inner.writable().await }));
633+
self.writable = Some(self.inner.writable());
636634
}
637635

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

658656
// Initialize the future to wait for readiness.
659657
if self.writable.is_none() {
660-
let inner = self.inner.clone();
661-
self.writable = Some(Box::pin(async move { inner.writable().await }));
658+
self.writable = Some(self.inner.writable());
662659
}
663660

664661
// Poll the future for readiness.
665662
if let Some(f) = &mut self.writable {
666-
let res = ready!(f.as_mut().poll(cx));
663+
let res = ready!(Pin::new(f).poll(cx));
667664
self.writable = None;
668665
res?;
669666
}
@@ -691,13 +688,12 @@ impl AsyncWrite for TcpStream {
691688

692689
// Initialize the future to wait for readiness.
693690
if self.writable.is_none() {
694-
let inner = self.inner.clone();
695-
self.writable = Some(Box::pin(async move { inner.writable().await }));
691+
self.writable = Some(self.inner.writable());
696692
}
697693

698694
// Poll the future for readiness.
699695
if let Some(f) = &mut self.writable {
700-
let res = ready!(f.as_mut().poll(cx));
696+
let res = ready!(Pin::new(f).poll(cx));
701697
self.writable = None;
702698
res?;
703699
}

src/unix.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ impl fmt::Debug for Incoming<'_> {
234234
/// ```
235235
pub struct UnixStream {
236236
inner: Arc<Async<std::os::unix::net::UnixStream>>,
237-
readable: Option<Pin<Box<dyn Future<Output = io::Result<()>> + Send + Sync>>>,
238-
writable: Option<Pin<Box<dyn Future<Output = io::Result<()>> + Send + Sync>>>,
237+
readable: Option<async_io::Readable>,
238+
writable: Option<async_io::Writable>,
239239
}
240240

241241
impl UnwindSafe for UnixStream {}
@@ -396,13 +396,12 @@ impl AsyncRead for UnixStream {
396396

397397
// Initialize the future to wait for readiness.
398398
if self.readable.is_none() {
399-
let inner = self.inner.clone();
400-
self.readable = Some(Box::pin(async move { inner.readable().await }));
399+
self.readable = Some(self.inner.readable());
401400
}
402401

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

429428
// Initialize the future to wait for readiness.
430429
if self.writable.is_none() {
431-
let inner = self.inner.clone();
432-
self.writable = Some(Box::pin(async move { inner.writable().await }));
430+
self.writable = Some(self.inner.writable());
433431
}
434432

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

455453
// Initialize the future to wait for readiness.
456454
if self.writable.is_none() {
457-
let inner = self.inner.clone();
458-
self.writable = Some(Box::pin(async move { inner.writable().await }));
455+
self.writable = Some(self.inner.writable());
459456
}
460457

461458
// Poll the future for readiness.
462459
if let Some(f) = &mut self.writable {
463-
let res = ready!(f.as_mut().poll(cx));
460+
let res = ready!(Pin::new(f).poll(cx));
464461
self.writable = None;
465462
res?;
466463
}

0 commit comments

Comments
 (0)