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
//! A generic event source wrapping an `Evented` type

use std::cell::RefCell;
use std::io;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, RawFd};
use std::rc::Rc;

use mio::{Evented, Poll, PollOpt, Ready, Token};

use {EventDispatcher, EventSource};

/// A generic event source wrapping an `Evented` type
///
/// It will simply forward the readiness and an acces to
/// the wrapped `Evented` type to the suer callback. See
/// the `Event` type in this module.
pub struct Generic<E: Evented + 'static> {
    inner: Rc<RefCell<E>>,
    interest: Ready,
    pollopts: PollOpt,
}

impl<E: Evented + 'static> Generic<E> {
    /// Wrap an `Evented` type into a `Generic` event source
    ///
    /// It is initialized with no interest nor poll options,
    /// as such you should set them using the `set_interest`
    /// and `set_pollopts` methods before inserting it in the
    /// event loop.
    pub fn new(source: E) -> Generic<E> {
        Generic {
            inner: Rc::new(RefCell::new(source)),
            interest: Ready::empty(),
            pollopts: PollOpt::empty(),
        }
    }

    /// Wrap an `Evented` type from an `Rc` into a `Generic` event source
    ///
    /// Same as the `new` method, but you can provide a source that is alreay
    /// in a reference counted pointer, so that `Generic` won't add a new
    /// layer. This is useful if you need to share this source accross multiple
    /// modules, and `calloop` is not the first one to be initialized.
    pub fn from_rc(source: Rc<RefCell<E>>) -> Generic<E> {
        Generic {
            inner: source,
            interest: Ready::empty(),
            pollopts: PollOpt::empty(),
        }
    }

    /// Change the interest for this evented source
    ///
    /// If the source was already inserted in an event loop,
    /// it needs to be re-registered for the change to take
    /// effect.
    pub fn set_interest(&mut self, interest: Ready) {
        self.interest = interest;
    }

    /// Change the poll options for this evented source
    ///
    /// If the source was already inserted in an event loop,
    /// it needs to be re-registered for the change to take
    /// effect.
    pub fn set_pollopts(&mut self, pollopts: PollOpt) {
        self.pollopts = pollopts;
    }

    /// Get a clone of the inner `Rc` wrapping your event source
    pub fn clone_inner(&self) -> Rc<RefCell<E>> {
        self.inner.clone()
    }

    /// Unwrap the `Generic` source to retrieve the underlying `Evented`.
    ///
    /// If you didn't clone the `Rc<RefCell<E>>` from the `Event<E>` you received,
    /// the returned `Rc` should be unique.
    pub fn unwrap(self) -> Rc<RefCell<E>> {
        self.inner
    }
}

impl<Fd: AsRawFd> Generic<EventedFd<Fd>> {
    /// Wrap a file descriptor based source into a `Generic` event source.
    ///
    /// This will only work with poll-compatible file descriptors, which typically
    /// not include basic files.
    #[cfg(unix)]
    pub fn from_fd_source(source: Fd) -> Generic<EventedFd<Fd>> {
        Generic::new(EventedFd(source))
    }
}

impl Generic<EventedRawFd> {
    /// Wrap a raw file descriptor into a `Generic` event source.
    ///
    /// This will only work with poll-compatible file descriptors, which typically
    /// not include basic files.
    ///
    /// This does _not_ take ownership of the file descriptor, hence you are responsible
    /// of its correct lifetime.
    #[cfg(unix)]
    pub fn from_raw_fd(fd: RawFd) -> Generic<EventedRawFd> {
        Generic::new(EventedRawFd(fd))
    }
}

/// An event generated by the `Generic` source
pub struct Event<E: Evented + 'static> {
    /// An access to the source that generated this event
    pub source: Rc<RefCell<E>>,
    /// The associated rediness
    pub readiness: Ready,
}

/// An owning wrapper implementing Evented for any file descriptor based type in Unix
#[cfg(unix)]
pub struct EventedFd<F: AsRawFd>(pub F);

impl<F: AsRawFd> Evented for EventedFd<F> {
    fn register(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        ::mio::unix::EventedFd(&self.0.as_raw_fd()).register(poll, token, interest, opts)
    }

    fn reregister(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        ::mio::unix::EventedFd(&self.0.as_raw_fd()).reregister(poll, token, interest, opts)
    }

    fn deregister(&self, poll: &Poll) -> io::Result<()> {
        ::mio::unix::EventedFd(&self.0.as_raw_fd()).deregister(poll)
    }
}

/// A wrapper implementing Evented for any raw file descriptor.
///
/// It does _not_ take ownership of the file descriptor, you are
/// responsible for ensuring its correct lifetime.
#[cfg(unix)]
pub struct EventedRawFd(pub RawFd);

impl Evented for EventedRawFd {
    fn register(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        ::mio::unix::EventedFd(&self.0).register(poll, token, interest, opts)
    }

    fn reregister(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        ::mio::unix::EventedFd(&self.0).reregister(poll, token, interest, opts)
    }

    fn deregister(&self, poll: &Poll) -> io::Result<()> {
        ::mio::unix::EventedFd(&self.0).deregister(poll)
    }
}

impl<E: Evented + 'static> Evented for Generic<E> {
    fn register(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        self.inner.borrow().register(poll, token, interest, opts)
    }

    fn reregister(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        self.inner.borrow().reregister(poll, token, interest, opts)
    }

    fn deregister(&self, poll: &Poll) -> io::Result<()> {
        self.inner.borrow().deregister(poll)
    }
}

impl<E: Evented + 'static> EventSource for Generic<E> {
    type Event = Event<E>;

    fn interest(&self) -> Ready {
        self.interest
    }

    fn pollopts(&self) -> PollOpt {
        self.pollopts
    }

    fn make_dispatcher<Data: 'static, F: FnMut(Event<E>, &mut Data) + 'static>(
        &self,
        callback: F,
    ) -> Rc<RefCell<EventDispatcher<Data>>> {
        Rc::new(RefCell::new(Dispatcher {
            _data: ::std::marker::PhantomData,
            inner: self.inner.clone(),
            callback,
        }))
    }
}

struct Dispatcher<Data, E: Evented + 'static, F: FnMut(Event<E>, &mut Data)> {
    _data: ::std::marker::PhantomData<fn(&mut Data)>,
    inner: Rc<RefCell<E>>,
    callback: F,
}

impl<Data, E: Evented + 'static, F: FnMut(Event<E>, &mut Data)> EventDispatcher<Data>
    for Dispatcher<Data, E, F>
{
    fn ready(&mut self, ready: Ready, data: &mut Data) {
        (self.callback)(
            Event {
                source: self.inner.clone(),
                readiness: ready,
            },
            data,
        )
    }
}

#[cfg(test)]
mod test {
    use std::io::{self, Read, Write};

    use super::{Event, Generic};
    #[cfg(unix)]
    #[test]
    fn dispatch_unix() {
        use std::os::unix::net::UnixStream;

        let mut event_loop = ::EventLoop::new().unwrap();

        let handle = event_loop.handle();

        let (mut tx, rx) = UnixStream::pair().unwrap();

        let mut generic = Generic::from_fd_source(rx);
        generic.set_interest(::mio::Ready::readable());

        let mut dispached = false;

        handle
            .insert_source(generic, move |Event { source, readiness }, d| {
                assert!(readiness.is_readable());
                // we have not registered for writability
                assert!(!readiness.is_writable());
                let mut buffer = vec![0; 10];
                let ret = source.borrow_mut().0.read(&mut buffer).unwrap();
                assert_eq!(ret, 6);
                assert_eq!(&buffer[..6], &[1, 2, 3, 4, 5, 6]);

                *d = true;
            }).map_err(Into::<io::Error>::into)
            .unwrap();

        event_loop
            .dispatch(Some(::std::time::Duration::from_millis(0)), &mut dispached)
            .unwrap();

        assert!(!dispached);

        tx.write(&[1, 2, 3, 4, 5, 6]).unwrap();
        tx.flush().unwrap();

        event_loop
            .dispatch(Some(::std::time::Duration::from_millis(0)), &mut dispached)
            .unwrap();

        assert!(dispached);
    }
}