Mod trait Fetcher::new to return Arc<Self>

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-07 15:16:10 +10:00
parent 0c83d010b1
commit 31b7439a69
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
3 changed files with 10 additions and 7 deletions

View file

@ -1,4 +1,5 @@
use std::path::Path; use std::path::Path;
use std::sync::Arc;
pub use gh_crate_meta::*; pub use gh_crate_meta::*;
pub use log::debug; pub use log::debug;
@ -12,7 +13,7 @@ mod quickinstall;
#[async_trait::async_trait] #[async_trait::async_trait]
pub trait Fetcher { pub trait Fetcher {
/// Create a new fetcher from some data /// Create a new fetcher from some data
async fn new(data: &Data) -> Box<Self> async fn new(data: &Data) -> Arc<Self>
where where
Self: Sized; Self: Sized;
@ -44,11 +45,11 @@ pub struct Data {
#[derive(Default)] #[derive(Default)]
pub struct MultiFetcher { pub struct MultiFetcher {
fetchers: Vec<Box<dyn Fetcher>>, fetchers: Vec<Arc<dyn Fetcher>>,
} }
impl MultiFetcher { impl MultiFetcher {
pub fn add(&mut self, fetcher: Box<dyn Fetcher>) { pub fn add(&mut self, fetcher: Arc<dyn Fetcher>) {
self.fetchers.push(fetcher); self.fetchers.push(fetcher);
} }

View file

@ -1,4 +1,5 @@
use std::path::Path; use std::path::Path;
use std::sync::Arc;
use log::{debug, info, warn}; use log::{debug, info, warn};
use reqwest::Method; use reqwest::Method;
@ -22,8 +23,8 @@ impl GhCrateMeta {
#[async_trait::async_trait] #[async_trait::async_trait]
impl super::Fetcher for GhCrateMeta { impl super::Fetcher for GhCrateMeta {
async fn new(data: &Data) -> Box<Self> { async fn new(data: &Data) -> Arc<Self> {
Box::new(Self { data: data.clone() }) Arc::new(Self { data: data.clone() })
} }
async fn check(&self) -> Result<bool, BinstallError> { async fn check(&self) -> Result<bool, BinstallError> {

View file

@ -1,4 +1,5 @@
use std::path::Path; use std::path::Path;
use std::sync::Arc;
use log::info; use log::info;
use reqwest::Method; use reqwest::Method;
@ -17,11 +18,11 @@ pub struct QuickInstall {
#[async_trait::async_trait] #[async_trait::async_trait]
impl super::Fetcher for QuickInstall { impl super::Fetcher for QuickInstall {
async fn new(data: &Data) -> Box<Self> { async fn new(data: &Data) -> Arc<Self> {
let crate_name = &data.name; let crate_name = &data.name;
let version = &data.version; let version = &data.version;
let target = &data.target; let target = &data.target;
Box::new(Self { Arc::new(Self {
package: format!("{crate_name}-{version}-{target}"), package: format!("{crate_name}-{version}-{target}"),
}) })
} }