diff --git a/src/lib.rs b/src/lib.rs index 05e9ded..b72f76c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,13 +116,13 @@ use alloc::vec; use core::iter::FromIterator; #[cfg(not(feature = "std"))] -use core::{fmt, mem, ops, slice}; +use core::{fmt, hint, mem, ops, slice}; #[cfg(feature = "std")] use std::iter::FromIterator; #[cfg(feature = "std")] -use std::{fmt, mem, ops, slice, vec}; +use std::{fmt, hint, mem, ops, slice, vec}; /// Pre-allocated storage for a uniform data type /// @@ -732,9 +732,12 @@ impl Slab { } /// Return a reference to the value associated with the given key without - /// performing bounds checking. + /// without checking that there is an element associated with that key. /// - /// This function should be used with care. + /// # Safety + /// + /// * The key must be within bounds + /// * There must be a value present at the slot the key refers to /// /// # Examples /// @@ -750,14 +753,17 @@ impl Slab { pub unsafe fn get_unchecked(&self, key: usize) -> &T { match *self.entries.get_unchecked(key) { Entry::Occupied(ref val) => val, - _ => unreachable!(), + _ => hint::unreachable_unchecked(), } } /// Return a mutable reference to the value associated with the given key - /// without performing bounds checking. + /// without checking that there is an element associated with that key. /// - /// This function should be used with care. + /// # Safety + /// + /// * The key must be within bounds + /// * There must be a value present at the slot the key refers to /// /// # Examples /// @@ -776,7 +782,7 @@ impl Slab { pub unsafe fn get_unchecked_mut(&mut self, key: usize) -> &mut T { match *self.entries.get_unchecked_mut(key) { Entry::Occupied(ref mut val) => val, - _ => unreachable!(), + _ => hint::unreachable_unchecked(), } }