]> git.lizzy.rs Git - rust.git/blob - library/std/src/lazy.rs
Rollup merge of #74814 - matklad:unwind-safe, r=KodrAus
[rust.git] / library / std / src / lazy.rs
1 //! Lazy values and one-time initialization of static data.
2
3 use crate::{
4     cell::{Cell, UnsafeCell},
5     fmt,
6     mem::{self, MaybeUninit},
7     ops::{Deref, Drop},
8     panic::{RefUnwindSafe, UnwindSafe},
9     sync::Once,
10 };
11
12 #[doc(inline)]
13 #[unstable(feature = "once_cell", issue = "74465")]
14 pub use core::lazy::*;
15
16 /// A synchronization primitive which can be written to only once.
17 ///
18 /// This type is a thread-safe `OnceCell`.
19 ///
20 /// # Examples
21 ///
22 /// ```
23 /// #![feature(once_cell)]
24 ///
25 /// use std::lazy::SyncOnceCell;
26 ///
27 /// static CELL: SyncOnceCell<String> = SyncOnceCell::new();
28 /// assert!(CELL.get().is_none());
29 ///
30 /// std::thread::spawn(|| {
31 ///     let value: &String = CELL.get_or_init(|| {
32 ///         "Hello, World!".to_string()
33 ///     });
34 ///     assert_eq!(value, "Hello, World!");
35 /// }).join().unwrap();
36 ///
37 /// let value: Option<&String> = CELL.get();
38 /// assert!(value.is_some());
39 /// assert_eq!(value.unwrap().as_str(), "Hello, World!");
40 /// ```
41 #[unstable(feature = "once_cell", issue = "74465")]
42 pub struct SyncOnceCell<T> {
43     once: Once,
44     // Whether or not the value is initialized is tracked by `state_and_queue`.
45     value: UnsafeCell<MaybeUninit<T>>,
46 }
47
48 // Why do we need `T: Send`?
49 // Thread A creates a `SyncOnceCell` and shares it with
50 // scoped thread B, which fills the cell, which is
51 // then destroyed by A. That is, destructor observes
52 // a sent value.
53 #[unstable(feature = "once_cell", issue = "74465")]
54 unsafe impl<T: Sync + Send> Sync for SyncOnceCell<T> {}
55 #[unstable(feature = "once_cell", issue = "74465")]
56 unsafe impl<T: Send> Send for SyncOnceCell<T> {}
57
58 #[unstable(feature = "once_cell", issue = "74465")]
59 impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for SyncOnceCell<T> {}
60 #[unstable(feature = "once_cell", issue = "74465")]
61 impl<T: UnwindSafe> UnwindSafe for SyncOnceCell<T> {}
62
63 #[unstable(feature = "once_cell", issue = "74465")]
64 impl<T> Default for SyncOnceCell<T> {
65     fn default() -> SyncOnceCell<T> {
66         SyncOnceCell::new()
67     }
68 }
69
70 #[unstable(feature = "once_cell", issue = "74465")]
71 impl<T: fmt::Debug> fmt::Debug for SyncOnceCell<T> {
72     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73         match self.get() {
74             Some(v) => f.debug_tuple("Once").field(v).finish(),
75             None => f.write_str("Once(Uninit)"),
76         }
77     }
78 }
79
80 #[unstable(feature = "once_cell", issue = "74465")]
81 impl<T: Clone> Clone for SyncOnceCell<T> {
82     fn clone(&self) -> SyncOnceCell<T> {
83         let cell = Self::new();
84         if let Some(value) = self.get() {
85             match cell.set(value.clone()) {
86                 Ok(()) => (),
87                 Err(_) => unreachable!(),
88             }
89         }
90         cell
91     }
92 }
93
94 #[unstable(feature = "once_cell", issue = "74465")]
95 impl<T> From<T> for SyncOnceCell<T> {
96     fn from(value: T) -> Self {
97         let cell = Self::new();
98         match cell.set(value) {
99             Ok(()) => cell,
100             Err(_) => unreachable!(),
101         }
102     }
103 }
104
105 #[unstable(feature = "once_cell", issue = "74465")]
106 impl<T: PartialEq> PartialEq for SyncOnceCell<T> {
107     fn eq(&self, other: &SyncOnceCell<T>) -> bool {
108         self.get() == other.get()
109     }
110 }
111
112 #[unstable(feature = "once_cell", issue = "74465")]
113 impl<T: Eq> Eq for SyncOnceCell<T> {}
114
115 impl<T> SyncOnceCell<T> {
116     /// Creates a new empty cell.
117     #[unstable(feature = "once_cell", issue = "74465")]
118     pub const fn new() -> SyncOnceCell<T> {
119         SyncOnceCell { once: Once::new(), value: UnsafeCell::new(MaybeUninit::uninit()) }
120     }
121
122     /// Gets the reference to the underlying value.
123     ///
124     /// Returns `None` if the cell is empty, or being initialized. This
125     /// method never blocks.
126     #[unstable(feature = "once_cell", issue = "74465")]
127     pub fn get(&self) -> Option<&T> {
128         if self.is_initialized() {
129             // Safe b/c checked is_initialized
130             Some(unsafe { self.get_unchecked() })
131         } else {
132             None
133         }
134     }
135
136     /// Gets the mutable reference to the underlying value.
137     ///
138     /// Returns `None` if the cell is empty. This method never blocks.
139     #[unstable(feature = "once_cell", issue = "74465")]
140     pub fn get_mut(&mut self) -> Option<&mut T> {
141         if self.is_initialized() {
142             // Safe b/c checked is_initialized and we have a unique access
143             Some(unsafe { self.get_unchecked_mut() })
144         } else {
145             None
146         }
147     }
148
149     /// Sets the contents of this cell to `value`.
150     ///
151     /// Returns `Ok(())` if the cell's value was updated.
152     ///
153     /// # Examples
154     ///
155     /// ```
156     /// #![feature(once_cell)]
157     ///
158     /// use std::lazy::SyncOnceCell;
159     ///
160     /// static CELL: SyncOnceCell<i32> = SyncOnceCell::new();
161     ///
162     /// fn main() {
163     ///     assert!(CELL.get().is_none());
164     ///
165     ///     std::thread::spawn(|| {
166     ///         assert_eq!(CELL.set(92), Ok(()));
167     ///     }).join().unwrap();
168     ///
169     ///     assert_eq!(CELL.set(62), Err(62));
170     ///     assert_eq!(CELL.get(), Some(&92));
171     /// }
172     /// ```
173     #[unstable(feature = "once_cell", issue = "74465")]
174     pub fn set(&self, value: T) -> Result<(), T> {
175         let mut value = Some(value);
176         self.get_or_init(|| value.take().unwrap());
177         match value {
178             None => Ok(()),
179             Some(value) => Err(value),
180         }
181     }
182
183     /// Gets the contents of the cell, initializing it with `f` if the cell
184     /// was empty.
185     ///
186     /// Many threads may call `get_or_init` concurrently with different
187     /// initializing functions, but it is guaranteed that only one function
188     /// will be executed.
189     ///
190     /// # Panics
191     ///
192     /// If `f` panics, the panic is propagated to the caller, and the cell
193     /// remains uninitialized.
194     ///
195     /// It is an error to reentrantly initialize the cell from `f`. The
196     /// exact outcome is unspecified. Current implementation deadlocks, but
197     /// this may be changed to a panic in the future.
198     ///
199     /// # Examples
200     ///
201     /// ```
202     /// #![feature(once_cell)]
203     ///
204     /// use std::lazy::SyncOnceCell;
205     ///
206     /// let cell = SyncOnceCell::new();
207     /// let value = cell.get_or_init(|| 92);
208     /// assert_eq!(value, &92);
209     /// let value = cell.get_or_init(|| unreachable!());
210     /// assert_eq!(value, &92);
211     /// ```
212     #[unstable(feature = "once_cell", issue = "74465")]
213     pub fn get_or_init<F>(&self, f: F) -> &T
214     where
215         F: FnOnce() -> T,
216     {
217         match self.get_or_try_init(|| Ok::<T, !>(f())) {
218             Ok(val) => val,
219         }
220     }
221
222     /// Gets the contents of the cell, initializing it with `f` if
223     /// the cell was empty. If the cell was empty and `f` failed, an
224     /// error is returned.
225     ///
226     /// # Panics
227     ///
228     /// If `f` panics, the panic is propagated to the caller, and
229     /// the cell remains uninitialized.
230     ///
231     /// It is an error to reentrantly initialize the cell from `f`.
232     /// The exact outcome is unspecified. Current implementation
233     /// deadlocks, but this may be changed to a panic in the future.
234     ///
235     /// # Examples
236     ///
237     /// ```
238     /// #![feature(once_cell)]
239     ///
240     /// use std::lazy::SyncOnceCell;
241     ///
242     /// let cell = SyncOnceCell::new();
243     /// assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
244     /// assert!(cell.get().is_none());
245     /// let value = cell.get_or_try_init(|| -> Result<i32, ()> {
246     ///     Ok(92)
247     /// });
248     /// assert_eq!(value, Ok(&92));
249     /// assert_eq!(cell.get(), Some(&92))
250     /// ```
251     #[unstable(feature = "once_cell", issue = "74465")]
252     pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
253     where
254         F: FnOnce() -> Result<T, E>,
255     {
256         // Fast path check
257         // NOTE: We need to perform an acquire on the state in this method
258         // in order to correctly synchronize `SyncLazy::force`. This is
259         // currently done by calling `self.get()`, which in turn calls
260         // `self.is_initialized()`, which in turn performs the acquire.
261         if let Some(value) = self.get() {
262             return Ok(value);
263         }
264         self.initialize(f)?;
265
266         debug_assert!(self.is_initialized());
267
268         // Safety: The inner value has been initialized
269         Ok(unsafe { self.get_unchecked() })
270     }
271
272     /// Consumes the `SyncOnceCell`, returning the wrapped value. Returns
273     /// `None` if the cell was empty.
274     ///
275     /// # Examples
276     ///
277     /// ```
278     /// #![feature(once_cell)]
279     ///
280     /// use std::lazy::SyncOnceCell;
281     ///
282     /// let cell: SyncOnceCell<String> = SyncOnceCell::new();
283     /// assert_eq!(cell.into_inner(), None);
284     ///
285     /// let cell = SyncOnceCell::new();
286     /// cell.set("hello".to_string()).unwrap();
287     /// assert_eq!(cell.into_inner(), Some("hello".to_string()));
288     /// ```
289     #[unstable(feature = "once_cell", issue = "74465")]
290     pub fn into_inner(mut self) -> Option<T> {
291         // Safety: Safe because we immediately free `self` without dropping
292         let inner = unsafe { self.take_inner() };
293
294         // Don't drop this `SyncOnceCell`. We just moved out one of the fields, but didn't set
295         // the state to uninitialized.
296         mem::ManuallyDrop::new(self);
297         inner
298     }
299
300     /// Takes the value out of this `SyncOnceCell`, moving it back to an uninitialized state.
301     ///
302     /// Has no effect and returns `None` if the `SyncOnceCell` hasn't been initialized.
303     ///
304     /// Safety is guaranteed by requiring a mutable reference.
305     ///
306     /// # Examples
307     ///
308     /// ```
309     /// #![feature(once_cell)]
310     ///
311     /// use std::lazy::SyncOnceCell;
312     ///
313     /// let mut cell: SyncOnceCell<String> = SyncOnceCell::new();
314     /// assert_eq!(cell.take(), None);
315     ///
316     /// let mut cell = SyncOnceCell::new();
317     /// cell.set("hello".to_string()).unwrap();
318     /// assert_eq!(cell.take(), Some("hello".to_string()));
319     /// assert_eq!(cell.get(), None);
320     /// ```
321     #[unstable(feature = "once_cell", issue = "74465")]
322     pub fn take(&mut self) -> Option<T> {
323         mem::take(self).into_inner()
324     }
325
326     /// Takes the wrapped value out of a `SyncOnceCell`.
327     /// Afterwards the cell is no longer initialized.
328     ///
329     /// Safety: The cell must now be free'd WITHOUT dropping. No other usages of the cell
330     /// are valid. Only used by `into_inner` and `drop`.
331     unsafe fn take_inner(&mut self) -> Option<T> {
332         // The mutable reference guarantees there are no other threads that can observe us
333         // taking out the wrapped value.
334         // Right after this function `self` is supposed to be freed, so it makes little sense
335         // to atomically set the state to uninitialized.
336         if self.is_initialized() {
337             let value = mem::replace(&mut self.value, UnsafeCell::new(MaybeUninit::uninit()));
338             Some(value.into_inner().assume_init())
339         } else {
340             None
341         }
342     }
343
344     #[inline]
345     fn is_initialized(&self) -> bool {
346         self.once.is_completed()
347     }
348
349     #[cold]
350     fn initialize<F, E>(&self, f: F) -> Result<(), E>
351     where
352         F: FnOnce() -> Result<T, E>,
353     {
354         let mut res: Result<(), E> = Ok(());
355         let slot = &self.value;
356
357         // Ignore poisoning from other threads
358         // If another thread panics, then we'll be able to run our closure
359         self.once.call_once_force(|p| {
360             match f() {
361                 Ok(value) => {
362                     unsafe { (&mut *slot.get()).write(value) };
363                 }
364                 Err(e) => {
365                     res = Err(e);
366
367                     // Treat the underlying `Once` as poisoned since we
368                     // failed to initialize our value. Calls
369                     p.poison();
370                 }
371             }
372         });
373         res
374     }
375
376     /// Safety: The value must be initialized
377     unsafe fn get_unchecked(&self) -> &T {
378         debug_assert!(self.is_initialized());
379         (&*self.value.get()).get_ref()
380     }
381
382     /// Safety: The value must be initialized
383     unsafe fn get_unchecked_mut(&mut self) -> &mut T {
384         debug_assert!(self.is_initialized());
385         (&mut *self.value.get()).get_mut()
386     }
387 }
388
389 impl<T> Drop for SyncOnceCell<T> {
390     fn drop(&mut self) {
391         // Safety: The cell is being dropped, so it can't be accessed again
392         unsafe { self.take_inner() };
393     }
394 }
395
396 /// A value which is initialized on the first access.
397 ///
398 /// This type is a thread-safe `Lazy`, and can be used in statics.
399 ///
400 /// # Examples
401 ///
402 /// ```
403 /// #![feature(once_cell)]
404 ///
405 /// use std::collections::HashMap;
406 ///
407 /// use std::lazy::SyncLazy;
408 ///
409 /// static HASHMAP: SyncLazy<HashMap<i32, String>> = SyncLazy::new(|| {
410 ///     println!("initializing");
411 ///     let mut m = HashMap::new();
412 ///     m.insert(13, "Spica".to_string());
413 ///     m.insert(74, "Hoyten".to_string());
414 ///     m
415 /// });
416 ///
417 /// fn main() {
418 ///     println!("ready");
419 ///     std::thread::spawn(|| {
420 ///         println!("{:?}", HASHMAP.get(&13));
421 ///     }).join().unwrap();
422 ///     println!("{:?}", HASHMAP.get(&74));
423 ///
424 ///     // Prints:
425 ///     //   ready
426 ///     //   initializing
427 ///     //   Some("Spica")
428 ///     //   Some("Hoyten")
429 /// }
430 /// ```
431 #[unstable(feature = "once_cell", issue = "74465")]
432 pub struct SyncLazy<T, F = fn() -> T> {
433     cell: SyncOnceCell<T>,
434     init: Cell<Option<F>>,
435 }
436
437 #[unstable(feature = "once_cell", issue = "74465")]
438 impl<T: fmt::Debug, F> fmt::Debug for SyncLazy<T, F> {
439     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
440         f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
441     }
442 }
443
444 // We never create a `&F` from a `&SyncLazy<T, F>` so it is fine
445 // to not impl `Sync` for `F`
446 // we do create a `&mut Option<F>` in `force`, but this is
447 // properly synchronized, so it only happens once
448 // so it also does not contribute to this impl.
449 #[unstable(feature = "once_cell", issue = "74465")]
450 unsafe impl<T, F: Send> Sync for SyncLazy<T, F> where SyncOnceCell<T>: Sync {}
451 // auto-derived `Send` impl is OK.
452
453 #[unstable(feature = "once_cell", issue = "74465")]
454 impl<T, F: UnwindSafe> RefUnwindSafe for SyncLazy<T, F> where SyncOnceCell<T>: RefUnwindSafe {}
455 #[unstable(feature = "once_cell", issue = "74465")]
456 impl<T, F: UnwindSafe> UnwindSafe for SyncLazy<T, F> where SyncOnceCell<T>: UnwindSafe {}
457
458 impl<T, F> SyncLazy<T, F> {
459     /// Creates a new lazy value with the given initializing
460     /// function.
461     #[unstable(feature = "once_cell", issue = "74465")]
462     pub const fn new(f: F) -> SyncLazy<T, F> {
463         SyncLazy { cell: SyncOnceCell::new(), init: Cell::new(Some(f)) }
464     }
465 }
466
467 impl<T, F: FnOnce() -> T> SyncLazy<T, F> {
468     /// Forces the evaluation of this lazy value and
469     /// returns a reference to result. This is equivalent
470     /// to the `Deref` impl, but is explicit.
471     ///
472     /// # Examples
473     ///
474     /// ```
475     /// #![feature(once_cell)]
476     ///
477     /// use std::lazy::SyncLazy;
478     ///
479     /// let lazy = SyncLazy::new(|| 92);
480     ///
481     /// assert_eq!(SyncLazy::force(&lazy), &92);
482     /// assert_eq!(&*lazy, &92);
483     /// ```
484     #[unstable(feature = "once_cell", issue = "74465")]
485     pub fn force(this: &SyncLazy<T, F>) -> &T {
486         this.cell.get_or_init(|| match this.init.take() {
487             Some(f) => f(),
488             None => panic!("Lazy instance has previously been poisoned"),
489         })
490     }
491 }
492
493 #[unstable(feature = "once_cell", issue = "74465")]
494 impl<T, F: FnOnce() -> T> Deref for SyncLazy<T, F> {
495     type Target = T;
496     fn deref(&self) -> &T {
497         SyncLazy::force(self)
498     }
499 }
500
501 #[unstable(feature = "once_cell", issue = "74465")]
502 impl<T: Default> Default for SyncLazy<T> {
503     /// Creates a new lazy value using `Default` as the initializing function.
504     fn default() -> SyncLazy<T> {
505         SyncLazy::new(T::default)
506     }
507 }
508
509 #[cfg(test)]
510 mod tests {
511     use crate::{
512         lazy::{Lazy, SyncLazy, SyncOnceCell},
513         panic,
514         sync::{
515             atomic::{AtomicUsize, Ordering::SeqCst},
516             mpsc::channel,
517             Mutex,
518         },
519     };
520
521     #[test]
522     fn lazy_default() {
523         static CALLED: AtomicUsize = AtomicUsize::new(0);
524
525         struct Foo(u8);
526         impl Default for Foo {
527             fn default() -> Self {
528                 CALLED.fetch_add(1, SeqCst);
529                 Foo(42)
530             }
531         }
532
533         let lazy: Lazy<Mutex<Foo>> = <_>::default();
534
535         assert_eq!(CALLED.load(SeqCst), 0);
536
537         assert_eq!(lazy.lock().unwrap().0, 42);
538         assert_eq!(CALLED.load(SeqCst), 1);
539
540         lazy.lock().unwrap().0 = 21;
541
542         assert_eq!(lazy.lock().unwrap().0, 21);
543         assert_eq!(CALLED.load(SeqCst), 1);
544     }
545
546     #[test]
547     fn lazy_poisoning() {
548         let x: Lazy<String> = Lazy::new(|| panic!("kaboom"));
549         for _ in 0..2 {
550             let res = panic::catch_unwind(panic::AssertUnwindSafe(|| x.len()));
551             assert!(res.is_err());
552         }
553     }
554
555     // miri doesn't support threads
556     #[cfg(not(miri))]
557     fn spawn_and_wait<R: Send + 'static>(f: impl FnOnce() -> R + Send + 'static) -> R {
558         crate::thread::spawn(f).join().unwrap()
559     }
560
561     #[cfg(not(miri))]
562     fn spawn(f: impl FnOnce() + Send + 'static) {
563         let _ = crate::thread::spawn(f);
564     }
565
566     // "stub threads" for Miri
567     #[cfg(miri)]
568     fn spawn_and_wait<R: Send + 'static>(f: impl FnOnce() -> R + Send + 'static) -> R {
569         f(())
570     }
571
572     #[cfg(miri)]
573     fn spawn(f: impl FnOnce() + Send + 'static) {
574         f(())
575     }
576
577     #[test]
578     fn sync_once_cell() {
579         static ONCE_CELL: SyncOnceCell<i32> = SyncOnceCell::new();
580
581         assert!(ONCE_CELL.get().is_none());
582
583         spawn_and_wait(|| {
584             ONCE_CELL.get_or_init(|| 92);
585             assert_eq!(ONCE_CELL.get(), Some(&92));
586         });
587
588         ONCE_CELL.get_or_init(|| panic!("Kabom!"));
589         assert_eq!(ONCE_CELL.get(), Some(&92));
590     }
591
592     #[test]
593     fn sync_once_cell_get_mut() {
594         let mut c = SyncOnceCell::new();
595         assert!(c.get_mut().is_none());
596         c.set(90).unwrap();
597         *c.get_mut().unwrap() += 2;
598         assert_eq!(c.get_mut(), Some(&mut 92));
599     }
600
601     #[test]
602     fn sync_once_cell_get_unchecked() {
603         let c = SyncOnceCell::new();
604         c.set(92).unwrap();
605         unsafe {
606             assert_eq!(c.get_unchecked(), &92);
607         }
608     }
609
610     #[test]
611     fn sync_once_cell_drop() {
612         static DROP_CNT: AtomicUsize = AtomicUsize::new(0);
613         struct Dropper;
614         impl Drop for Dropper {
615             fn drop(&mut self) {
616                 DROP_CNT.fetch_add(1, SeqCst);
617             }
618         }
619
620         let x = SyncOnceCell::new();
621         spawn_and_wait(move || {
622             x.get_or_init(|| Dropper);
623             assert_eq!(DROP_CNT.load(SeqCst), 0);
624             drop(x);
625         });
626
627         assert_eq!(DROP_CNT.load(SeqCst), 1);
628     }
629
630     #[test]
631     fn sync_once_cell_drop_empty() {
632         let x = SyncOnceCell::<String>::new();
633         drop(x);
634     }
635
636     #[test]
637     fn clone() {
638         let s = SyncOnceCell::new();
639         let c = s.clone();
640         assert!(c.get().is_none());
641
642         s.set("hello".to_string()).unwrap();
643         let c = s.clone();
644         assert_eq!(c.get().map(String::as_str), Some("hello"));
645     }
646
647     #[test]
648     fn get_or_try_init() {
649         let cell: SyncOnceCell<String> = SyncOnceCell::new();
650         assert!(cell.get().is_none());
651
652         let res = panic::catch_unwind(|| cell.get_or_try_init(|| -> Result<_, ()> { panic!() }));
653         assert!(res.is_err());
654         assert!(!cell.is_initialized());
655         assert!(cell.get().is_none());
656
657         assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
658
659         assert_eq!(
660             cell.get_or_try_init(|| Ok::<_, ()>("hello".to_string())),
661             Ok(&"hello".to_string())
662         );
663         assert_eq!(cell.get(), Some(&"hello".to_string()));
664     }
665
666     #[test]
667     fn from_impl() {
668         assert_eq!(SyncOnceCell::from("value").get(), Some(&"value"));
669         assert_ne!(SyncOnceCell::from("foo").get(), Some(&"bar"));
670     }
671
672     #[test]
673     fn partialeq_impl() {
674         assert!(SyncOnceCell::from("value") == SyncOnceCell::from("value"));
675         assert!(SyncOnceCell::from("foo") != SyncOnceCell::from("bar"));
676
677         assert!(SyncOnceCell::<String>::new() == SyncOnceCell::new());
678         assert!(SyncOnceCell::<String>::new() != SyncOnceCell::from("value".to_owned()));
679     }
680
681     #[test]
682     fn into_inner() {
683         let cell: SyncOnceCell<String> = SyncOnceCell::new();
684         assert_eq!(cell.into_inner(), None);
685         let cell = SyncOnceCell::new();
686         cell.set("hello".to_string()).unwrap();
687         assert_eq!(cell.into_inner(), Some("hello".to_string()));
688     }
689
690     #[test]
691     fn sync_lazy_new() {
692         static CALLED: AtomicUsize = AtomicUsize::new(0);
693         static SYNC_LAZY: SyncLazy<i32> = SyncLazy::new(|| {
694             CALLED.fetch_add(1, SeqCst);
695             92
696         });
697
698         assert_eq!(CALLED.load(SeqCst), 0);
699
700         spawn_and_wait(|| {
701             let y = *SYNC_LAZY - 30;
702             assert_eq!(y, 62);
703             assert_eq!(CALLED.load(SeqCst), 1);
704         });
705
706         let y = *SYNC_LAZY - 30;
707         assert_eq!(y, 62);
708         assert_eq!(CALLED.load(SeqCst), 1);
709     }
710
711     #[test]
712     fn sync_lazy_default() {
713         static CALLED: AtomicUsize = AtomicUsize::new(0);
714
715         struct Foo(u8);
716         impl Default for Foo {
717             fn default() -> Self {
718                 CALLED.fetch_add(1, SeqCst);
719                 Foo(42)
720             }
721         }
722
723         let lazy: SyncLazy<Mutex<Foo>> = <_>::default();
724
725         assert_eq!(CALLED.load(SeqCst), 0);
726
727         assert_eq!(lazy.lock().unwrap().0, 42);
728         assert_eq!(CALLED.load(SeqCst), 1);
729
730         lazy.lock().unwrap().0 = 21;
731
732         assert_eq!(lazy.lock().unwrap().0, 21);
733         assert_eq!(CALLED.load(SeqCst), 1);
734     }
735
736     #[test]
737     #[cfg_attr(miri, ignore)] // leaks memory
738     fn static_sync_lazy() {
739         static XS: SyncLazy<Vec<i32>> = SyncLazy::new(|| {
740             let mut xs = Vec::new();
741             xs.push(1);
742             xs.push(2);
743             xs.push(3);
744             xs
745         });
746
747         spawn_and_wait(|| {
748             assert_eq!(&*XS, &vec![1, 2, 3]);
749         });
750
751         assert_eq!(&*XS, &vec![1, 2, 3]);
752     }
753
754     #[test]
755     #[cfg_attr(miri, ignore)] // leaks memory
756     fn static_sync_lazy_via_fn() {
757         fn xs() -> &'static Vec<i32> {
758             static XS: SyncOnceCell<Vec<i32>> = SyncOnceCell::new();
759             XS.get_or_init(|| {
760                 let mut xs = Vec::new();
761                 xs.push(1);
762                 xs.push(2);
763                 xs.push(3);
764                 xs
765             })
766         }
767         assert_eq!(xs(), &vec![1, 2, 3]);
768     }
769
770     #[test]
771     fn sync_lazy_poisoning() {
772         let x: SyncLazy<String> = SyncLazy::new(|| panic!("kaboom"));
773         for _ in 0..2 {
774             let res = panic::catch_unwind(|| x.len());
775             assert!(res.is_err());
776         }
777     }
778
779     #[test]
780     fn is_sync_send() {
781         fn assert_traits<T: Send + Sync>() {}
782         assert_traits::<SyncOnceCell<String>>();
783         assert_traits::<SyncLazy<String>>();
784     }
785
786     #[test]
787     fn eval_once_macro() {
788         macro_rules! eval_once {
789             (|| -> $ty:ty {
790                 $($body:tt)*
791             }) => {{
792                 static ONCE_CELL: SyncOnceCell<$ty> = SyncOnceCell::new();
793                 fn init() -> $ty {
794                     $($body)*
795                 }
796                 ONCE_CELL.get_or_init(init)
797             }};
798         }
799
800         let fib: &'static Vec<i32> = eval_once! {
801             || -> Vec<i32> {
802                 let mut res = vec![1, 1];
803                 for i in 0..10 {
804                     let next = res[i] + res[i + 1];
805                     res.push(next);
806                 }
807                 res
808             }
809         };
810         assert_eq!(fib[5], 8)
811     }
812
813     #[test]
814     #[cfg_attr(miri, ignore)] // deadlocks without real threads
815     fn sync_once_cell_does_not_leak_partially_constructed_boxes() {
816         static ONCE_CELL: SyncOnceCell<String> = SyncOnceCell::new();
817
818         let n_readers = 10;
819         let n_writers = 3;
820         const MSG: &str = "Hello, World";
821
822         let (tx, rx) = channel();
823
824         for _ in 0..n_readers {
825             let tx = tx.clone();
826             spawn(move || {
827                 loop {
828                     if let Some(msg) = ONCE_CELL.get() {
829                         tx.send(msg).unwrap();
830                         break;
831                     }
832                     #[cfg(target_env = "sgx")]
833                     crate::thread::yield_now();
834                 }
835             });
836         }
837         for _ in 0..n_writers {
838             spawn(move || {
839                 let _ = ONCE_CELL.set(MSG.to_owned());
840             });
841         }
842
843         for _ in 0..n_readers {
844             let msg = rx.recv().unwrap();
845             assert_eq!(msg, MSG);
846         }
847     }
848 }