Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic in Storage::get #2657

Merged
merged 6 commits into from
May 14, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,46 +149,59 @@ impl<T, I: id::TypedId> ops::IndexMut<id::Valid<I>> for Storage<T, I> {
impl<T, I: id::TypedId> Storage<T, I> {
pub(crate) fn contains(&self, id: I) -> bool {
let (index, epoch, _) = id.unzip();
match self.map[index as usize] {
Element::Vacant => false,
Element::Occupied(_, storage_epoch) | Element::Error(storage_epoch, ..) => {
epoch == storage_epoch
}
match self.map.get(index as usize) {
SparkyPotato marked this conversation as resolved.
Show resolved Hide resolved
Some(element) => match *element {
Element::Vacant => false,
Element::Occupied(_, storage_epoch) | Element::Error(storage_epoch, ..) => {
epoch == storage_epoch
}
},
None => false,
}
}

/// Get a reference to an item behind a potentially invalid ID.
/// Panics if there is an epoch mismatch, or the entry is empty.
pub(crate) fn get(&self, id: I) -> Result<&T, InvalidId> {
let (index, epoch, _) = id.unzip();
let (result, storage_epoch) = match self.map[index as usize] {
Element::Occupied(ref v, epoch) => (Ok(v), epoch),
Element::Vacant => panic!("{}[{}] does not exist", self.kind, index),
Element::Error(epoch, ..) => (Err(InvalidId), epoch),
};
assert_eq!(
epoch, storage_epoch,
"{}[{}] is no longer alive",
self.kind, index
);
result
match self.map.get(index as usize) {
Some(element) => {
let (result, storage_epoch) = match *element {
SparkyPotato marked this conversation as resolved.
Show resolved Hide resolved
Element::Occupied(ref v, epoch) => (Ok(v), epoch),
Element::Vacant => panic!("{}[{}] does not exist", self.kind, index),
Element::Error(epoch, ..) => (Err(InvalidId), epoch),
};
assert_eq!(
epoch, storage_epoch,
"{}[{}] is no longer alive",
self.kind, index
);
result
}
None => Err(InvalidId),
}
}

/// Get a mutable reference to an item behind a potentially invalid ID.
/// Panics if there is an epoch mismatch, or the entry is empty.
pub(crate) fn get_mut(&mut self, id: I) -> Result<&mut T, InvalidId> {
let (index, epoch, _) = id.unzip();
let (result, storage_epoch) = match self.map[index as usize] {
Element::Occupied(ref mut v, epoch) => (Ok(v), epoch),
Element::Vacant => panic!("{}[{}] does not exist", self.kind, index),
Element::Error(epoch, ..) => (Err(InvalidId), epoch),
};
assert_eq!(
epoch, storage_epoch,
"{}[{}] is no longer alive",
self.kind, index
);
result
match self.map.get_mut(index as usize) {
Some(element) => {
let (result, storage_epoch) = match *element {
Element::Occupied(ref mut v, epoch) => (Ok(v), epoch),
Element::Vacant => panic!("{}[{}] does not exist", self.kind, index),
Element::Error(epoch, ..) => (Err(InvalidId), epoch),
};
assert_eq!(
epoch, storage_epoch,
"{}[{}] is no longer alive",
self.kind, index
);
result
}
None => Err(InvalidId),
}
}

pub(crate) fn label_for_invalid_id(&self, id: I) -> &str {
Expand Down