diff options
Diffstat (limited to 'engine')
| -rw-r--r-- | engine/src/asset.rs | 118 |
1 files changed, 51 insertions, 67 deletions
diff --git a/engine/src/asset.rs b/engine/src/asset.rs index 75b8592..08c92bd 100644 --- a/engine/src/asset.rs +++ b/engine/src/asset.rs @@ -40,11 +40,11 @@ pub struct Label<'a> impl Label<'_> { #[must_use] - pub fn to_owned(&self) -> LabelOwned + fn to_owned(&self) -> LabelOwned { LabelOwned { - path: self.path.to_path_buf(), - name: self.name.as_ref().map(std::string::ToString::to_string), + path: self.path.clone().into_owned(), + name: self.name.clone().map(Cow::into_owned), } } } @@ -65,17 +65,6 @@ impl From<PathBuf> for Label<'_> } } -impl<'a> From<&'a LabelOwned> for Label<'a> -{ - fn from(label: &'a LabelOwned) -> Self - { - Self { - path: (&label.path).into(), - name: label.name.as_ref().map(|name| Cow::Borrowed(name.as_str())), - } - } -} - impl Display for Label<'_> { fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result @@ -91,40 +80,6 @@ impl Display for Label<'_> } } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct LabelOwned -{ - pub path: PathBuf, - pub name: Option<String>, -} - -impl LabelOwned -{ - #[must_use] - pub fn to_label(&self) -> Label<'_> - { - Label { - path: (&self.path).into(), - name: self.name.as_ref().map(|name| Cow::Borrowed(name.as_str())), - } - } -} - -impl Display for LabelOwned -{ - fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result - { - write!(formatter, "{}", self.path.display())?; - - if let Some(name) = &self.name { - formatter.write_str("::")?; - formatter.write_str(name)?; - } - - Ok(()) - } -} - #[derive(Debug, Sole)] pub struct Assets { @@ -256,22 +211,14 @@ impl Assets stored_asset.strong.downcast_ref::<AssetT>().is_some() } - pub fn get_label<AssetT>(&self, handle: &Handle<AssetT>) -> Option<LabelOwned> + pub fn get_label<AssetT>(&self, handle: &Handle<AssetT>) -> Option<Label<'static>> where AssetT: Asset, { - let id = handle.id.into_valid()?; - - let lookup_entry = self.asset_lookup.borrow().get(&id.label_hash)?.clone(); - - let LookupEntry::Occupied(_, label) = lookup_entry else { - return None; - }; - - Some(label) + self.get_label_by_id(handle.id) } - pub fn get_label_by_id(&self, id: Id) -> Option<LabelOwned> + pub fn get_label_by_id(&self, id: Id) -> Option<Label<'static>> { let id = id.into_valid()?; @@ -281,7 +228,7 @@ impl Assets return None; }; - Some(label) + Some(label.clone().into_label()) } #[tracing::instrument(skip_all, fields(label, asset_type = %type_name::<AssetT>()))] @@ -446,9 +393,11 @@ impl Assets let index = self.store.len() - 1; + let label = label.to_owned(); + self.asset_lookup .get_mut() - .insert(label_hash, LookupEntry::Occupied(index, label.to_owned())); + .insert(label_hash, LookupEntry::Occupied(index, label.clone())); let id = Id { inner: IdInner::Valid(IdValid { label_hash }), @@ -456,7 +405,7 @@ impl Assets self.events .curr_tick_events - .push(Event::Stored(id, label.to_owned())); + .push(Event::Stored(id, label.into_label())); Handle::from_id(id) } @@ -617,7 +566,7 @@ impl Submitter<'_> name: None, }; - let label_hash = LabelHash::new(&label.to_label()); + let label_hash = LabelHash::new(&label.as_label()); let _ = self.import_work_msg_sender.send(ImportWorkMessage::Store { do_store: |assets, label, boxed_asset| { @@ -625,7 +574,7 @@ impl Submitter<'_> unreachable!(); }; - assets.store_with_label::<AssetT>(&label, *asset); + assets.store_with_label::<AssetT>(label.as_label(), *asset); }, label, asset: Box::new(asset), @@ -647,7 +596,7 @@ impl Submitter<'_> name: Some(name.as_ref().into()), }; - let label_hash = LabelHash::new(&label.to_label()); + let label_hash = LabelHash::new(&label.as_label()); let _ = self.import_work_msg_sender.send(ImportWorkMessage::Store { do_store: |assets, label, boxed_asset| { @@ -655,7 +604,7 @@ impl Submitter<'_> unreachable!(); }; - assets.store_with_label::<AssetT>(&label, *asset); + assets.store_with_label::<AssetT>(label.as_label(), *asset); }, label, asset: Box::new(asset), @@ -764,7 +713,7 @@ impl Events pub enum Event { /// Asset stored. - Stored(Id, LabelOwned), + Stored(Id, Label<'static>), } #[derive(Debug, thiserror::Error)] @@ -983,3 +932,38 @@ impl AssetTypeId Self { inner: TypeId::of::<AssetT>() } } } + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +struct LabelOwned +{ + path: PathBuf, + name: Option<String>, +} + +impl LabelOwned +{ + #[must_use] + fn as_label(&self) -> Label<'_> + { + Label { + path: self.path.as_path().into(), + name: self.name.as_deref().map(Into::into), + } + } + + fn into_label(self) -> Label<'static> + { + Label { + path: self.path.into(), + name: self.name.map(Into::into), + } + } +} + +impl Display for LabelOwned +{ + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + <Label<'_> as Display>::fmt(&self.as_label(), formatter) + } +} |
