mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 13:08:42 +00:00
Refactor: Extract AutoAbortJoinHandle
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
358bea5c6d
commit
5d70f61317
2 changed files with 49 additions and 44 deletions
|
@ -1,14 +1,9 @@
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
future::Future,
|
io::{stderr, stdin, Write},
|
||||||
io::{self, stderr, stdin, Write},
|
|
||||||
ops::{Deref, DerefMut},
|
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
pin::Pin,
|
|
||||||
task::{Context, Poll},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use bytes::Bytes;
|
|
||||||
use cargo_toml::Manifest;
|
use cargo_toml::Manifest;
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use futures_util::stream::StreamExt;
|
use futures_util::stream::StreamExt;
|
||||||
|
@ -18,7 +13,6 @@ use scopeguard::ScopeGuard;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tar::Archive;
|
use tar::Archive;
|
||||||
use tinytemplate::TinyTemplate;
|
use tinytemplate::TinyTemplate;
|
||||||
use tokio::{sync::mpsc, task};
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use xz2::read::XzDecoder;
|
use xz2::read::XzDecoder;
|
||||||
use zip::read::ZipArchive;
|
use zip::read::ZipArchive;
|
||||||
|
@ -29,6 +23,9 @@ use crate::{BinstallError, Meta, PkgFmt};
|
||||||
mod async_file_writer;
|
mod async_file_writer;
|
||||||
pub use async_file_writer::AsyncFileWriter;
|
pub use async_file_writer::AsyncFileWriter;
|
||||||
|
|
||||||
|
mod auto_abort_join_handle;
|
||||||
|
pub use auto_abort_join_handle::AutoAbortJoinHandle;
|
||||||
|
|
||||||
/// Load binstall metadata from the crate `Cargo.toml` at the provided path
|
/// Load binstall metadata from the crate `Cargo.toml` at the provided path
|
||||||
pub fn load_manifest_path<P: AsRef<Path>>(
|
pub fn load_manifest_path<P: AsRef<Path>>(
|
||||||
manifest_path: P,
|
manifest_path: P,
|
||||||
|
@ -233,40 +230,3 @@ pub trait Template: Serialize {
|
||||||
Ok(tt.render("path", self)?)
|
Ok(tt.render("path", self)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
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> {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.0.abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Deref for AutoAbortJoinHandle<T> {
|
|
||||||
type Target = task::JoinHandle<T>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> DerefMut for AutoAbortJoinHandle<T> {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Future for AutoAbortJoinHandle<T> {
|
|
||||||
type Output = Result<T, task::JoinError>;
|
|
||||||
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
Pin::new(&mut Pin::into_inner(self).0).poll(cx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
45
src/helpers/auto_abort_join_handle.rs
Normal file
45
src/helpers/auto_abort_join_handle.rs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
use std::{
|
||||||
|
future::Future,
|
||||||
|
ops::{Deref, DerefMut},
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
|
||||||
|
use tokio::task::{JoinError, JoinHandle};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct AutoAbortJoinHandle<T>(JoinHandle<T>);
|
||||||
|
|
||||||
|
impl<T> AutoAbortJoinHandle<T> {
|
||||||
|
pub fn new(handle: JoinHandle<T>) -> Self {
|
||||||
|
Self(handle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Drop for AutoAbortJoinHandle<T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.0.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Deref for AutoAbortJoinHandle<T> {
|
||||||
|
type Target = JoinHandle<T>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> DerefMut for AutoAbortJoinHandle<T> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Future for AutoAbortJoinHandle<T> {
|
||||||
|
type Output = Result<T, JoinError>;
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
Pin::new(&mut Pin::into_inner(self).0).poll(cx)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue