Panic to prevent arena Handle overflow (#667)

* Panic to prevent arena Handle overflow

* Use `NonZeroU32::new` to detect overflow
This commit is contained in:
Zakarum
2021-04-07 21:00:40 +03:00
committed by GitHub
parent 0058ce5a7d
commit ccb6dc9088

View File

@@ -189,7 +189,8 @@ impl<T> Arena<T> {
/// Adds a new value to the arena, returning a typed handle.
pub fn append(&mut self, value: T) -> Handle<T> {
let position = self.data.len() + 1;
let index = unsafe { Index::new_unchecked(position as u32) };
let index =
Index::new(position as u32).expect("Failed to append to Arena. Handle overflows");
self.data.push(value);
Handle::new(index)
}