Skip to content

Commit d1263f5

Browse files
committed
use set() in SyncOnceCell::from
1 parent 237a977 commit d1263f5

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/libstd/lazy.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,25 @@ impl<T: fmt::Debug> fmt::Debug for SyncOnceCell<T> {
8585
#[unstable(feature = "once_cell", issue = "68198")]
8686
impl<T: Clone> Clone for SyncOnceCell<T> {
8787
fn clone(&self) -> SyncOnceCell<T> {
88-
let res = SyncOnceCell::new();
88+
let cell = Self::new();
8989
if let Some(value) = self.get() {
90-
match res.set(value.clone()) {
90+
match cell.set(value.clone()) {
9191
Ok(()) => (),
9292
Err(_) => unreachable!(),
9393
}
9494
}
95-
res
95+
cell
9696
}
9797
}
9898

9999
#[unstable(feature = "once_cell", issue = "68198")]
100100
impl<T> From<T> for SyncOnceCell<T> {
101101
fn from(value: T) -> Self {
102102
let cell = Self::new();
103-
cell.get_or_init(|| value);
104-
cell
103+
match cell.set(value) {
104+
Ok(()) => cell,
105+
Err(_) => unreachable!(),
106+
}
105107
}
106108
}
107109

@@ -155,8 +157,7 @@ impl<T> SyncOnceCell<T> {
155157

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

411414
const STATE_MASK: usize = 0x3;
412415

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

0 commit comments

Comments
 (0)