Skip to content

Commit

Permalink
use set() in SyncOnceCell::from
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jul 16, 2020
1 parent 237a977 commit d1263f5
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/libstd/lazy.rs
Expand Up @@ -85,23 +85,25 @@ impl<T: fmt::Debug> fmt::Debug for SyncOnceCell<T> {
#[unstable(feature = "once_cell", issue = "68198")]
impl<T: Clone> Clone for SyncOnceCell<T> {
fn clone(&self) -> SyncOnceCell<T> {
let res = SyncOnceCell::new();
let cell = Self::new();
if let Some(value) = self.get() {
match res.set(value.clone()) {
match cell.set(value.clone()) {
Ok(()) => (),
Err(_) => unreachable!(),
}
}
res
cell
}
}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T> From<T> for SyncOnceCell<T> {
fn from(value: T) -> Self {
let cell = Self::new();
cell.get_or_init(|| value);
cell
match cell.set(value) {
Ok(()) => cell,
Err(_) => unreachable!(),
}
}
}

Expand Down Expand Up @@ -155,8 +157,7 @@ impl<T> SyncOnceCell<T> {

/// Sets the contents of this cell to `value`.
///
/// Returns `Ok(())` if the cell was empty and `Err(value)` if it was
/// full.
/// Returns `Ok(())` if the cell's value was updated.
///
/// # Examples
///
Expand Down Expand Up @@ -262,8 +263,10 @@ impl<T> SyncOnceCell<T> {
F: FnOnce() -> Result<T, E>,
{
// Fast path check
// NOTE: This acquire here is important to ensure
// `SyncLazy::force` is correctly synchronized
// NOTE: We need to perform an acquire on the state in this method
// in order to correctly synchronize `SyncLazy::force`. This is
// currently done by calling `self.get()`, which in turn calls
// `self.is_initialized()`, which in turn performs the acquire.
if let Some(value) = self.get() {
return Ok(value);
}
Expand Down Expand Up @@ -410,6 +413,8 @@ const COMPLETE: usize = 0x2;

const STATE_MASK: usize = 0x3;

// The alignment here is so that we can stash the state in the lower
// bits of the `next` pointer
#[repr(align(4))]
struct Waiter {
thread: Cell<Option<Thread>>,
Expand Down

0 comments on commit d1263f5

Please sign in to comment.