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
use event::Event;
use relay::Relay;
use std::marker::PhantomData;

// Generic Event taking closures

#[doc(hidden)]
pub struct GenericEvent<Context, F>
    where F: Fn(&Relay<Context>, &mut Context), Context: Send {
    handler: F,
    context: PhantomData<Context>,
}

impl<Context: Send, F: Fn(&Relay<Context>, &mut Context)> Event<Context> for GenericEvent<Context, F> {
    fn process(self: Box<Self>, relay: &Relay<Context>, ctx: &mut Context) {
        (self.handler)(relay, ctx);
    }
}


/**
An helper function to create an event from a function or a closure

# Example
```
# use bifrost::*;
# struct Context {}
# let mut context = Context {};
# let dispatcher = Dispatcher::new(&mut context);
# let relay = dispatcher.create_relay();
let my_event = event(
    |relay, context| println!("Do something here")
);
# relay.send(my_event)
```
**/
pub fn event<Context, F>(handler: F) -> GenericEvent<Context, F>
    where F: Fn(&Relay<Context>, &mut Context), Context: Send {
    GenericEvent {
        handler,
        context: PhantomData,
    }
}