1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
// Forked from `once_cell v1.21.3` crate by @matklad
// Original code available at https://github.com/matklad/once_cell/tree/v1.21.3
//!
//! # Implementation details
//!
//! The implementation is heavily based on the
//! [`once_cell`](https://github.com/matklad/once_cell) crate by @matklad, especially the
//! [implementation for parking-lot](https://github.com/matklad/once_cell/blob/master/src/imp_pl.rs).
// This crate must not panic: a single reachable panic pulls `core::panicking` and the formatting
// machinery into every binary that links it, and breaks users who forbid panics outright. These
// lints catch the ways a panic is usually introduced; `ci/no-panic.sh` checks the compiled output
// for the ways they cannot see, such as `assert!`.
use ;
/// Defines a function that is `const` everywhere except under `--cfg loom`, whose atomics cannot
/// be constructed in a const context. The body is written once.
use OnceCell as Imp;
use crate;
/// The outcome of a successful [`OnceCell::get_or_insert`] call.
///
/// Either variant means the cell holds a value and that [`stored`](Self::stored) hands out a
/// reference to it. They differ only in _whose_ value that is: the one passed to `get_or_insert`,
/// or one that an earlier caller had already put there.
///
/// # Example
///
/// ```
/// use once_cell_no_std::{Insertion, OnceCell};
///
/// let cell = OnceCell::new();
///
/// // the cell was empty, so the value went in
/// let insertion = cell.get_or_insert(92).unwrap();
/// assert_eq!(insertion, Insertion::Inserted(&92));
/// assert!(insertion.was_inserted());
///
/// // the cell was full, so the value is handed back instead of being dropped
/// let insertion = cell.get_or_insert(62).unwrap();
/// assert_eq!(insertion, Insertion::AlreadyInitialized { stored: &92, rejected: 62 });
/// assert_eq!(insertion.into_rejected_value(), Some(62));
///
/// // either way, `stored` is the value that is in the cell
/// assert_eq!(cell.get_or_insert(17).unwrap().stored(), &92);
/// ```
/// A thread-safe cell which can be written to only once.
///
/// `OnceCell` provides `&` references to the contents without RAII guards.
///
/// Reading a non-`None` value out of `OnceCell` establishes a
/// happens-before relationship with a corresponding write. For example, if
/// thread A initializes the cell with `get_or_init(f)`, and thread B
/// subsequently reads the result of this call, B also observes all the side
/// effects of `f`.
///
/// `OnceCell` guarantees that at most one initialization function will be called to compute the
/// value. If two threads of execution call [`get_or_init`](Self::get_or_init) (or similar) concurrently, one of them
/// will return a [ConcurrentInitialization] error. It's up to the caller to decide how to handle
/// this error (e.g. wait and retry until the value is initialized by the other thread or panic if
/// this situation is unexpected).
///
/// The alternative to returning the [ConcurrentInitialization] error would be to let one of the
/// threads wait. If this is what you prefer, check out the original
/// [`once_cell::OnceCell`](https://docs.rs/once_cell/1.21.3/once_cell/sync/struct.OnceCell.html)
/// type that this crate is forked from. Note that waiting requires some form of OS support, but
/// also supports `no_std` use cases through its `critical-section` feature.
///
/// # Example
/// ```
/// use once_cell_no_std::OnceCell;
///
/// static CELL: OnceCell<String> = OnceCell::new();
/// assert!(CELL.get().is_none());
///
/// std::thread::spawn(|| {
/// let value: &String = CELL.get_or_init(|| {
/// "Hello, World!".to_string()
/// }).unwrap();
/// assert_eq!(value, "Hello, World!");
/// }).join().unwrap();
///
/// let value: Option<&String> = CELL.get();
/// assert!(value.is_some());
/// assert_eq!(value.unwrap().as_str(), "Hello, World!");
/// ```
///
/// # Handling concurrent initialization
///
/// Since this type never blocks, it is up to the caller to decide what to do when it runs into a
/// concurrent initialization. Every method that can run into it reports it as an explicit error,
/// so a caller that wants to wait can simply retry. See
/// [Waiting for a concurrent initialization](crate#waiting-for-a-concurrent-initialization) for a
/// spin-retry helper, how it compares to `spin::Once` and `lazy_static`, and when spinning is the
/// wrong choice.
///
/// If the value already exists instead of being computed by an init function, use
/// [`set`](Self::set) or [`get_or_insert`](Self::get_or_insert) in the same way: their errors hand
/// the value back, so the retry does not need to clone it.
;
/// Clones the cell, and the value it holds if there is one.
///
/// A cell that another caller is currently initializing counts as empty, because this type never
/// blocks: the clone is empty, and initializing it later has no effect on the original. Cloning a
/// cell that is being written to concurrently is therefore racy by nature — whether the value is
/// carried over depends on the timing.
/// Compares the values the two cells hold, if any.
///
/// Two empty cells are equal, and an empty cell differs from a full one. A cell that another
/// caller is currently initializing counts as empty, so it compares equal to an empty cell and
/// unequal to a full one — including to the value it is about to hold. Comparing a cell that is
/// being written to concurrently is therefore racy by nature, and the result can change from one
/// call to the next.
/// A snapshot of the state of a [`OnceCell`], returned by [`OnceCell::state`].
///
/// # This is an observation, not a decision
///
/// The returned state describes the cell at the moment of the call and may have changed again by
/// the time it is inspected. It is meant for reporting: logging, diagnostics, health checks, and
/// tests. Driving control flow from it is a mistake, because neither of the two "not available"
/// states supports the conclusion it seems to invite:
///
/// - [`Uninitialized`](Self::Uninitialized) does not mean an initialization will succeed. Another
/// caller may start one before you do.
/// - [`Initializing`](Self::Initializing) does not mean an initialization will complete. The init
/// function may fail or panic and return the cell to `Uninitialized`, so a caller that waits for
/// it to finish may wait forever.
///
/// Use [`get_or_init`](OnceCell::get_or_init), [`set`](OnceCell::set), or
/// [`get_or_insert`](OnceCell::get_or_insert) when the answer has to be acted upon: they resolve the race
/// atomically and report contention as of the instant of the attempt.
///
/// [`Initialized`](Self::Initialized) is the one state that is stable: a cell only leaves it
/// through `&mut` access, which no other caller can hold at the same time. Observing it therefore
/// does guarantee that a subsequent [`get`](OnceCell::get) returns `Some`.
///
/// # Example
///
/// ```
/// use once_cell_no_std::{CellState, OnceCell};
///
/// let cell = OnceCell::new();
/// assert_eq!(cell.state(), CellState::Uninitialized);
///
/// cell.set(92).unwrap();
/// assert_eq!(cell.state(), CellState::Initialized);
/// ```