Impl AutoAbortJoinHandle::new & make its field private

plus change all its users to use its new APIs.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-08 20:53:49 +10:00
parent f41391a53c
commit d9bcca8b78
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 12 additions and 6 deletions

View file

@ -64,13 +64,13 @@ impl MultiFetcher {
.map(|fetcher| { .map(|fetcher| {
( (
fetcher.clone(), fetcher.clone(),
AutoAbortJoinHandle(tokio::spawn(async move { fetcher.check().await })), AutoAbortJoinHandle::new(tokio::spawn(async move { fetcher.check().await })),
) )
}) })
.collect(); .collect();
for (fetcher, mut handle) in handles { for (fetcher, handle) in handles {
match (&mut handle.0).await { match handle.await {
Ok(Ok(true)) => return Some(fetcher), Ok(Ok(true)) => return Some(fetcher),
Ok(Ok(false)) => (), Ok(Ok(false)) => (),
Ok(Err(err)) => { Ok(Err(err)) => {

View file

@ -246,7 +246,7 @@ impl AsyncFileWriter {
let mut file = fs::File::create(path)?; let mut file = fs::File::create(path)?;
let (tx, mut rx) = mpsc::channel::<Bytes>(100); let (tx, mut rx) = mpsc::channel::<Bytes>(100);
let handle = AutoAbortJoinHandle(task::spawn_blocking(move || { let handle = AutoAbortJoinHandle::new(task::spawn_blocking(move || {
while let Some(bytes) = rx.blocking_recv() { while let Some(bytes) = rx.blocking_recv() {
file.write_all(&*bytes)?; file.write_all(&*bytes)?;
} }
@ -300,7 +300,7 @@ impl AsyncFileWriter {
} }
async fn wait(handle: &mut AutoAbortJoinHandle<io::Result<()>>) -> io::Result<()> { async fn wait(handle: &mut AutoAbortJoinHandle<io::Result<()>>) -> io::Result<()> {
match (&mut handle.0).await { match handle.await {
Ok(res) => res, Ok(res) => res,
Err(join_err) => Err(io::Error::new(io::ErrorKind::Other, join_err)), Err(join_err) => Err(io::Error::new(io::ErrorKind::Other, join_err)),
} }
@ -308,7 +308,13 @@ impl AsyncFileWriter {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct AutoAbortJoinHandle<T>(pub task::JoinHandle<T>); pub struct AutoAbortJoinHandle<T>(task::JoinHandle<T>);
impl<T> AutoAbortJoinHandle<T> {
pub fn new(handle: task::JoinHandle<T>) -> Self {
Self(handle)
}
}
impl<T> Drop for AutoAbortJoinHandle<T> { impl<T> Drop for AutoAbortJoinHandle<T> {
fn drop(&mut self) { fn drop(&mut self) {