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