summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-09-28 18:45:51 +0200
committerHampusM <hampus@hampusmat.com>2026-09-28 18:45:51 +0200
commite776925533d09a436210d51273da3994c685c40a (patch)
tree2a0d8dc9cbfc00fda76226b53a60d784848371aa
parent6c894036b84326347ded75519d9465c3673a779c (diff)
feat(engine): add Assets::get_mut fn
-rw-r--r--engine/src/asset.rs46
1 files changed, 38 insertions, 8 deletions
diff --git a/engine/src/asset.rs b/engine/src/asset.rs
index 04cb385..7aacc5b 100644
--- a/engine/src/asset.rs
+++ b/engine/src/asset.rs
@@ -13,7 +13,6 @@ use std::sync::mpsc::{
Receiver as MpscReceiver,
Sender as MpscSender,
};
-use std::sync::Arc;
use ecs::actions::Actions;
@@ -170,7 +169,40 @@ impl Assets
unreachable!();
};
- let Some(asset) = stored_asset.strong.downcast_ref::<AssetT>() else {
+ let Some(asset) = stored_asset.inner.downcast_ref::<AssetT>() else {
+ tracing::error!("Wrong asset type");
+ return None;
+ };
+
+ Some(asset)
+ }
+
+ #[tracing::instrument(
+ skip_all,
+ fields(asset_label, asset_type = %type_name::<AssetT>())
+ )]
+ pub fn get_mut<AssetT>(&mut self, handle: &Handle<AssetT>) -> Option<&mut AssetT>
+ where
+ AssetT: Asset,
+ {
+ let id = handle.id.into_valid()?;
+
+ let asset_lookup = self.asset_lookup.borrow();
+
+ let AssetState::Ready(asset_index, asset_label) =
+ &asset_lookup.get(&id.label_hash)?.state
+ else {
+ return None;
+ };
+
+ tracing::Span::current()
+ .record("asset_label", tracing::field::display(&asset_label));
+
+ let Some(stored_asset) = self.store.get_mut(*asset_index) else {
+ unreachable!();
+ };
+
+ let Some(asset) = stored_asset.inner.downcast_mut::<AssetT>() else {
tracing::error!("Wrong asset type");
return None;
};
@@ -203,7 +235,7 @@ impl Assets
unreachable!();
};
- if stored_asset.strong.downcast_ref::<AssetT>().is_none() {
+ if stored_asset.inner.downcast_ref::<AssetT>().is_none() {
tracing::error!("Wrong asset type");
return None;
}
@@ -231,7 +263,7 @@ impl Assets
unreachable!();
};
- stored_asset.strong.downcast_ref::<AssetT>().is_some()
+ stored_asset.inner.downcast_ref::<AssetT>().is_some()
}
pub fn get_label<AssetT>(&self, handle: &Handle<AssetT>) -> Option<Label<'static>>
@@ -1049,16 +1081,14 @@ enum AssetState
#[derive(Debug)]
struct StoredAsset
{
- strong: Arc<dyn Any + Send + Sync>,
+ inner: Box<dyn Any + Send + Sync>,
}
impl StoredAsset
{
fn new<AssetT: Asset>(asset: AssetT) -> Self
{
- let strong = Arc::new(asset);
-
- Self { strong }
+ Self { inner: Box::new(asset) }
}
}