Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add UnwrapNever helper to remove some unsafe #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::marker::PhantomData;
use core::mem::{ManuallyDrop, MaybeUninit};
use core::ops::{Index, IndexMut};

use crate::util::{Never, UnwrapUnchecked};
use crate::util::{Never, UnwrapNever};
use crate::{DefaultKey, Key, KeyData};

// Storage inside a slot or metadata for the freelist when vacant.
Expand Down Expand Up @@ -344,7 +344,8 @@ impl<K: Key, V> SlotMap<K, V> {
/// ```
#[inline(always)]
pub fn insert(&mut self, value: V) -> K {
unsafe { self.try_insert_with_key::<_, Never>(move |_| Ok(value)).unwrap_unchecked_() }
self.try_insert_with_key::<_, Never>(move |_| Ok(value))
.unwrap_never()
}

/// Inserts a value given by `f` into the slot map. The key where the
Expand All @@ -369,7 +370,8 @@ impl<K: Key, V> SlotMap<K, V> {
where
F: FnOnce(K) -> V,
{
unsafe { self.try_insert_with_key::<_, Never>(move |k| Ok(f(k))).unwrap_unchecked_() }
self.try_insert_with_key::<_, Never>(move |k| Ok(f(k)))
.unwrap_never()
}

/// Inserts a value given by `f` into the slot map. The key where the
Expand Down
8 changes: 5 additions & 3 deletions src/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use core::iter::FusedIterator;
use core::mem::MaybeUninit;
use core::ops::{Index, IndexMut};

use crate::util::{Never, UnwrapUnchecked};
use crate::util::{Never, UnwrapNever};
use crate::{DefaultKey, Key, KeyData};

// A slot, which represents storage for an index and a current version.
Expand Down Expand Up @@ -252,7 +252,8 @@ impl<K: Key, V> DenseSlotMap<K, V> {
/// ```
#[inline(always)]
pub fn insert(&mut self, value: V) -> K {
unsafe { self.try_insert_with_key::<_, Never>(move |_| Ok(value)).unwrap_unchecked_() }
self.try_insert_with_key::<_, Never>(move |_| Ok(value))
.unwrap_never()
}

/// Inserts a value given by `f` into the slot map. The key where the
Expand All @@ -277,7 +278,8 @@ impl<K: Key, V> DenseSlotMap<K, V> {
where
F: FnOnce(K) -> V,
{
unsafe { self.try_insert_with_key::<_, Never>(move |k| Ok(f(k))).unwrap_unchecked_() }
self.try_insert_with_key::<_, Never>(move |k| Ok(f(k)))
.unwrap_never()
}

/// Inserts a value given by `f` into the slot map. The key where the
Expand Down
8 changes: 5 additions & 3 deletions src/hop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use core::mem::ManuallyDrop;
use core::mem::MaybeUninit;
use core::ops::{Index, IndexMut};

use crate::util::{Never, UnwrapUnchecked};
use crate::util::{Never, UnwrapNever};
use crate::{DefaultKey, Key, KeyData};

// Metadata to maintain the freelist.
Expand Down Expand Up @@ -363,7 +363,8 @@ impl<K: Key, V> HopSlotMap<K, V> {
/// ```
#[inline(always)]
pub fn insert(&mut self, value: V) -> K {
unsafe { self.try_insert_with_key::<_, Never>(move |_| Ok(value)).unwrap_unchecked_() }
self.try_insert_with_key::<_, Never>(move |_| Ok(value))
.unwrap_never()
}

// Helper function to make using the freelist painless.
Expand Down Expand Up @@ -395,7 +396,8 @@ impl<K: Key, V> HopSlotMap<K, V> {
where
F: FnOnce(K) -> V,
{
unsafe { self.try_insert_with_key::<_, Never>(move |k| Ok(f(k))).unwrap_unchecked_() }
self.try_insert_with_key::<_, Never>(move |k| Ok(f(k)))
.unwrap_never()
}

/// Inserts a value given by `f` into the slot map. The key where the
Expand Down
18 changes: 9 additions & 9 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ impl<T> UnwrapUnchecked<T> for Option<T> {
}
}

impl<T, E: Debug> UnwrapUnchecked<T> for Result<T, E> {
unsafe fn unwrap_unchecked_(self) -> T {
if cfg!(debug_assertions) {
self.unwrap()
} else {
match self {
Ok(x) => x,
Err(_) => unreachable_unchecked(),
}
pub trait UnwrapNever<T> {
fn unwrap_never(self) -> T;
}

impl<T> UnwrapNever<T> for Result<T, Never> {
fn unwrap_never(self) -> T {
match self {
Ok(t) => t,
Err(e) => match e {},
}
}
}