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
// Forked from `once_cell v1.21.3` crate by @matklad
// Original code available at https://github.com/matklad/once_cell/tree/v1.21.3
//! # Overview
//!
//! `once_cell_no_std` provides a `no_std` [`OnceCell`] type that implements [`Sync`] and can be used in
//! statics. It does _not_ use spinlocks or any other form of blocking. Instead, concurrent
//! initialization is reported as an explicit `ConcurrentInitialization` error that the caller can
//! handle as it likes.
//!
//! `OnceCell` might store arbitrary non-`Copy` types, can be assigned to at most once and provide direct access
//! to the stored contents. In a nutshell, API looks *roughly* like this:
//!
//! ```rust,ignore
//! impl OnceCell<T> {
//! fn new() -> OnceCell<T> { ... }
//! fn set(&self, value: T) -> Result<Result<(), T>, ConcurrentInitialization> { ... }
//! fn get(&self) -> Option<&T> { ... }
//! }
//! ```
//!
//! Note that the `set` method requires only a shared reference, so it can also be used in
//! non-mutable `static`s.
//!
//! ## Example
//!
//! ```rust
//! use std::{env, io};
//!
//! use once_cell_no_std::OnceCell;
//!
//! #[derive(Debug)]
//! pub struct Logger {
//! // ...
//! }
//! static INSTANCE: OnceCell<Logger> = OnceCell::new();
//!
//! impl Logger {
//! pub fn global() -> &'static Logger {
//! INSTANCE.get().expect("logger is not initialized")
//! }
//!
//! fn from_cli(args: env::Args) -> Result<Logger, std::io::Error> {
//! // ...
//! # Ok(Logger {})
//! }
//! }
//!
//! fn main() {
//! let logger = Logger::from_cli(env::args()).unwrap();
//! INSTANCE.set(logger).unwrap();
//! // use `Logger::global()` from now on
//! }
//! ```
//!
//! # 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).
//!
//! # Related crates
//!
//! This crate was forked from the great
//! [`once_cell` crate](https://docs.rs/once_cell/1.21.3/once_cell/). The original `once_cell` crate
//! provides two flavors of `OnceCell` types: [`unsync::OnceCell`][unsync-once-cell] and
//! [`sync::OnceCell`][sync-once-cell]. The following
//! table compares the types against `once_cell_no_std::OnceCell`:
//!
//! | | `once_cell_no_std::OnceCell` | [`once_cell::sync::OnceCell`][sync-once-cell] | [`once_cell::unsync::OnceCell`][unsync-once-cell] |
//! | ---------------------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- |
//! | implements `Sync` | yes | yes | no |
//! | concurrent initialization leads to | `ConcurrentInitialization` error returned | thread blocked | cannot happen |
//! | `no_std` supported | yes | partially (requires [`critical-section`](https://docs.rs/critical-section/latest/critical_section/) implementation) | yes |
//!
//! Parts of `once_cell` API are included into `std`/`core` [as of Rust 1.70.0](https://github.com/rust-lang/rust/pull/105587).
//! The following table compares `once_cell_no_std::OnceCell` against the [`core::cell::OnceCell`] and [`std::sync::OnceLock`] types:
//!
//! | | `once_cell_no_std::OnceCell` | [`std::sync::OnceLock`] | [`core::cell::OnceCell`] |
//! | ---------------------------------- | ----------------------------------------- | ----------------------- | ------------------------ |
//! | implements `Sync` | yes | yes | no |
//! | concurrent initialization leads to | `ConcurrentInitialization` error returned | thread blocked | cannot happen |
//! | `no_std` supported | yes | no | yes |
//!
//! For more related crates, check out the [README of `once_cell`](https://github.com/matklad/once_cell?tab=readme-ov-file#related-crates).
//!
//! [unsync-once-cell]: https://docs.rs/once_cell/1.21.3/once_cell/unsync/struct.OnceCell.html
//! [sync-once-cell]: https://docs.rs/once_cell/1.21.3/once_cell/sync/struct.OnceCell.html
//! [`core::cell::OnceCell`]: https://doc.rust-lang.org/stable/core/cell/struct.OnceCell.html
//! [`std::sync::OnceLock`]: https://doc.rust-lang.org/stable/std/sync/struct.OnceLock.html
use ;
use OnceCell as Imp;
use crateConcurrentInitialization;
/// 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!");
/// ```
;