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
/*
 * Created on Thu Aug 10 2023
 *
 * Copyright (c) storycraft. Licensed under the MIT Licence.
 */

#![doc = include_str!("../README.md")]

#[doc(hidden)]
pub mod __private;

use std::{
    fmt::Debug,
    future::Future,
    mem,
    pin::Pin,
    task::{Context, Poll, Waker}, ptr::NonNull,
};

use higher_kinded_types::ForLifetime;
use parking_lot::Mutex;

use pin_list::{id::Unchecked, CursorMut};

#[macro_export]
/// Higher kinded type helper for [`EventSource`]
macro_rules! EventSource {
    ($($ty: tt)*) => {
        $crate::EventSource<$crate::__private::ForLt!($($ty)*)>
    };
}

#[macro_export]
/// Emit event. As methods can't do mutable reborrowing correctly, you should use this macro.
macro_rules! emit {
    ($source: expr, $event: expr) => {
        $source.with_emitter(|mut emitter| while emitter.emit_next($event).is_some() {});
    };
}

/// Event source
pub struct EventSource<T: ForLifetime> {
    list: Mutex<PinList<T>>,
}

// SAFETY: EventSource doesn't own any data
unsafe impl<T: ForLifetime> Send for EventSource<T> {}

// SAFETY: Sync guaranteed by Mutex
unsafe impl<T: ForLifetime> Sync for EventSource<T> {}

impl<T: ForLifetime> EventSource<T> {
    /// Create new [`EventSource`]
    pub const fn new() -> Self {
        Self {
            list: Mutex::new(PinList::new(unsafe { Unchecked::new() })),
        }
    }

    pub fn with_emitter(&self, emit_fn: impl FnOnce(EventEmitter<T>)) {
        let mut list = self.list.lock();

        emit_fn(EventEmitter {
            cursor: list.cursor_front_mut(),
        });
    }

    /// Listen event until listener returns [`Option::Some`]
    /// 
    /// It can be called after woken if another event occurred before task continue.
    pub fn on<F>(&self, listener: F) -> EventFnFuture<F, T>
    where
        F: FnMut(T::Of<'_>) -> Option<()> + Sync,
    {
        EventFnFuture {
            source: self,
            listener,
            node: pin_list::Node::new(),
        }
    }

    /// Listen event until listener returns [`Option::Some`]
    /// 
    /// Unlike [`EventSource::on`] it will ignore every events once listener returns with [`Option::Some`].
    pub async fn once<F, R>(&self, mut listener: F) -> R
    where
        F: FnMut(T::Of<'_>) -> Option<R> + Sync,
        R: Sync,
    {
        let mut res = None;

        self.on(|event| {
            if res.is_some() {
                return None;
            }

            listener(event).map(|output| {
                res = Some(output);
            })
        })
        .await;

        res.unwrap()
    }
}

impl<T: ForLifetime> Debug for EventSource<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EventSource")
            .field("list", &self.list)
            .finish()
    }
}

#[derive(Debug)]
pub struct EventEmitter<'a, T: ForLifetime> {
    cursor: CursorMut<'a, NodeTypes<T>>,
}

impl<T: ForLifetime> EventEmitter<'_, T> {
    pub fn emit_next(&mut self, event: T::Of<'_>) -> Option<()> {
        let node = self.cursor.protected_mut()?;

        // SAFETY: Closure is pinned and the pointer is valid
        if unsafe { node.poll(event) } {
            if let Some(waker) = node.waker.take() {
                waker.wake();
            }
        }

        self.cursor.move_next();

        Some(())
    }
}

type NodeTypes<T> = dyn pin_list::Types<
    Id = pin_list::id::Unchecked,
    Protected = ListenerItem<T>,
    Unprotected = (),
    Removed = (),
>;

type PinList<T> = pin_list::PinList<NodeTypes<T>>;

type Node<T> = pin_list::Node<NodeTypes<T>>;

pin_project_lite::pin_project!(
    #[project(!Unpin)]
    #[derive(Debug)]
    #[must_use = "futures do nothing unless you `.await` or poll them"]
    pub struct EventFnFuture<'a, F, T: ForLifetime> {
        source: &'a EventSource<T>,

        listener: F,

        #[pin]
        node: Node<T>,
    }

    impl<F, T: ForLifetime> PinnedDrop for EventFnFuture<'_, F, T> {
        fn drop(this: Pin<&mut Self>) {
            let project = this.project();
            let node = match project.node.initialized_mut() {
                Some(initialized) => initialized,
                None => return,
            };

            let _ = node.reset(&mut project.source.list.lock());
        }
    }
);

// SAFETY: Everything in EventFnFuture is safe to send and closure is Send
unsafe impl<F: Send, T: ForLifetime> Send for EventFnFuture<'_, F, T> {}

// SAFETY: Everything in EventFnFuture is safe to sync and closure is Sync
unsafe impl<F: Sync, T: ForLifetime> Sync for EventFnFuture<'_, F, T> {}

impl<'a, T: ForLifetime, F: FnMut(T::Of<'_>) -> Option<()>> Future
    for EventFnFuture<'a, F, T>
{
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut this = self.project();

        let mut list = this.source.list.lock();

        let node = {
            let initialized = match this.node.as_mut().initialized_mut() {
                Some(initialized) => initialized,
                None => list.push_back(this.node, ListenerItem::new(this.listener), ()),
            };

            initialized.protected_mut(&mut list).unwrap()
        };

        if node.done {
            return Poll::Ready(());
        }

        node.update_waker(cx.waker());

        Poll::Pending
    }
}

type DynClosure<'closure, T> =
    dyn for<'a> FnMut(<T as ForLifetime>::Of<'a>) -> Option<()> + 'closure;

#[derive(Debug)]
struct ListenerItem<T: ForLifetime> {
    done: bool,
    waker: Option<Waker>,
    closure_ptr: NonNull<DynClosure<'static, T>>,
}

impl<T: ForLifetime> ListenerItem<T> {
    fn new<'a>(closure_ptr: &'a mut DynClosure<T>) -> Self
    where
        T: 'a,
    {
        Self {
            done: false,
            waker: None,

            // SAFETY: See ListenerItem::poll for safety requirement
            closure_ptr: unsafe {
                mem::transmute::<NonNull<_>, NonNull<_>>(NonNull::from(closure_ptr))
            },
        }
    }

    fn update_waker(&mut self, waker: &Waker) {
        match self.waker {
            Some(ref waker) if waker.will_wake(waker) => (),

            _ => {
                self.waker = Some(waker.clone());
            }
        }
    }

    /// # Safety
    /// Calling this method is only safe if pointer of closure is valid
    unsafe fn poll(&mut self, event: T::Of<'_>) -> bool {
        if self.closure_ptr.as_mut()(event).is_some() && !self.done {
            self.done = true;
        }

        self.done
    }
}