Implement get_(mut_)unchecked

This commit is contained in:
Stjepan Glavina 2017-04-03 14:06:33 +02:00
parent 9a8c98d144
commit 0eb4ea496c
1 changed files with 56 additions and 3 deletions

View File

@ -297,7 +297,9 @@ impl<T> Arena<T> {
self.head = !0;
}
/// Returns a reference to the object stored at `index`, or `None` if it's out of bounds.
/// Returns a reference to the object stored at `index`.
///
/// If `index` is out of bounds or the slot is vacant, `None` is returned.
///
/// # Examples
///
@ -320,8 +322,9 @@ impl<T> Arena<T> {
}
}
/// Returns a mutable reference to the object stored at `index`, or `None` if it's out of
/// bounds.
/// Returns a mutable reference to the object stored at `index`.
///
/// If `index` is out of bounds or the slot is vacant, `None` is returned.
///
/// # Examples
///
@ -344,6 +347,56 @@ impl<T> Arena<T> {
}
}
/// Returns a reference to the object stored at `index`.
///
/// # Panics
///
/// Panics if `index` is out of bounds or the slot is vacant.
///
/// # Examples
///
/// ```
/// use vec_arena::Arena;
///
/// let mut arena = Arena::new();
/// let index = arena.insert("hello");
///
/// unsafe { assert_eq!(&*arena.get_unchecked(index), &"hello") }
/// ```
#[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"),
Some(&Slot::Occupied(ref object)) => object,
}
}
/// Returns a mutable reference to the object stored at `index`.
///
/// # Panics
///
/// Panics if `index` is out of bounds or the slot is vacant.
///
/// # Examples
///
/// ```
/// use vec_arena::Arena;
///
/// let mut arena = Arena::new();
/// let index = arena.insert("hello");
///
/// unsafe { assert_eq!(&*arena.get_unchecked_mut(index), &"hello") }
/// ```
#[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"),
Some(&mut Slot::Occupied(ref mut object)) => object,
}
}
/// Reserves capacity for at least `additional` more objects to be inserted. The arena may
/// reserve more space to avoid frequent reallocations.
///