event-handler 0.1.1

Event invocation utilities
Documentation
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! # Event Handling
//!
//! An .NET `event`, `EventHandler` and `EventArgs` inspired event handling system.
//!
//! ## Examples
//!
//! The following example constructs an [`Event`] and two handles to it.
//! It invokes the event through one of the handles (de-registering it), then on the event itself.
//!
//! ```
//! use event_handler::prelude::*;
//! use std::sync::{Arc, Mutex};
//!
//! // The values we want to mutate.
//! // These need to be Send such that the handle functions can update them.
//! let value = Arc::new(Mutex::new(0));
//! let value2 = Arc::new(Mutex::new(0));
//!
//! // Create an event handler.
//! let event = Event::new();
//!
//! // Create a closure to mutate the value.
//! let update_first_value = {
//!     let first_value = value.clone();
//!     move |amount| *first_value.lock().unwrap() = amount
//! };
//!
//! // Create a closure to mutate the other value.
//! let update_second_value = {
//!     let second_value = value2.clone();
//!     move |amount| *second_value.lock().unwrap() = amount * 2
//! };
//!
//! // Register the function to the event.
//! let handle = event.add_fn(update_first_value).unwrap();
//! let _handle = event.add_fn(update_second_value).unwrap();
//!
//! // Two handlers are now registered.
//! assert_eq!(event.len(), 2);
//!
//! // Invoke the event on the handle.
//! // Since we move the handle, this will de-register the handler when the scope is left.
//! assert!(std::thread::spawn(move || { handle.invoke(41) })
//!     .join()
//!     .is_ok());
//! assert_eq!(event.len(), 1);
//! assert_eq!(*value.lock().unwrap(), 41);
//! assert_eq!(*value2.lock().unwrap(), 41 * 2);
//!
//! // Invoke the event on the event itself.
//! event.invoke(42);
//! assert_eq!(*value.lock().unwrap(), 41);         // previous value
//! assert_eq!(*value2.lock().unwrap(), 42 * 2);    // updated value
//! ```
//!
//! If the [`Event`] is dropped, calls to [`EventHandle::invoke`] will return an error.
//!
//! ```
//! # use event_handler::prelude::*;
//! # use std::sync::{Arc, Mutex};
//! let event = Event::new();
//!
//! // Register the function to the event.
//! let value = Arc::new(Mutex::new(0));
//! let handle = event.add_fn({
//!         let value = value.clone();
//!         move |amount| *value.lock().unwrap() = amount
//! }).unwrap();
//!
//! // Register another function.
//! let value2 = Arc::new(Mutex::new(0));
//! let late_handle = event.add_fn({
//!         let value = value2.clone();
//!         move |amount| *value.lock().unwrap() = amount * 2
//! }).unwrap();
//!
//! // Invoke the event on the handler itself.
//! // This will move the event, dropping it afterwards.
//! assert!(std::thread::spawn(move || {
//!     event.invoke(42);
//! })
//! .join()
//! .is_ok());
//! assert_eq!(*value.lock().unwrap(), 42);
//! assert_eq!(*value2.lock().unwrap(), 42 * 2);
//!
//! // This event invocation will fail because the event is already dropped.
//! assert_eq!(
//!     std::thread::spawn(move || { late_handle.invoke(41) })
//!         .join()
//!         .unwrap(),
//!     Err(EventInvocationError::EventDropped)
//! );
//! ```

#![allow(unsafe_code)]
#![forbid(unused_must_use)]

use std::cell::Cell;
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, RwLock, Weak};

pub mod prelude {
    pub use crate::{Event, EventHandle, EventInvocationError, Invoke};
}

/// Alias for trivial function pointers.
pub type FnEventHandlerDelegate<TEventArgs> = fn(TEventArgs) -> ();

/// An event registration.
pub struct Event<TEventArgs = ()> {
    handlers: Arc<MapLocked<TEventArgs>>,
}

unsafe impl<TEventArgs: Send + Sync> Sync for Event<TEventArgs> {}

/// A concrete type of a handler.
enum HandlerType<TEventArgs> {
    BoxedFn(Box<dyn Fn(TEventArgs) + Send>),
    BoxedFnOnce(Cell<Option<Box<dyn FnOnce(TEventArgs) + Send>>>),
    Function(FnEventHandlerDelegate<TEventArgs>),
}

unsafe impl<TEventArgs: Send + Sync> Sync for HandlerType<TEventArgs> {}

/// Helper type declaration for a locked [`MapInner`].
struct MapLocked<TEventArgs>(RwLock<MapInner<TEventArgs>>);

/// The actual storage type.
type MapInner<TEventArgs> = BTreeMap<HandleKey, HandlerType<TEventArgs>>;

/// A handle to a registration.
/// When the handle is dropped, the registration is revoked.
#[must_use = "This handle must be held alive for as long as the event should be used."]
pub struct EventHandle<TEventArgs> {
    /// The key in the map.
    key: HandleKey,
    /// Pointer to the map that (possibly) contains the key.
    pointer: Weak<MapLocked<TEventArgs>>,
}

/// A key entry for a handler.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Copy, Clone)]
enum HandleKey {
    PtrOfBox(usize),
    FunctionPointer(usize),
}

/// Hashing for [`HandleKey`] instances.
impl Hash for HandleKey {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            HandleKey::PtrOfBox(ptr) => {
                let address = ptr as *const usize as usize;
                address.hash(state)
            }
            HandleKey::FunctionPointer(ptr) => {
                let address = ptr as *const usize as usize;
                address.hash(state)
            }
        }
    }
}

impl<TEventArgs> EventHandle<TEventArgs> {
    /// Initializes a new `Handle` from a successful registration.
    fn new(key: HandleKey, pointer: &Arc<MapLocked<TEventArgs>>) -> Self {
        Self {
            key,
            pointer: Arc::downgrade(pointer),
        }
    }

    /// Determines whether the registration is still valid.
    pub fn is_valid(&self) -> bool {
        self.pointer.strong_count() > 0
    }

    /// Invokes the event with the specified arguments.
    ///
    /// ## Arguments
    /// * `args` - The event arguments to pass.
    pub fn invoke(&self, args: TEventArgs) -> Result<(), EventInvocationError>
    where
        TEventArgs: Clone,
    {
        if let Some(ptr) = self.pointer.upgrade() {
            ptr.invoke(args);
            Ok(())
        } else {
            Err(EventInvocationError::EventDropped)
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum EventInvocationError {
    /// The event was dropped.
    EventDropped,
}

impl Display for EventInvocationError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            EventInvocationError::EventDropped => write!(
                f,
                "Event could not be invoked because it was already dropped"
            ),
        }
    }
}

impl Error for EventInvocationError {}

impl<TEventArgs> Drop for EventHandle<TEventArgs> {
    fn drop(&mut self) {
        if let Some(lock) = self.pointer.upgrade() {
            let mut handlers = lock.write().unwrap();
            handlers.remove(&self.key);
        }
    }
}

impl<TEventArgs> Event<TEventArgs> {
    pub fn new() -> Self
    where
        TEventArgs: Clone,
    {
        Self {
            handlers: Arc::new(MapLocked::new(MapInner::new())),
        }
    }

    pub fn add_fn<T>(&self, handler: T) -> Result<EventHandle<TEventArgs>, String>
    where
        T: Fn(TEventArgs) -> () + Send + 'static,
    {
        let handler = Box::new(handler);
        let key = HandleKey::PtrOfBox((&*handler as *const _) as usize);
        let mut handlers = self.handlers.write().unwrap();
        let entry = HandlerType::BoxedFn(handler);
        match handlers.insert(key, entry) {
            None => Ok(EventHandle::new(key, &self.handlers)),
            Some(_) => Err(String::from("The handler was already registered")),
        }
    }

    pub fn add_fnonce<T>(&self, handler: T) -> Result<EventHandle<TEventArgs>, String>
    where
        T: FnOnce(TEventArgs) -> () + Send + 'static,
    {
        let handler = Box::new(handler);
        let key = HandleKey::PtrOfBox((&*handler as *const _) as usize);
        let mut handlers = self.handlers.write().unwrap();
        let entry = HandlerType::BoxedFnOnce(Cell::new(Some(handler)));
        match handlers.insert(key, entry) {
            None => Ok(EventHandle::new(key, &self.handlers)),
            Some(_) => Err(String::from("The handler was already registered")),
        }
    }

    pub fn add_ptr(
        &self,
        handler: FnEventHandlerDelegate<TEventArgs>,
    ) -> Result<EventHandle<TEventArgs>, String> {
        let key = HandleKey::FunctionPointer((&handler as *const _) as usize);
        let mut handlers = self.handlers.write().unwrap();
        let entry = HandlerType::Function(handler);
        match handlers.insert(key, entry) {
            None => Ok(EventHandle::new(key, &self.handlers)),
            Some(_) => Err(String::from("The handler was already registered")),
        }
    }

    /// Returns the number of currently registered handlers.
    pub fn len(&self) -> usize {
        self.handlers.read().unwrap().len()
    }

    /// Invokes the event.
    ///
    /// ## Arguments
    /// * `args` - The event arguments.
    pub fn invoke(&self, args: TEventArgs)
    where
        TEventArgs: Clone,
    {
        self.handlers.invoke(args)
    }
}

impl Default for Event {
    fn default() -> Self {
        Self::new()
    }
}

impl<TEventArgs> MapLocked<TEventArgs>
where
    TEventArgs: Clone,
{
    const fn new(inner: MapInner<TEventArgs>) -> Self {
        Self(RwLock::new(inner))
    }

    fn invoke(&self, args: TEventArgs) {
        let mut unregister_list = Vec::new();

        {
            let handlers = self.read().unwrap();
            for (key, entry) in handlers.iter() {
                let args = args.clone();
                match &entry {
                    HandlerType::Function(fun) => fun(args),
                    HandlerType::BoxedFn(fun) => fun(args),
                    HandlerType::BoxedFnOnce(cell) => {
                        let fun = cell.replace(None);
                        if fun.is_some() {
                            (fun.unwrap())(args);
                        }
                        unregister_list.push(key.clone());
                    }
                }
            }
        }

        // Clean up after any FnOnce type.
        if !unregister_list.is_empty() {
            let mut handlers = self.write().unwrap();
            for key in unregister_list {
                handlers.remove(&key);
            }
        }
    }
}

impl<TEventArgs> Deref for MapLocked<TEventArgs> {
    type Target = RwLock<MapInner<TEventArgs>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<TEventArgs> DerefMut for MapLocked<TEventArgs> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// Provides the `invoke` function for an event.
pub trait Invoke<TEventArgs>
where
    TEventArgs: Clone,
{
    fn invoke(&self, args: TEventArgs);
}

impl<TEventArgs> Invoke<TEventArgs> for Event<TEventArgs>
where
    TEventArgs: Clone,
{
    fn invoke(&self, args: TEventArgs) {
        self.invoke(args)
    }
}

impl<TEventArgs> Invoke<TEventArgs> for EventHandle<TEventArgs>
where
    TEventArgs: Clone,
{
    fn invoke(&self, args: TEventArgs) {
        self.invoke(args).ok();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn dummy(_args: ()) {
        println!("Dummy called.");
    }

    #[test]
    fn new_handler_has_no_registrations() {
        let handler = Event::<()>::new();
        assert_eq!(handler.len(), 0);
    }

    #[test]
    #[allow(unused_variables)]
    fn can_add_fn() {
        let handler = Event::<()>::new();
        let handle = handler.add_fn(dummy).unwrap();
        assert_eq!(handler.len(), 1);
        handler.invoke(());
    }

    #[test]
    #[allow(unused_variables)]
    fn can_add_fnonce() {
        let handler = Event::new();
        let handle = handler.add_fnonce(dummy).unwrap();
        assert_eq!(handler.len(), 1);
        handler.invoke(());
        assert_eq!(handler.len(), 0);
    }

    #[test]
    #[allow(unused_variables)]
    fn can_add_function_pointer() {
        let handler = Event::<()>::new();
        let handle = handler.add_ptr(dummy).unwrap();
        assert_eq!(handler.len(), 1);
        handler.invoke(());
    }

    #[test]
    #[allow(unused_variables)]
    fn cannot_register_same_function_twice() {
        let handler = Event::new();
        let handle = handler.add_ptr(dummy).unwrap();
        assert!(handler.add_ptr(dummy).is_err());
    }

    #[test]
    fn can_remove_handlers() {
        let handler = Event::new();
        let handle = handler.add_fn(dummy).unwrap();
        assert_eq!(handler.len(), 1);
        drop(handle);
        assert_eq!(handler.len(), 0);
    }

    #[test]
    fn handler_is_sync() {
        let handler: Event = Event::new();
        let _sync: Box<dyn Sync> = Box::new(handler);
    }
}