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

get_unchecked isn't unchecked #74

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
22 changes: 14 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -732,9 +732,12 @@ impl<T> Slab<T> {
}

/// 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
///
Expand All @@ -750,14 +753,17 @@ impl<T> Slab<T> {
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
///
Expand All @@ -776,7 +782,7 @@ impl<T> Slab<T> {
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(),
}
}

Expand Down