summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-09-24 18:57:36 +0200
committerHampusM <hampus@hampusmat.com>2026-09-27 19:20:44 +0200
commit9adf24618a943bab4ce39267760d3a5348519fb2 (patch)
tree591b7a34c2f088e5af0fd6a55b5363ad1b83ea9d /engine
parent022a9971cee14da96c6b955dd6413d060d77662b (diff)
feat(engine): add invalid asset IDs & handles
Diffstat (limited to 'engine')
-rw-r--r--engine/src/asset.rs69
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,
}