From 9adf24618a943bab4ce39267760d3a5348519fb2 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 24 Sep 2026 18:57:36 +0200 Subject: feat(engine): add invalid asset IDs & handles --- engine/src/asset.rs | 69 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/engine/src/asset.rs b/engine/src/asset.rs index 56b8a46..8337f5f 100644 --- a/engine/src/asset.rs +++ b/engine/src/asset.rs @@ -186,10 +186,12 @@ impl Assets handle: &Handle, ) -> Option<&Asset> { + let id = handle.id.into_valid()?; + let asset_lookup = self.asset_lookup.borrow(); let LookupEntry::Occupied(asset_index, asset_label) = - asset_lookup.get(&handle.id.label_hash)? + asset_lookup.get(&id.label_hash)? else { return None; }; @@ -242,10 +244,14 @@ impl Assets handle: &Handle, ) -> bool { + let Some(id) = handle.id.into_valid() else { + return false; + }; + let asset_lookup = self.asset_lookup.borrow(); let Some(LookupEntry::Occupied(asset_index, _)) = - asset_lookup.get(&handle.id.label_hash) + asset_lookup.get(&id.label_hash) else { return false; }; @@ -262,11 +268,9 @@ impl Assets handle: &Handle, ) -> Option { - let lookup_entry = self - .asset_lookup - .borrow() - .get(&handle.id.label_hash)? - .clone(); + 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; @@ -277,6 +281,8 @@ impl Assets pub fn get_label_by_id(&self, id: Id) -> Option { + let id = id.into_valid()?; + let lookup_entry = self.asset_lookup.borrow().get(&id.label_hash)?.clone(); let LookupEntry::Occupied(_, label) = lookup_entry else { @@ -441,11 +447,15 @@ impl Assets .get_mut() .insert(label_hash, LookupEntry::Occupied(index, label.to_owned())); + let id = Id { + inner: IdInner::Valid(IdValid { label_hash }), + }; + self.events .curr_tick_events - .push(Event::Stored(Id { label_hash }, label.to_owned())); + .push(Event::Stored(id, label.to_owned())); - Handle::new(label_hash) + Handle::from_id(id) } pub fn events(&self) -> &Events @@ -677,6 +687,11 @@ pub struct Handle impl Handle { + pub fn invalid() -> Self + { + Self::from_id(Id::invalid()) + } + #[must_use] pub fn from_id(id: Id) -> Self { @@ -691,10 +706,9 @@ impl Handle fn new(label_hash: LabelHash) -> Self { - Self { - id: Id { label_hash }, - _pd: PhantomData, - } + Self::from_id(Id { + inner: IdInner::Valid(IdValid { label_hash }), + }) } } @@ -709,6 +723,35 @@ impl Clone for Handle /// Asset ID. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Id +{ + inner: IdInner, +} + +impl Id +{ + pub fn invalid() -> Self + { + Self { inner: IdInner::Invalid } + } + + fn into_valid(self) -> Option + { + match self.inner { + IdInner::Valid(id) => Some(id), + IdInner::Invalid => None, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +enum IdInner +{ + Valid(IdValid), + Invalid, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +struct IdValid { label_hash: LabelHash, } -- cgit v1.2.3-18-g5258