diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index dbbe5959a..4252141e9 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -183,13 +183,19 @@ impl Storage { } } - //Prevents panic on out of range access. + // Prevents panic on out of range access, allows Vacant elements. pub(crate) fn try_remove(&mut self, id: I) -> Option { - let (index, epoch, backend) = id.unzip(); + let (index, epoch, _) = id.unzip(); if index as usize >= self.map.len() { - return None; + None + } else if let Element::Occupied(value, storage_epoch) = + std::mem::replace(&mut self.map[index as usize], Element::Vacant) + { + assert_eq!(epoch, storage_epoch); + Some(value) + } else { + None } - self.remove(I::zip(index, epoch, backend)) } pub(crate) fn iter(&self, backend: Backend) -> impl Iterator {