Skip to content

Commit c971597

Browse files
authored
opt: Use blocking::Task instead of boxing unblock()
This saves a heap allocation. Signed-off-by: John Nunley <[email protected]>
1 parent de5105b commit c971597

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ use std::os::windows::fs::OpenOptionsExt as _;
4848

4949
use async_lock::Mutex;
5050
use blocking::{unblock, Unblock};
51+
use futures_lite::future::FutureExt;
5152
use futures_lite::io::{AsyncRead, AsyncSeek, AsyncWrite, AsyncWriteExt};
53+
use futures_lite::ready;
5254
use futures_lite::stream::Stream;
53-
use futures_lite::{future, ready};
5455

5556
#[doc(no_inline)]
5657
pub use std::fs::{FileType, Metadata, Permissions};
@@ -282,7 +283,7 @@ pub struct ReadDir(State);
282283
/// The `ReadDir` can be either idle or busy performing an asynchronous operation.
283284
enum State {
284285
Idle(Option<std::fs::ReadDir>),
285-
Busy(future::Boxed<(std::fs::ReadDir, Option<io::Result<std::fs::DirEntry>>)>),
286+
Busy(blocking::Task<(std::fs::ReadDir, Option<io::Result<std::fs::DirEntry>>)>),
286287
}
287288

288289
impl fmt::Debug for ReadDir {
@@ -301,14 +302,14 @@ impl Stream for ReadDir {
301302
let mut inner = opt.take().unwrap();
302303

303304
// Start the operation asynchronously.
304-
self.0 = State::Busy(Box::pin(unblock(move || {
305+
self.0 = State::Busy(unblock(move || {
305306
let next = inner.next();
306307
(inner, next)
307-
})));
308+
}));
308309
}
309310
// Poll the asynchronous operation the file is currently blocked on.
310311
State::Busy(task) => {
311-
let (inner, opt) = ready!(task.as_mut().poll(cx));
312+
let (inner, opt) = ready!(task.poll(cx));
312313
self.0 = State::Idle(Some(inner));
313314
return Poll::Ready(opt.map(|res| res.map(|inner| DirEntry(Arc::new(inner)))));
314315
}

0 commit comments

Comments
 (0)