Thomasdezeeuw / gaea

Low-level library to build event driven applications, supporting lightweight non-blocking I/O.
https://docs.rs/gaea/
MIT License
16 stars 0 forks source link

Add thread-safe version of user space and timers #67

Closed Thomasdezeeuw closed 5 years ago

Thomasdezeeuw commented 5 years ago

Also a good test of the flexibility and usability of the event::Source trait.

Thomasdezeeuw commented 5 years ago

After some more consideration I've decided against this. It's fairly easy to create a wrapper around, for example, crossbeam's channel or any another thread-safe data structure. For example:

use crossbeam_channel::Receiver;

struct Events {
    /// Receiving side of the channel for `Event`s.
    receiver: Receiver<Event>,
}

impl<ES, E> event::Source<ES, E> for Events
    where ES: event::Sink,
{
    fn next_event_available(&self) -> Option<Duration> {
        if !self.receiver.is_empty() {
            Some(Duration::from_millis(0))
        } else {
            None
        }
    }

    fn poll(&mut self, event_sink: &mut ES) -> Result<(), E> {
        event_sink.extend(self.receiver.try_iter());
        Ok(())
    }
}