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
//! ## async once tool for lazy_static
//!
//! # Examples
//! ```rust
//!    use lazy_static::lazy_static;
//!    use tokio::runtime::Builder;
//!    use async_once::AsyncOnce;
//!
//!    lazy_static!{
//!        static ref FOO : AsyncOnce<u32> = AsyncOnce::new(async{
//!            1
//!        });
//!    }
//!    let rt = Builder::new_current_thread().build().unwrap();
//!    rt.block_on(async {
//!        assert_eq!(FOO.get().await , &1)
//!    })
//! ```
//!
//! ### run tests
//!
//! ```bash
//!    cargo test
//!    wasm-pack test --headless --chrome --firefox
//! ```
//!

use std::cell::Cell;
use std::future::Future;
use std::pin::Pin;
use std::ptr::null;
use std::sync::Arc;
use std::sync::Mutex;
use std::task::Context;
use std::task::Poll;
use std::task::Wake;
use std::task::Waker;

type Fut<T> = Mutex<Result<T, Pin<Box<dyn Future<Output = T>>>>>;
pub struct AsyncOnce<T: 'static> {
    ptr: Cell<*const T>,
    fut: Fut<T>,
    waker: Arc<MyWaker>,
}

unsafe impl<T: 'static> Sync for AsyncOnce<T> {}

impl<T> AsyncOnce<T> {
    pub fn new<F>(fut: F) -> AsyncOnce<T>
    where
        F: Future<Output = T> + 'static,
    {
        AsyncOnce {
            ptr: Cell::new(null()),
            fut: Mutex::new(Err(Box::pin(fut))),
            waker: Arc::new(MyWaker {
                wakers: Mutex::new(Vec::with_capacity(16)),
            }),
        }
    }
    #[inline(always)]
    pub fn get(&'static self) -> &'static Self {
        self
    }
}

struct MyWaker {
    wakers: Mutex<Vec<Waker>>,
}

impl Wake for MyWaker {
    fn wake_by_ref(self: &std::sync::Arc<Self>) {
        self.clone().wake();
    }

    fn wake(self: std::sync::Arc<Self>) {
        let mut wakers = self.wakers.lock().unwrap();
        while let Some(waker) = wakers.pop() {
            waker.wake();
        }
        drop(wakers);
    }
}

impl<T> Future for &'static AsyncOnce<T> {
    type Output = &'static T;
    #[inline(always)]
    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<&'static T> {
        if let Some(ptr) = unsafe { self.ptr.get().as_ref() } {
            return Poll::Ready(ptr);
        }
        let cxwaker = cx.waker().clone();
        let mut wakers = self.waker.wakers.lock().unwrap();
        let is_first = wakers.is_empty();
        if !wakers.iter().any(|wk| wk.will_wake(&cxwaker)) {
            wakers.push(cxwaker);
        }
        drop(wakers);
        let mut result = None;
        let mut fut = self.fut.lock().unwrap();
        match (is_first, fut.as_mut()) {
            (true, Err(fut)) => {
                let waker = Waker::from(self.waker.clone());
                let mut ctx = Context::from_waker(&waker);
                match Pin::new(fut).poll(&mut ctx) {
                    Poll::Ready(res) => {
                        result = Some(res);
                    }
                    Poll::Pending => {
                        return Poll::Pending;
                    }
                }
            }
            (true, Ok(res)) => {
                return Poll::Ready(unsafe { (res as *const T).as_ref().unwrap() });
            }
            _ => (),
        }
        if let Some(res) = result {
            *fut = Ok(res);
            let ptr = fut.as_ref().ok().unwrap() as *const T;
            self.ptr.set(ptr);
            drop(fut);
            let mut wakers = self.waker.wakers.lock().unwrap();
            while let Some(waker) = wakers.pop() {
                waker.wake();
            }
            drop(wakers);
            return Poll::Ready(unsafe { &*ptr });
        }
        drop(fut);
        Poll::Pending
    }
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn lazy_static_test_for_tokio() {
    use futures_timer::Delay;
    use lazy_static::lazy_static;
    use std::time::Duration;
    use tokio::runtime::Builder;
    lazy_static! {
        static ref FOO: AsyncOnce<u32> = AsyncOnce::new(async {
            tokio::spawn(async { assert_eq!(FOO.get().await, &1) });
            Delay::new(Duration::from_millis(100)).await;
            1
        });
    }
    let rt = Builder::new_current_thread().build().unwrap();
    let handle1 = rt.spawn(async {
        Delay::new(Duration::from_millis(100)).await;
        assert_eq!(FOO.get().await, &1)
    });
    let handle2 = rt.spawn(async {
        Delay::new(Duration::from_millis(150)).await;
        assert_eq!(FOO.get().await, &1)
    });
    rt.block_on(async {
        use futures::future;
        Delay::new(Duration::from_millis(50)).await;
        let value = match future::select(FOO.get(), future::ready(1u32)).await {
            future::Either::Left((value, _)) => *value,
            future::Either::Right((value, _)) => value,
        };
        assert_eq!(&value, &1);
        let _ = handle1.await;
        let _ = handle2.await;
    });
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn lazy_static_test_for_async_std() {
    use async_std::task;
    use futures_timer::Delay;
    use lazy_static::lazy_static;
    use std::time::Duration;
    lazy_static! {
        static ref FOO: AsyncOnce<u32> = AsyncOnce::new(async {
            Delay::new(Duration::from_millis(100)).await;
            1
        });
    }
    task::spawn(async { assert_eq!(FOO.get().await, &1) });
    task::spawn(async { assert_eq!(FOO.get().await, &1) });
    task::spawn(async { assert_eq!(FOO.get().await, &1) });
    task::block_on(async {
        Delay::new(Duration::from_millis(200)).await;
        assert_eq!(FOO.get().await, &1);
    });
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn lazy_static_test_for_smol() {
    use futures_timer::Delay;
    use lazy_static::lazy_static;
    use std::time::Duration;
    lazy_static! {
        static ref FOO: AsyncOnce<u32> = AsyncOnce::new(async {
            Delay::new(Duration::from_millis(100)).await;
            1
        });
    }
    smol::spawn(async { assert_eq!(FOO.get().await, &1) }).detach();
    smol::spawn(async { assert_eq!(FOO.get().await, &1) }).detach();
    smol::spawn(async { assert_eq!(FOO.get().await, &1) }).detach();
    smol::block_on(async {
        Delay::new(Duration::from_millis(200)).await;
        assert_eq!(FOO.get().await, &1);
    });
}