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 {}
pub struct LoadedAsset {
pub(crate) value: Option<Box<dyn AssetDynamic>>,
pub struct LoadedAsset<T: Asset> {
pub(crate) value: Option<T>,
pub(crate) dependencies: Vec<AssetPath<'static>>,
}
impl LoadedAsset {
pub fn new<T: Asset>(value: T) -> Self {
impl<T: Asset> LoadedAsset<T> {
pub fn new(value: T) -> Self {
Self {
value: Some(Box::new(value)),
value: Some(value),
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(crate) ref_change_channel: &'a RefChangeChannel,
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) version: usize,
}
@ -85,13 +102,14 @@ impl<'a> LoadContext<'a> {
self.labeled_assets.contains_key(&Some(label.to_string()))
}
pub fn set_default_asset(&mut self, asset: LoadedAsset) {
self.labeled_assets.insert(None, asset);
pub fn set_default_asset<T: Asset>(&mut self, asset: LoadedAsset<T>) {
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());
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)))
}