mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-22 13:38:43 +00:00
Optimize MultiFetcher
: Start check
ing ASAP
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
6180e9ec3e
commit
dc8d8ccd88
1 changed files with 9 additions and 18 deletions
|
@ -47,30 +47,21 @@ pub struct Data {
|
||||||
pub meta: PkgMeta,
|
pub meta: PkgMeta,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FetcherJoinHandle = AutoAbortJoinHandle<Result<bool, BinstallError>>;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MultiFetcher {
|
pub struct MultiFetcher(Vec<(Arc<dyn Fetcher>, FetcherJoinHandle)>);
|
||||||
fetchers: Vec<Arc<dyn Fetcher>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MultiFetcher {
|
impl MultiFetcher {
|
||||||
pub fn add(&mut self, fetcher: Arc<dyn Fetcher>) {
|
pub fn add(&mut self, fetcher: Arc<dyn Fetcher>) {
|
||||||
self.fetchers.push(fetcher);
|
self.0.push((
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn first_available(&self) -> Option<Arc<dyn Fetcher>> {
|
|
||||||
let handles: Vec<_> = self
|
|
||||||
.fetchers
|
|
||||||
.iter()
|
|
||||||
.cloned()
|
|
||||||
.map(|fetcher| {
|
|
||||||
(
|
|
||||||
fetcher.clone(),
|
fetcher.clone(),
|
||||||
AutoAbortJoinHandle::new(tokio::spawn(async move { fetcher.check().await })),
|
AutoAbortJoinHandle::new(tokio::spawn(async move { fetcher.check().await })),
|
||||||
)
|
));
|
||||||
})
|
}
|
||||||
.collect();
|
|
||||||
|
|
||||||
for (fetcher, handle) in handles {
|
pub async fn first_available(self) -> Option<Arc<dyn Fetcher>> {
|
||||||
|
for (fetcher, handle) in self.0 {
|
||||||
match handle.await {
|
match handle.await {
|
||||||
Ok(Ok(true)) => return Some(fetcher),
|
Ok(Ok(true)) => return Some(fetcher),
|
||||||
Ok(Ok(false)) => (),
|
Ok(Ok(false)) => (),
|
||||||
|
|
Loading…
Add table
Reference in a new issue