2 lazy::{Lazy, SyncLazy, SyncOnceCell},
5 atomic::{AtomicUsize, Ordering::SeqCst},
14 static CALLED: AtomicUsize = AtomicUsize::new(0);
17 impl Default for Foo {
18 fn default() -> Self {
19 CALLED.fetch_add(1, SeqCst);
24 let lazy: Lazy<Mutex<Foo>> = <_>::default();
26 assert_eq!(CALLED.load(SeqCst), 0);
28 assert_eq!(lazy.lock().unwrap().0, 42);
29 assert_eq!(CALLED.load(SeqCst), 1);
31 lazy.lock().unwrap().0 = 21;
33 assert_eq!(lazy.lock().unwrap().0, 21);
34 assert_eq!(CALLED.load(SeqCst), 1);
39 let x: Lazy<String> = Lazy::new(|| panic!("kaboom"));
41 let res = panic::catch_unwind(panic::AssertUnwindSafe(|| x.len()));
42 assert!(res.is_err());
46 fn spawn_and_wait<R: Send + 'static>(f: impl FnOnce() -> R + Send + 'static) -> R {
47 thread::spawn(f).join().unwrap()
51 #[cfg_attr(target_os = "emscripten", ignore)]
53 static ONCE_CELL: SyncOnceCell<i32> = SyncOnceCell::new();
55 assert!(ONCE_CELL.get().is_none());
58 ONCE_CELL.get_or_init(|| 92);
59 assert_eq!(ONCE_CELL.get(), Some(&92));
62 ONCE_CELL.get_or_init(|| panic!("Kabom!"));
63 assert_eq!(ONCE_CELL.get(), Some(&92));
67 fn sync_once_cell_get_mut() {
68 let mut c = SyncOnceCell::new();
69 assert!(c.get_mut().is_none());
71 *c.get_mut().unwrap() += 2;
72 assert_eq!(c.get_mut(), Some(&mut 92));
76 fn sync_once_cell_get_unchecked() {
77 let c = SyncOnceCell::new();
80 assert_eq!(c.get_unchecked(), &92);
85 #[cfg_attr(target_os = "emscripten", ignore)]
86 fn sync_once_cell_drop() {
87 static DROP_CNT: AtomicUsize = AtomicUsize::new(0);
89 impl Drop for Dropper {
91 DROP_CNT.fetch_add(1, SeqCst);
95 let x = SyncOnceCell::new();
96 spawn_and_wait(move || {
97 x.get_or_init(|| Dropper);
98 assert_eq!(DROP_CNT.load(SeqCst), 0);
102 assert_eq!(DROP_CNT.load(SeqCst), 1);
106 fn sync_once_cell_drop_empty() {
107 let x = SyncOnceCell::<String>::new();
113 let s = SyncOnceCell::new();
115 assert!(c.get().is_none());
117 s.set("hello".to_string()).unwrap();
119 assert_eq!(c.get().map(String::as_str), Some("hello"));
123 fn get_or_try_init() {
124 let cell: SyncOnceCell<String> = SyncOnceCell::new();
125 assert!(cell.get().is_none());
127 let res = panic::catch_unwind(|| cell.get_or_try_init(|| -> Result<_, ()> { panic!() }));
128 assert!(res.is_err());
129 assert!(!cell.is_initialized());
130 assert!(cell.get().is_none());
132 assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
134 assert_eq!(cell.get_or_try_init(|| Ok::<_, ()>("hello".to_string())), Ok(&"hello".to_string()));
135 assert_eq!(cell.get(), Some(&"hello".to_string()));
140 assert_eq!(SyncOnceCell::from("value").get(), Some(&"value"));
141 assert_ne!(SyncOnceCell::from("foo").get(), Some(&"bar"));
145 fn partialeq_impl() {
146 assert!(SyncOnceCell::from("value") == SyncOnceCell::from("value"));
147 assert!(SyncOnceCell::from("foo") != SyncOnceCell::from("bar"));
149 assert!(SyncOnceCell::<String>::new() == SyncOnceCell::new());
150 assert!(SyncOnceCell::<String>::new() != SyncOnceCell::from("value".to_owned()));
155 let cell: SyncOnceCell<String> = SyncOnceCell::new();
156 assert_eq!(cell.into_inner(), None);
157 let cell = SyncOnceCell::new();
158 cell.set("hello".to_string()).unwrap();
159 assert_eq!(cell.into_inner(), Some("hello".to_string()));
163 #[cfg_attr(target_os = "emscripten", ignore)]
165 static CALLED: AtomicUsize = AtomicUsize::new(0);
166 static SYNC_LAZY: SyncLazy<i32> = SyncLazy::new(|| {
167 CALLED.fetch_add(1, SeqCst);
171 assert_eq!(CALLED.load(SeqCst), 0);
174 let y = *SYNC_LAZY - 30;
176 assert_eq!(CALLED.load(SeqCst), 1);
179 let y = *SYNC_LAZY - 30;
181 assert_eq!(CALLED.load(SeqCst), 1);
185 fn sync_lazy_default() {
186 static CALLED: AtomicUsize = AtomicUsize::new(0);
189 impl Default for Foo {
190 fn default() -> Self {
191 CALLED.fetch_add(1, SeqCst);
196 let lazy: SyncLazy<Mutex<Foo>> = <_>::default();
198 assert_eq!(CALLED.load(SeqCst), 0);
200 assert_eq!(lazy.lock().unwrap().0, 42);
201 assert_eq!(CALLED.load(SeqCst), 1);
203 lazy.lock().unwrap().0 = 21;
205 assert_eq!(lazy.lock().unwrap().0, 21);
206 assert_eq!(CALLED.load(SeqCst), 1);
210 #[cfg_attr(target_os = "emscripten", ignore)]
211 fn static_sync_lazy() {
212 static XS: SyncLazy<Vec<i32>> = SyncLazy::new(|| {
213 let mut xs = Vec::new();
221 assert_eq!(&*XS, &vec![1, 2, 3]);
224 assert_eq!(&*XS, &vec![1, 2, 3]);
228 fn static_sync_lazy_via_fn() {
229 fn xs() -> &'static Vec<i32> {
230 static XS: SyncOnceCell<Vec<i32>> = SyncOnceCell::new();
232 let mut xs = Vec::new();
239 assert_eq!(xs(), &vec![1, 2, 3]);
243 fn sync_lazy_poisoning() {
244 let x: SyncLazy<String> = SyncLazy::new(|| panic!("kaboom"));
246 let res = panic::catch_unwind(|| x.len());
247 assert!(res.is_err());
253 fn assert_traits<T: Send + Sync>() {}
254 assert_traits::<SyncOnceCell<String>>();
255 assert_traits::<SyncLazy<String>>();
259 fn eval_once_macro() {
260 macro_rules! eval_once {
264 static ONCE_CELL: SyncOnceCell<$ty> = SyncOnceCell::new();
268 ONCE_CELL.get_or_init(init)
272 let fib: &'static Vec<i32> = eval_once! {
274 let mut res = vec![1, 1];
276 let next = res[i] + res[i + 1];
282 assert_eq!(fib[5], 8)
286 #[cfg_attr(target_os = "emscripten", ignore)]
287 fn sync_once_cell_does_not_leak_partially_constructed_boxes() {
288 static ONCE_CELL: SyncOnceCell<String> = SyncOnceCell::new();
292 const MSG: &str = "Hello, World";
294 let (tx, rx) = channel();
296 for _ in 0..n_readers {
298 thread::spawn(move || {
300 if let Some(msg) = ONCE_CELL.get() {
301 tx.send(msg).unwrap();
304 #[cfg(target_env = "sgx")]
305 crate::thread::yield_now();
309 for _ in 0..n_writers {
310 thread::spawn(move || {
311 let _ = ONCE_CELL.set(MSG.to_owned());
315 for _ in 0..n_readers {
316 let msg = rx.recv().unwrap();
317 assert_eq!(msg, MSG);
323 let cell = SyncOnceCell::new();
325 let s = String::new();
326 cell.set(&s).unwrap();