Skip to content

Commit

Permalink
bind the labeled asset type to the actual loaded asset (#1363)
Browse files Browse the repository at this point in the history
bind the labeled asset type to the actual loaded asset
  • Loading branch information
mockersf authored Jan 31, 2021
1 parent 4904080 commit 83e30a8
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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)))
}

Expand Down

0 comments on commit 83e30a8

Please sign in to comment.