Skip to content

Commit ad0af31

Browse files
Make SavedAsset::get_labeled accept &str as label (#11612)
# Objective - SavedAsset's iter_labels returns ```&str```, however accessing LabeledAssets requires ```CowArc<'static, str>``` - Although SavedAsset holds UntypedHandles in its hashmap of LabeledAssets, they are inaccessible as LabeledAssets are casted to SavedAsset or ErasedLoadedAsset, which don't contain their UntypedHandles - Adresses #11609 ## Solution - Used Trait bounds to allow for either ```CowArc<'static, str>``` or ```&str``` to be used as a label in get_labeled and get_erased_labeled. - Added method get_untyped_handle to get UntypedHandle from the LabeledAsset. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent 14f1a4f commit ad0af31

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

crates/bevy_asset/src/saver.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{io::Writer, meta::Settings, Asset, ErasedLoadedAsset};
2-
use crate::{AssetLoader, LabeledAsset};
2+
use crate::{AssetLoader, LabeledAsset, UntypedHandle};
33
use bevy_utils::{BoxedFuture, CowArc, HashMap};
44
use serde::{Deserialize, Serialize};
5-
use std::ops::Deref;
5+
use std::{borrow::Borrow, hash::Hash, ops::Deref};
66

77
/// Saves an [`Asset`] of a given [`AssetSaver::Asset`] type. [`AssetSaver::OutputLoader`] will then be used to load the saved asset
88
/// in the final deployed application. The saver should produce asset bytes in a format that [`AssetSaver::OutputLoader`] can read.
@@ -95,11 +95,12 @@ impl<'a, A: Asset> SavedAsset<'a, A> {
9595
}
9696

9797
/// Returns the labeled asset, if it exists and matches this type.
98-
pub fn get_labeled<B: Asset>(
99-
&self,
100-
label: impl Into<CowArc<'static, str>>,
101-
) -> Option<SavedAsset<B>> {
102-
let labeled = self.labeled_assets.get(&label.into())?;
98+
pub fn get_labeled<B: Asset, Q>(&self, label: &Q) -> Option<SavedAsset<B>>
99+
where
100+
CowArc<'static, str>: Borrow<Q>,
101+
Q: ?Sized + Hash + Eq,
102+
{
103+
let labeled = self.labeled_assets.get(label)?;
103104
let value = labeled.asset.value.downcast_ref::<B>()?;
104105
Some(SavedAsset {
105106
value,
@@ -108,14 +109,25 @@ impl<'a, A: Asset> SavedAsset<'a, A> {
108109
}
109110

110111
/// Returns the type-erased labeled asset, if it exists and matches this type.
111-
pub fn get_erased_labeled(
112-
&self,
113-
label: impl Into<CowArc<'static, str>>,
114-
) -> Option<&ErasedLoadedAsset> {
115-
let labeled = self.labeled_assets.get(&label.into())?;
112+
pub fn get_erased_labeled<Q>(&self, label: &Q) -> Option<&ErasedLoadedAsset>
113+
where
114+
CowArc<'static, str>: Borrow<Q>,
115+
Q: ?Sized + Hash + Eq,
116+
{
117+
let labeled = self.labeled_assets.get(label)?;
116118
Some(&labeled.asset)
117119
}
118120

121+
/// Returns the [`UntypedHandle`] of the labeled asset with the provided 'label', if it exists.
122+
pub fn get_untyped_handle<Q>(&self, label: &Q) -> Option<&UntypedHandle>
123+
where
124+
CowArc<'static, str>: Borrow<Q>,
125+
Q: ?Sized + Hash + Eq,
126+
{
127+
let labeled = self.labeled_assets.get(label)?;
128+
Some(&labeled.handle)
129+
}
130+
119131
/// Iterate over all labels for "labeled assets" in the loaded asset
120132
pub fn iter_labels(&self) -> impl Iterator<Item = &str> {
121133
self.labeled_assets.keys().map(|s| &**s)

0 commit comments

Comments
 (0)