@@ -85,23 +85,25 @@ impl<T: fmt::Debug> fmt::Debug for SyncOnceCell<T> {
85
85
#[ unstable( feature = "once_cell" , issue = "68198" ) ]
86
86
impl < T : Clone > Clone for SyncOnceCell < T > {
87
87
fn clone ( & self ) -> SyncOnceCell < T > {
88
- let res = SyncOnceCell :: new ( ) ;
88
+ let cell = Self :: new ( ) ;
89
89
if let Some ( value) = self . get ( ) {
90
- match res . set ( value. clone ( ) ) {
90
+ match cell . set ( value. clone ( ) ) {
91
91
Ok ( ( ) ) => ( ) ,
92
92
Err ( _) => unreachable ! ( ) ,
93
93
}
94
94
}
95
- res
95
+ cell
96
96
}
97
97
}
98
98
99
99
#[ unstable( feature = "once_cell" , issue = "68198" ) ]
100
100
impl < T > From < T > for SyncOnceCell < T > {
101
101
fn from ( value : T ) -> Self {
102
102
let cell = Self :: new ( ) ;
103
- cell. get_or_init ( || value) ;
104
- cell
103
+ match cell. set ( value) {
104
+ Ok ( ( ) ) => cell,
105
+ Err ( _) => unreachable ! ( ) ,
106
+ }
105
107
}
106
108
}
107
109
@@ -155,8 +157,7 @@ impl<T> SyncOnceCell<T> {
155
157
156
158
/// Sets the contents of this cell to `value`.
157
159
///
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.
160
161
///
161
162
/// # Examples
162
163
///
@@ -262,8 +263,10 @@ impl<T> SyncOnceCell<T> {
262
263
F : FnOnce ( ) -> Result < T , E > ,
263
264
{
264
265
// 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.
267
270
if let Some ( value) = self . get ( ) {
268
271
return Ok ( value) ;
269
272
}
@@ -410,6 +413,8 @@ const COMPLETE: usize = 0x2;
410
413
411
414
const STATE_MASK : usize = 0x3 ;
412
415
416
+ // The alignment here is so that we can stash the state in the lower
417
+ // bits of the `next` pointer
413
418
#[ repr( align( 4 ) ) ]
414
419
struct Waiter {
415
420
thread : Cell < Option < Thread > > ,
0 commit comments