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
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use pin_project::pin_project;

pub(crate) fn lazy<F, R>(f: F) -> Lazy<F, R>
where
    F: FnOnce() -> R,
    R: Future,
{
    Lazy {
        inner: InnerLazy::Init(f),
    }
}

#[pin_project]
pub(crate) struct Lazy<F, R> {
    #[pin]
    inner: InnerLazy<F, R>,
}

#[pin_project(project = InnerProj, project_replace = InnerProjReplace)]
enum InnerLazy<F, R> {
    Init(F),
    Future(#[pin] R),
    Empty,
}

impl<F, R> Future for Lazy<F, R>
where
    F: FnOnce() -> R,
    R: Future,
{
    type Output = R::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut this = self.project();
        loop {
            if let InnerProj::Future(future) = this.inner.as_mut().project() {
                return future.poll(cx);
            }

            if let InnerProjReplace::Init(f) = this.inner.as_mut().project_replace(InnerLazy::Empty)
            {
                this.inner.set(InnerLazy::Future(f()));
            }

            if let InnerProj::Empty = this.inner.as_mut().project() {
                panic!("Lazy future polled after completion");
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::AtomicUsize;

    use futures_util::poll;

    use super::*;

    #[tokio::test]
    async fn lazy_future() {
        let (tx, rx) = tokio::sync::oneshot::channel();
        let count = AtomicUsize::new(0);
        let mut future = std::pin::pin!(lazy(|| async {
            rx.await.unwrap();
            count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }));

        assert_eq!(count.load(std::sync::atomic::Ordering::SeqCst), 0);
        assert_eq!(poll!(future.as_mut()), Poll::Pending);

        tx.send(()).unwrap();
        assert_eq!(poll!(future.as_mut()), Poll::Ready(()));
        assert_eq!(count.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[cfg(not(tarpaulin))]
    #[tokio::test]
    #[should_panic]
    async fn lazy_future_panic() {
        let count = AtomicUsize::new(0);
        let mut future = std::pin::pin!(lazy(|| async {
            count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }));

        assert_eq!(count.load(std::sync::atomic::Ordering::SeqCst), 0);
        assert!(poll!(future.as_mut()).is_ready());
        assert_eq!(count.load(std::sync::atomic::Ordering::SeqCst), 1);
        let _ = poll!(future.as_mut());
    }
}