Skip to content

Commit

Permalink
Remove the meta field from LoadedAsset and ErasedLoadedAsset. (#…
Browse files Browse the repository at this point in the history
…15487)

# Objective

Fixes #15485.

## Solution

Deletes the field! The `meta` field had no way to access or mutate it.

## Testing

- It builds!

---

## Migration Guide

- `ErasedAssetLoader` now takes a borrow to `AssetMetaDyn` instead of a
`Box`.
- `LoadedAsset::new_with_dependencies` no longer requires a `meta`
argument.
- `LoadContext::finish` no longer requires a `meta` argument.
  • Loading branch information
andriyDev authored Dec 13, 2024
1 parent 72079cf commit 4ba47ed
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 40 deletions.
30 changes: 10 additions & 20 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait ErasedAssetLoader: Send + Sync + 'static {
fn load<'a>(
&'a self,
reader: &'a mut dyn Reader,
meta: Box<dyn AssetMetaDyn>,
meta: &'a dyn AssetMetaDyn,
load_context: LoadContext<'a>,
) -> BoxedFuture<
'a,
Expand Down Expand Up @@ -81,7 +81,7 @@ where
fn load<'a>(
&'a self,
reader: &'a mut dyn Reader,
meta: Box<dyn AssetMetaDyn>,
meta: &'a dyn AssetMetaDyn,
mut load_context: LoadContext<'a>,
) -> BoxedFuture<
'a,
Expand All @@ -96,7 +96,7 @@ where
let asset = <L as AssetLoader>::load(self, reader, settings, &mut load_context)
.await
.map_err(Into::into)?;
Ok(load_context.finish(asset, Some(meta)).into())
Ok(load_context.finish(asset).into())
})
}

Expand Down Expand Up @@ -147,12 +147,11 @@ pub struct LoadedAsset<A: Asset> {
pub(crate) dependencies: HashSet<UntypedAssetId>,
pub(crate) loader_dependencies: HashMap<AssetPath<'static>, AssetHash>,
pub(crate) labeled_assets: HashMap<CowArc<'static, str>, LabeledAsset>,
pub(crate) meta: Option<Box<dyn AssetMetaDyn>>,
}

impl<A: Asset> LoadedAsset<A> {
/// Create a new loaded asset. This will use [`VisitAssetDependencies`](crate::VisitAssetDependencies) to populate `dependencies`.
pub fn new_with_dependencies(value: A, meta: Option<Box<dyn AssetMetaDyn>>) -> Self {
pub fn new_with_dependencies(value: A) -> Self {
let mut dependencies = <HashSet<_>>::default();
value.visit_dependencies(&mut |id| {
dependencies.insert(id);
Expand All @@ -162,7 +161,6 @@ impl<A: Asset> LoadedAsset<A> {
dependencies,
loader_dependencies: HashMap::default(),
labeled_assets: HashMap::default(),
meta,
}
}

Expand Down Expand Up @@ -192,7 +190,7 @@ impl<A: Asset> LoadedAsset<A> {

impl<A: Asset> From<A> for LoadedAsset<A> {
fn from(asset: A) -> Self {
LoadedAsset::new_with_dependencies(asset, None)
LoadedAsset::new_with_dependencies(asset)
}
}

Expand All @@ -202,7 +200,6 @@ pub struct ErasedLoadedAsset {
pub(crate) dependencies: HashSet<UntypedAssetId>,
pub(crate) loader_dependencies: HashMap<AssetPath<'static>, AssetHash>,
pub(crate) labeled_assets: HashMap<CowArc<'static, str>, LabeledAsset>,
pub(crate) meta: Option<Box<dyn AssetMetaDyn>>,
}

impl<A: Asset> From<LoadedAsset<A>> for ErasedLoadedAsset {
Expand All @@ -212,7 +209,6 @@ impl<A: Asset> From<LoadedAsset<A>> for ErasedLoadedAsset {
dependencies: asset.dependencies,
loader_dependencies: asset.loader_dependencies,
labeled_assets: asset.labeled_assets,
meta: asset.meta,
}
}
}
Expand Down Expand Up @@ -254,15 +250,13 @@ impl ErasedLoadedAsset {

/// Cast this loaded asset as the given type. If the type does not match,
/// the original type-erased asset is returned.
#[expect(clippy::result_large_err, reason = "Function returns `Self` on error.")]
pub fn downcast<A: Asset>(mut self) -> Result<LoadedAsset<A>, ErasedLoadedAsset> {
match self.value.downcast::<A>() {
Ok(value) => Ok(LoadedAsset {
value: *value,
dependencies: self.dependencies,
loader_dependencies: self.loader_dependencies,
labeled_assets: self.labeled_assets,
meta: self.meta,
}),
Err(value) => {
self.value = value;
Expand Down Expand Up @@ -364,7 +358,7 @@ impl<'a> LoadContext<'a> {
/// for i in 0..2 {
/// let mut labeled = load_context.begin_labeled_asset();
/// handles.push(std::thread::spawn(move || {
/// (i.to_string(), labeled.finish(Image::default(), None))
/// (i.to_string(), labeled.finish(Image::default()))
/// }));
/// }
///
Expand Down Expand Up @@ -397,7 +391,7 @@ impl<'a> LoadContext<'a> {
) -> Handle<A> {
let mut context = self.begin_labeled_asset();
let asset = load(&mut context);
let loaded_asset = context.finish(asset, None);
let loaded_asset = context.finish(asset);
self.add_loaded_labeled_asset(label, loaded_asset)
}

Expand Down Expand Up @@ -451,13 +445,12 @@ impl<'a> LoadContext<'a> {

/// "Finishes" this context by populating the final [`Asset`] value (and the erased [`AssetMeta`] value, if it exists).
/// The relevant asset metadata collected in this context will be stored in the returned [`LoadedAsset`].
pub fn finish<A: Asset>(self, value: A, meta: Option<Box<dyn AssetMetaDyn>>) -> LoadedAsset<A> {
pub fn finish<A: Asset>(self, value: A) -> LoadedAsset<A> {
LoadedAsset {
value,
dependencies: self.dependencies,
loader_dependencies: self.loader_dependencies,
labeled_assets: self.labeled_assets,
meta,
}
}

Expand Down Expand Up @@ -524,7 +517,7 @@ impl<'a> LoadContext<'a> {
pub(crate) async fn load_direct_internal(
&mut self,
path: AssetPath<'static>,
meta: Box<dyn AssetMetaDyn>,
meta: &dyn AssetMetaDyn,
loader: &dyn ErasedAssetLoader,
reader: &mut dyn Reader,
) -> Result<ErasedLoadedAsset, LoadDirectError> {
Expand All @@ -543,10 +536,7 @@ impl<'a> LoadContext<'a> {
dependency: path.clone(),
error,
})?;
let info = loaded_asset
.meta
.as_ref()
.and_then(|m| m.processed_info().as_ref());
let info = meta.processed_info().as_ref();
let hash = info.map(|i| i.full_hash).unwrap_or_default();
self.loader_dependencies.insert(path, hash);
Ok(loaded_asset)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/loader_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ impl<'builder, 'reader, T> NestedLoader<'_, '_, T, Immediate<'builder, 'reader>>

let asset = self
.load_context
.load_direct_internal(path.clone(), meta, &*loader, reader.as_mut())
.load_direct_internal(path.clone(), meta.as_ref(), &*loader, reader.as_mut())
.await?;
Ok((loader, asset))
}
Expand Down
9 changes: 1 addition & 8 deletions crates/bevy_asset/src/processor/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,7 @@ impl<'a> ProcessContext<'a> {
let loader = server.get_asset_loader_with_type_name(loader_name).await?;
let mut reader = SliceReader::new(self.asset_bytes);
let loaded_asset = server
.load_with_meta_loader_and_reader(
self.path,
Box::new(meta),
&*loader,
&mut reader,
false,
true,
)
.load_with_meta_loader_and_reader(self.path, &meta, &*loader, &mut reader, false, true)
.await?;
for (path, full_hash) in &loaded_asset.loader_dependencies {
self.new_processed_info
Expand Down
23 changes: 13 additions & 10 deletions crates/bevy_asset/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,8 @@ impl AssetServer {
match server.load_untyped_async(path).await {
Ok(handle) => server.send_asset_event(InternalAssetEvent::Loaded {
id,
loaded_asset: LoadedAsset::new_with_dependencies(
LoadedUntypedAsset { handle },
None,
)
.into(),
loaded_asset: LoadedAsset::new_with_dependencies(LoadedUntypedAsset { handle })
.into(),
}),
Err(err) => {
error!("{err}");
Expand Down Expand Up @@ -647,7 +644,14 @@ impl AssetServer {
};

match self
.load_with_meta_loader_and_reader(&base_path, meta, &*loader, &mut *reader, true, false)
.load_with_meta_loader_and_reader(
&base_path,
meta.as_ref(),
&*loader,
&mut *reader,
true,
false,
)
.await
{
Ok(loaded_asset) => {
Expand Down Expand Up @@ -735,7 +739,7 @@ impl AssetServer {
/// After the asset has been fully loaded by the [`AssetServer`], it will show up in the relevant [`Assets`] storage.
#[must_use = "not using the returned strong handle may result in the unexpected release of the asset"]
pub fn add<A: Asset>(&self, asset: A) -> Handle<A> {
self.load_asset(LoadedAsset::new_with_dependencies(asset, None))
self.load_asset(LoadedAsset::new_with_dependencies(asset))
}

pub(crate) fn load_asset<A: Asset>(&self, asset: impl Into<LoadedAsset<A>>) -> Handle<A> {
Expand Down Expand Up @@ -798,7 +802,7 @@ impl AssetServer {
let task = IoTaskPool::get().spawn(async move {
match future.await {
Ok(asset) => {
let loaded_asset = LoadedAsset::new_with_dependencies(asset, None).into();
let loaded_asset = LoadedAsset::new_with_dependencies(asset).into();
event_sender
.send(InternalAssetEvent::Loaded { id, loaded_asset })
.unwrap();
Expand Down Expand Up @@ -928,7 +932,6 @@ impl AssetServer {
id,
loaded_asset: LoadedAsset::new_with_dependencies(
LoadedFolder { handles },
None,
)
.into(),
}),
Expand Down Expand Up @@ -1314,7 +1317,7 @@ impl AssetServer {
pub(crate) async fn load_with_meta_loader_and_reader(
&self,
asset_path: &AssetPath<'_>,
meta: Box<dyn AssetMetaDyn>,
meta: &dyn AssetMetaDyn,
loader: &dyn ErasedAssetLoader,
reader: &mut dyn Reader,
load_dependencies: bool,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ async fn load_gltf<'a, 'b, 'c>(
joints: joint_entities,
});
}
let loaded_scene = scene_load_context.finish(Scene::new(world), None);
let loaded_scene = scene_load_context.finish(Scene::new(world));
let scene_handle = load_context.add_loaded_labeled_asset(scene_label(&scene), loaded_scene);

if let Some(name) = scene.name() {
Expand Down

0 comments on commit 4ba47ed

Please sign in to comment.