mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-22 05:28:42 +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,
|
||||
}
|
||||
|
||||
type FetcherJoinHandle = AutoAbortJoinHandle<Result<bool, BinstallError>>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MultiFetcher {
|
||||
fetchers: Vec<Arc<dyn Fetcher>>,
|
||||
}
|
||||
pub struct MultiFetcher(Vec<(Arc<dyn Fetcher>, FetcherJoinHandle)>);
|
||||
|
||||
impl MultiFetcher {
|
||||
pub fn add(&mut self, fetcher: Arc<dyn Fetcher>) {
|
||||
self.fetchers.push(fetcher);
|
||||
self.0.push((
|
||||
fetcher.clone(),
|
||||
AutoAbortJoinHandle::new(tokio::spawn(async move { fetcher.check().await })),
|
||||
));
|
||||
}
|
||||
|
||||
pub async fn first_available(&self) -> Option<Arc<dyn Fetcher>> {
|
||||
let handles: Vec<_> = self
|
||||
.fetchers
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|fetcher| {
|
||||
(
|
||||
fetcher.clone(),
|
||||
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 {
|
||||
Ok(Ok(true)) => return Some(fetcher),
|
||||
Ok(Ok(false)) => (),
|
||||
|
|
Loading…
Add table
Reference in a new issue