@@ -207,13 +207,9 @@ impl<T> OnReady<T> {
207
207
InitState :: ManualUninitialized => {
208
208
self . state = InitState :: Initialized { value } ;
209
209
}
210
- InitState :: AutoPrepared { .. } => {
210
+ InitState :: AutoPrepared { .. } | InitState :: AutoInitializationFailed => {
211
211
panic ! ( "cannot call init() on auto-initialized OnReady objects" )
212
212
}
213
- InitState :: AutoInitializing => {
214
- // SAFETY: Loading is ephemeral state that is only set in init_auto() and immediately overwritten.
215
- unsafe { std:: hint:: unreachable_unchecked ( ) }
216
- }
217
213
InitState :: Initialized { .. } => {
218
214
panic ! ( "already initialized; did you call init() more than once?" )
219
215
}
@@ -223,22 +219,22 @@ impl<T> OnReady<T> {
223
219
/// Runs initialization.
224
220
///
225
221
/// # Panics
226
- /// If the value is already initialized.
222
+ /// - If the value is already initialized.
223
+ /// - If previous auto initialization failed.
227
224
pub ( crate ) fn init_auto ( & mut self , base : & Gd < Node > ) {
228
225
// Two branches needed, because mem::replace() could accidentally overwrite an already initialized value.
229
226
match & self . state {
230
227
InitState :: ManualUninitialized => return , // skipped
231
228
InitState :: AutoPrepared { .. } => { } // handled below
232
- InitState :: AutoInitializing => {
233
- // SAFETY: Loading is ephemeral state that is only set below and immediately overwritten.
234
- unsafe { std:: hint:: unreachable_unchecked ( ) }
229
+ InitState :: AutoInitializationFailed => {
230
+ panic ! ( "OnReady automatic value initialization has already failed" )
235
231
}
236
232
InitState :: Initialized { .. } => panic ! ( "OnReady object already initialized" ) ,
237
233
} ;
238
234
239
- // Temporarily replace with dummy state, as it's not possible to take ownership of the initializer closure otherwise .
235
+ // Temporally replace with AutoInitializationFailed state which will be left in iff initialization fails .
240
236
let InitState :: AutoPrepared { initializer } =
241
- mem:: replace ( & mut self . state , InitState :: AutoInitializing )
237
+ mem:: replace ( & mut self . state , InitState :: AutoInitializationFailed )
242
238
else {
243
239
// SAFETY: condition checked above.
244
240
unsafe { std:: hint:: unreachable_unchecked ( ) }
@@ -267,7 +263,9 @@ impl<T> std::ops::Deref for OnReady<T> {
267
263
InitState :: AutoPrepared { .. } => {
268
264
panic ! ( "OnReady automatic value uninitialized, is only available in ready()" )
269
265
}
270
- InitState :: AutoInitializing => unreachable ! ( ) ,
266
+ InitState :: AutoInitializationFailed => {
267
+ panic ! ( "OnReady automatic value initialization failed" )
268
+ }
271
269
InitState :: Initialized { value } => value,
272
270
}
273
271
}
@@ -284,7 +282,9 @@ impl<T> std::ops::DerefMut for OnReady<T> {
284
282
InitState :: ManualUninitialized | InitState :: AutoPrepared { .. } => {
285
283
panic ! ( "value not yet initialized" )
286
284
}
287
- InitState :: AutoInitializing => unreachable ! ( ) ,
285
+ InitState :: AutoInitializationFailed => {
286
+ panic ! ( "OnReady automatic value initialization failed" )
287
+ }
288
288
}
289
289
}
290
290
}
@@ -313,7 +313,7 @@ type InitFn<T> = dyn FnOnce(&Gd<Node>) -> T;
313
313
enum InitState < T > {
314
314
ManualUninitialized ,
315
315
AutoPrepared { initializer : Box < InitFn < T > > } ,
316
- AutoInitializing , // needed because state cannot be empty
316
+ AutoInitializationFailed ,
317
317
Initialized { value : T } ,
318
318
}
319
319
@@ -324,7 +324,9 @@ impl<T: Debug> Debug for InitState<T> {
324
324
InitState :: AutoPrepared { .. } => {
325
325
fmt. debug_struct ( "AutoPrepared" ) . finish_non_exhaustive ( )
326
326
}
327
- InitState :: AutoInitializing => fmt. debug_struct ( "AutoInitializing" ) . finish ( ) ,
327
+ InitState :: AutoInitializationFailed => {
328
+ fmt. debug_struct ( "AutoInitializationFailed" ) . finish ( )
329
+ }
328
330
InitState :: Initialized { value } => fmt
329
331
. debug_struct ( "Initialized" )
330
332
. field ( "value" , value)
0 commit comments