bind the labeled asset type to the actual loaded asset (#1363)

bind the labeled asset type to the actual loaded asset
This commit is contained in:
François 2021-01-31 22:54:15 +01:00 committed by GitHub
parent 4904080382
commit 83e30a841a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,15 +29,15 @@ impl<T> Asset for T where T: TypeUuid + AssetDynamic + TypeUuidDynamic {}
impl<T> AssetDynamic for T where T: Send + Sync + 'static + TypeUuidDynamic {} impl<T> AssetDynamic for T where T: Send + Sync + 'static + TypeUuidDynamic {}
pub struct LoadedAsset { pub struct LoadedAsset<T: Asset> {
pub(crate) value: Option<Box<dyn AssetDynamic>>, pub(crate) value: Option<T>,
pub(crate) dependencies: Vec<AssetPath<'static>>, pub(crate) dependencies: Vec<AssetPath<'static>>,
} }
impl LoadedAsset { impl<T: Asset> LoadedAsset<T> {
pub fn new<T: Asset>(value: T) -> Self { pub fn new(value: T) -> Self {
Self { Self {
value: Some(Box::new(value)), value: Some(value),
dependencies: Vec::new(), dependencies: Vec::new(),
} }
} }
@ -53,10 +53,27 @@ impl LoadedAsset {
} }
} }
pub(crate) struct BoxedLoadedAsset {
pub(crate) value: Option<Box<dyn AssetDynamic>>,
pub(crate) dependencies: Vec<AssetPath<'static>>,
}
impl<T: Asset> From<LoadedAsset<T>> for BoxedLoadedAsset {
fn from(asset: LoadedAsset<T>) -> Self {
BoxedLoadedAsset {
value: match asset.value {
Some(value) => Some(Box::new(value)),
None => None,
},
dependencies: asset.dependencies,
}
}
}
pub struct LoadContext<'a> { pub struct LoadContext<'a> {
pub(crate) ref_change_channel: &'a RefChangeChannel, pub(crate) ref_change_channel: &'a RefChangeChannel,
pub(crate) asset_io: &'a dyn AssetIo, pub(crate) asset_io: &'a dyn AssetIo,
pub(crate) labeled_assets: HashMap<Option<String>, LoadedAsset>, pub(crate) labeled_assets: HashMap<Option<String>, BoxedLoadedAsset>,
pub(crate) path: &'a Path, pub(crate) path: &'a Path,
pub(crate) version: usize, pub(crate) version: usize,
} }
@ -85,13 +102,14 @@ impl<'a> LoadContext<'a> {
self.labeled_assets.contains_key(&Some(label.to_string())) self.labeled_assets.contains_key(&Some(label.to_string()))
} }
pub fn set_default_asset(&mut self, asset: LoadedAsset) { pub fn set_default_asset<T: Asset>(&mut self, asset: LoadedAsset<T>) {
self.labeled_assets.insert(None, asset); self.labeled_assets.insert(None, asset.into());
} }
pub fn set_labeled_asset<T: Asset>(&mut self, label: &str, asset: LoadedAsset) -> Handle<T> { pub fn set_labeled_asset<T: Asset>(&mut self, label: &str, asset: LoadedAsset<T>) -> Handle<T> {
assert!(!label.is_empty()); assert!(!label.is_empty());
self.labeled_assets.insert(Some(label.to_string()), asset); self.labeled_assets
.insert(Some(label.to_string()), asset.into());
self.get_handle(AssetPath::new_ref(self.path(), Some(label))) self.get_handle(AssetPath::new_ref(self.path(), Some(label)))
} }