diff options
| -rw-r--r-- | engine/src/asset.rs | 69 |
1 files 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<Asset>, ) -> 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<Asset>, ) -> 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<Asset>, ) -> Option<LabelOwned> { - 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<LabelOwned> { + 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<Asset: 'static> impl<Asset: 'static> Handle<Asset> { + pub fn invalid() -> Self + { + Self::from_id(Id::invalid()) + } + #[must_use] pub fn from_id(id: Id) -> Self { @@ -691,10 +706,9 @@ impl<Asset: 'static> Handle<Asset> fn new(label_hash: LabelHash) -> Self { - Self { - id: Id { label_hash }, - _pd: PhantomData, - } + Self::from_id(Id { + inner: IdInner::Valid(IdValid { label_hash }), + }) } } @@ -710,6 +724,35 @@ impl<Asset: 'static> Clone for Handle<Asset> #[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<IdValid> + { + 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, } |
