Skip to content

Use blocking::Task instead of boxing unblock() #23

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

Merged
merged 1 commit into from
Sep 17, 2023
Merged
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
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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)))));
}
Expand Down