mirror of https://github.com/smol-rs/vec-arena
Use unreachable crate
This commit is contained in:
parent
5e136e3121
commit
bde1db88b2
|
@ -7,3 +7,6 @@ repository = "https://github.com/stjepang/vec-arena"
|
|||
documentation = "https://docs.rs/vec-arena"
|
||||
readme = "README.md"
|
||||
license = "Apache-2.0/MIT"
|
||||
|
||||
[dependencies]
|
||||
unreachable = "0.1"
|
||||
|
|
20
src/lib.rs
20
src/lib.rs
|
@ -13,6 +13,8 @@
|
|||
//! * [Doubly linked list](https://github.com/stjepang/vec-arena/blob/master/examples/linked_list.rs)
|
||||
//! * [Splay tree](https://github.com/stjepang/vec-arena/blob/master/examples/splay_tree.rs)
|
||||
|
||||
extern crate unreachable;
|
||||
|
||||
use std::fmt;
|
||||
use std::iter;
|
||||
use std::mem;
|
||||
|
@ -21,6 +23,8 @@ use std::ptr;
|
|||
use std::slice;
|
||||
use std::vec;
|
||||
|
||||
use unreachable::unreachable;
|
||||
|
||||
/// A slot, which is either vacant or occupied.
|
||||
///
|
||||
/// Vacant slots in arena are linked together into a singly linked list. This allows the arena to
|
||||
|
@ -351,9 +355,9 @@ impl<T> Arena<T> {
|
|||
|
||||
/// Returns a reference to the object stored at `index`.
|
||||
///
|
||||
/// # Panics
|
||||
/// # Safety
|
||||
///
|
||||
/// Panics if `index` is out of bounds or the slot is vacant.
|
||||
/// Behavior is undefined if `index` is out of bounds or the slot is vacant.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -368,17 +372,17 @@ impl<T> Arena<T> {
|
|||
#[inline]
|
||||
pub unsafe fn get_unchecked(&self, index: usize) -> &T {
|
||||
match self.slots.get(index) {
|
||||
None => panic!("the index is out of bounds"),
|
||||
Some(&Slot::Vacant(_)) => panic!("the slot is vacant"),
|
||||
None => unreachable(),
|
||||
Some(&Slot::Vacant(_)) => unreachable(),
|
||||
Some(&Slot::Occupied(ref object)) => object,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the object stored at `index`.
|
||||
///
|
||||
/// # Panics
|
||||
/// # Safety
|
||||
///
|
||||
/// Panics if `index` is out of bounds or the slot is vacant.
|
||||
/// Behavior is undefined if `index` is out of bounds or the slot is vacant.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -393,8 +397,8 @@ impl<T> Arena<T> {
|
|||
#[inline]
|
||||
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
|
||||
match self.slots.get_mut(index) {
|
||||
None => panic!("the index is out of bounds"),
|
||||
Some(&mut Slot::Vacant(_)) => panic!("the slot is vacant"),
|
||||
None => unreachable(),
|
||||
Some(&mut Slot::Vacant(_)) => unreachable(),
|
||||
Some(&mut Slot::Occupied(ref mut object)) => object,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue