diff --git a/src/imp_cs.rs b/src/imp_cs.rs index 668f18e..04018f1 100644 --- a/src/imp_cs.rs +++ b/src/imp_cs.rs @@ -63,7 +63,7 @@ impl OnceCell { pub(crate) unsafe fn get_unchecked(&self) -> &T { debug_assert!(self.is_initialized()); // SAFETY: The caller ensures that the value is initialized and access synchronized. - crate::unwrap_unchecked(self.value.borrow(CriticalSection::new()).get()) + self.value.borrow(CriticalSection::new()).get().unwrap_unchecked() } #[inline] diff --git a/src/imp_pl.rs b/src/imp_pl.rs index 84d8593..37a8554 100644 --- a/src/imp_pl.rs +++ b/src/imp_pl.rs @@ -58,7 +58,7 @@ impl OnceCell { // but that is more complicated // - finally, if it returns Ok, we store the value and store the flag with // `Release`, which synchronizes with `Acquire`s. - let f = unsafe { crate::unwrap_unchecked(f.take()) }; + let f = unsafe { f.take().unwrap_unchecked() }; match f() { Ok(value) => unsafe { // Safe b/c we have a unique access and no panic may happen @@ -101,7 +101,7 @@ impl OnceCell { pub(crate) unsafe fn get_unchecked(&self) -> &T { debug_assert!(self.is_initialized()); let slot = &*self.value.get(); - crate::unwrap_unchecked(slot.as_ref()) + slot.as_ref().unwrap_unchecked() } /// Gets the mutable reference to the underlying value. diff --git a/src/imp_std.rs b/src/imp_std.rs index f6d9472..3b9e6d2 100644 --- a/src/imp_std.rs +++ b/src/imp_std.rs @@ -37,17 +37,11 @@ impl UnwindSafe for OnceCell {} impl OnceCell { pub(crate) const fn new() -> OnceCell { - OnceCell { - queue: AtomicPtr::new(INCOMPLETE_PTR), - value: UnsafeCell::new(None), - } + OnceCell { queue: AtomicPtr::new(INCOMPLETE_PTR), value: UnsafeCell::new(None) } } pub(crate) const fn with_value(value: T) -> OnceCell { - OnceCell { - queue: AtomicPtr::new(COMPLETE_PTR), - value: UnsafeCell::new(Some(value)), - } + OnceCell { queue: AtomicPtr::new(COMPLETE_PTR), value: UnsafeCell::new(Some(value)) } } /// Safety: synchronizes with store to value via Release/(Acquire|SeqCst). @@ -74,7 +68,7 @@ impl OnceCell { initialize_or_wait( &self.queue, Some(&mut || { - let f = unsafe { crate::unwrap_unchecked(f.take()) }; + let f = unsafe { f.take().unwrap_unchecked() }; match f() { Ok(value) => { unsafe { *slot = Some(value) }; @@ -105,7 +99,7 @@ impl OnceCell { pub(crate) unsafe fn get_unchecked(&self) -> &T { debug_assert!(self.is_initialized()); let slot = &*self.value.get(); - crate::unwrap_unchecked(slot.as_ref()) + slot.as_ref().unwrap_unchecked() } /// Gets the mutable reference to the underlying value. diff --git a/src/lib.rs b/src/lib.rs index 4c2d0e4..db7e19a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -390,8 +390,6 @@ pub mod unsync { panic::{RefUnwindSafe, UnwindSafe}, }; - use super::unwrap_unchecked; - /// A cell which can be written to only once. It is not thread safe. /// /// Unlike [`std::cell::RefCell`], a `OnceCell` provides simple `&` @@ -563,7 +561,7 @@ pub mod unsync { // checked that slot is currently `None`, so this write // maintains the `inner`'s invariant. *slot = Some(value); - Ok(unsafe { unwrap_unchecked(slot.as_ref()) }) + Ok(unsafe { slot.as_ref().unwrap_unchecked() }) } /// Gets the contents of the cell, initializing it with `f` @@ -636,7 +634,7 @@ pub mod unsync { // `assert`, while keeping `set/get` would be sound, but it seems // better to panic, rather than to silently use an old value. assert!(self.set(val).is_ok(), "reentrant init"); - Ok(unsafe { unwrap_unchecked(self.get()) }) + Ok(unsafe { self.get().unwrap_unchecked() }) } /// Takes the value out of this `OnceCell`, moving it back to an uninitialized state. @@ -872,7 +870,7 @@ pub mod sync { panic::RefUnwindSafe, }; - use super::{imp::OnceCell as Imp, unwrap_unchecked}; + use super::imp::OnceCell as Imp; /// A thread-safe cell which can be written to only once. /// @@ -1083,7 +1081,7 @@ pub mod sync { /// ``` pub fn try_insert(&self, value: T) -> Result<&T, (&T, T)> { let mut value = Some(value); - let res = self.get_or_init(|| unsafe { unwrap_unchecked(value.take()) }); + let res = self.get_or_init(|| unsafe { value.take().unwrap_unchecked() }); match value { None => Ok(res), Some(value) => Err((res, value)), @@ -1414,15 +1412,3 @@ pub mod sync { #[cfg(feature = "race")] pub mod race; - -// Remove once MSRV is at least 1.58. -#[inline] -unsafe fn unwrap_unchecked(val: Option) -> T { - match val { - Some(value) => value, - None => { - debug_assert!(false); - core::hint::unreachable_unchecked() - } - } -}