opt: Use blocking::Task instead of boxing unblock()

This saves a heap allocation.

Signed-off-by: John Nunley <dev@notgull.net>
This commit is contained in:
John Nunley 2023-09-17 10:20:05 -07:00 committed by GitHub
parent de5105bce4
commit c971597872
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 5 deletions

View File

@ -48,9 +48,10 @@ use std::os::windows::fs::OpenOptionsExt as _;
use async_lock::Mutex;
use blocking::{unblock, Unblock};
use futures_lite::future::FutureExt;
use futures_lite::io::{AsyncRead, AsyncSeek, AsyncWrite, AsyncWriteExt};
use futures_lite::ready;
use futures_lite::stream::Stream;
use futures_lite::{future, ready};
#[doc(no_inline)]
pub use std::fs::{FileType, Metadata, Permissions};
@ -282,7 +283,7 @@ pub struct ReadDir(State);
/// The `ReadDir` can be either idle or busy performing an asynchronous operation.
enum State {
Idle(Option<std::fs::ReadDir>),
Busy(future::Boxed<(std::fs::ReadDir, Option<io::Result<std::fs::DirEntry>>)>),
Busy(blocking::Task<(std::fs::ReadDir, Option<io::Result<std::fs::DirEntry>>)>),
}
impl fmt::Debug for ReadDir {
@ -301,14 +302,14 @@ impl Stream for ReadDir {
let mut inner = opt.take().unwrap();
// Start the operation asynchronously.
self.0 = State::Busy(Box::pin(unblock(move || {
self.0 = State::Busy(unblock(move || {
let next = inner.next();
(inner, next)
})));
}));
}
// Poll the asynchronous operation the file is currently blocked on.
State::Busy(task) => {
let (inner, opt) = ready!(task.as_mut().poll(cx));
let (inner, opt) = ready!(task.poll(cx));
self.0 = State::Idle(Some(inner));
return Poll::Ready(opt.map(|res| res.map(|inner| DirEntry(Arc::new(inner)))));
}