make loader clonable

This commit is contained in:
Robin Appelman 2023-12-26 17:14:42 +01:00
commit 01cd28ec09
3 changed files with 10 additions and 8 deletions

2
Cargo.lock generated
View file

@ -613,7 +613,7 @@ dependencies = [
[[package]] [[package]]
name = "tf-asset-loader" name = "tf-asset-loader"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"steamlocate", "steamlocate",
"thiserror", "thiserror",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "tf-asset-loader" name = "tf-asset-loader"
version = "0.1.2" version = "0.1.3"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
description = "Utility for loading assets from tf2 data files" description = "Utility for loading assets from tf2 data files"

View file

@ -4,6 +4,7 @@ pub use source::AssetSource;
use std::env::var_os; use std::env::var_os;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc;
use steamlocate::SteamDir; use steamlocate::SteamDir;
use thiserror::Error; use thiserror::Error;
use tracing::warn; use tracing::warn;
@ -21,8 +22,9 @@ pub enum LoaderError {
Other(String), Other(String),
} }
#[derive(Clone)]
pub struct Loader { pub struct Loader {
sources: Vec<Box<dyn AssetSource>>, sources: Vec<Arc<dyn AssetSource>>,
} }
impl Debug for Loader { impl Debug for Loader {
@ -56,13 +58,13 @@ impl Loader {
} }
res.ok() res.ok()
}) })
.map(|vpk| Box::new(vpk) as Box<dyn AssetSource>); .map(|vpk| Arc::new(vpk) as Arc<dyn AssetSource>);
#[allow(unused_mut)] #[allow(unused_mut)]
let mut sources = vec![ let mut sources = vec![
Box::new(tf_dir) as Box<dyn AssetSource>, Arc::new(tf_dir) as Arc<dyn AssetSource>,
Box::new(download), Arc::new(download),
Box::new(hl_dir), Arc::new(hl_dir),
]; ];
#[cfg(feature = "vpk")] #[cfg(feature = "vpk")]
@ -72,7 +74,7 @@ impl Loader {
} }
pub fn add_source<S: AssetSource + 'static>(&mut self, source: S) { pub fn add_source<S: AssetSource + 'static>(&mut self, source: S) {
self.sources.push(Box::new(source)) self.sources.push(Arc::new(source))
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]