Skip to content

Commit fffb1bd

Browse files
Revert "use set() in SyncOnceCell::from"
This reverts commit d1263f5.
1 parent f9febd5 commit fffb1bd

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/libstd/lazy.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,23 @@ 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 cell = Self::new();
88+
let res = SyncOnceCell::new();
8989
if let Some(value) = self.get() {
90-
match cell.set(value.clone()) {
90+
match res.set(value.clone()) {
9191
Ok(()) => (),
9292
Err(_) => unreachable!(),
9393
}
9494
}
95-
cell
95+
res
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-
match cell.set(value) {
104-
Ok(()) => cell,
105-
Err(_) => unreachable!(),
106-
}
103+
cell.get_or_init(|| value);
104+
cell
107105
}
108106
}
109107

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

158156
/// Sets the contents of this cell to `value`.
159157
///
160-
/// Returns `Ok(())` if the cell's value was updated.
158+
/// Returns `Ok(())` if the cell was empty and `Err(value)` if it was
159+
/// full.
161160
///
162161
/// # Examples
163162
///
@@ -263,10 +262,8 @@ impl<T> SyncOnceCell<T> {
263262
F: FnOnce() -> Result<T, E>,
264263
{
265264
// Fast path check
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.
265+
// NOTE: This acquire here is important to ensure
266+
// `SyncLazy::force` is correctly synchronized
270267
if let Some(value) = self.get() {
271268
return Ok(value);
272269
}
@@ -413,8 +410,6 @@ const COMPLETE: usize = 0x2;
413410

414411
const STATE_MASK: usize = 0x3;
415412

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

0 commit comments

Comments
 (0)