Skip to content

Commit

Permalink
Merge #238
Browse files Browse the repository at this point in the history
238: Reorgonize test suite r=matklad a=matklad

bors r+

Co-authored-by: Alex Kladov <aleksey.kladov@gmail.com>
  • Loading branch information
bors[bot] and matklad committed Jun 4, 2023
2 parents 61f8384 + f5f648f commit 67f5856
Show file tree
Hide file tree
Showing 12 changed files with 1,082 additions and 1,008 deletions.
2 changes: 1 addition & 1 deletion src/imp_cs.rs
Expand Up @@ -63,7 +63,7 @@ impl<T> OnceCell<T> {
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]
Expand Down
4 changes: 2 additions & 2 deletions src/imp_pl.rs
Expand Up @@ -58,7 +58,7 @@ impl<T> OnceCell<T> {
// 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
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<T> OnceCell<T> {
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.
Expand Down
14 changes: 4 additions & 10 deletions src/imp_std.rs
Expand Up @@ -37,17 +37,11 @@ impl<T: UnwindSafe> UnwindSafe for OnceCell<T> {}

impl<T> OnceCell<T> {
pub(crate) const fn new() -> OnceCell<T> {
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<T> {
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).
Expand All @@ -74,7 +68,7 @@ impl<T> OnceCell<T> {
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) };
Expand Down Expand Up @@ -105,7 +99,7 @@ impl<T> OnceCell<T> {
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.
Expand Down
22 changes: 4 additions & 18 deletions src/lib.rs
Expand Up @@ -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 `&`
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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<T>(val: Option<T>) -> T {
match val {
Some(value) => value,
None => {
debug_assert!(false);
core::hint::unreachable_unchecked()
}
}
}

0 comments on commit 67f5856

Please sign in to comment.