diff options
author | tomaka <pierre.krieger1708@gmail.com> | 2015-02-10 22:34:18 +0100 |
---|---|---|
committer | tomaka <pierre.krieger1708@gmail.com> | 2015-02-10 22:34:18 +0100 |
commit | 4500702a0294065f876ca10e7629fd32e213ca76 (patch) | |
tree | 0bf6f0f1580345f6441765e6e74cc64bcaf7e79a /src/win32 | |
parent | 605bd37554655436841e4cd2c4fbb3d046de2330 (diff) | |
parent | 95b1c96181e538004800942ae8ab07a81ec49454 (diff) | |
download | glutin-4500702a0294065f876ca10e7629fd32e213ca76.tar.gz glutin-4500702a0294065f876ca10e7629fd32e213ca76.zip |
Merge pull request #70 from tomaka/fix-iterators
Use platform-specific iterators instead
Diffstat (limited to 'src/win32')
-rw-r--r-- | src/win32/mod.rs | 90 |
1 files changed, 50 insertions, 40 deletions
diff --git a/src/win32/mod.rs b/src/win32/mod.rs index beb7825..c502e3d 100644 --- a/src/win32/mod.rs +++ b/src/win32/mod.rs @@ -201,50 +201,16 @@ impl Window { } /// See the docs in the crate root file. - // TODO: return iterator - pub fn poll_events(&self) -> RingBuf<Event> { - let mut events = RingBuf::new(); - loop { - match self.events_receiver.try_recv() { - Ok(ev) => events.push_back(ev), - Err(_) => break - } + pub fn poll_events(&self) -> PollEventsIterator { + PollEventsIterator { + window: self, } - - // if one of the received events is `Closed`, setting `is_closed` to true - if events.iter().any(|e| match e { &::events::Event::Closed => true, _ => false }) { - use std::sync::atomic::Ordering::Relaxed; - self.is_closed.store(true, Relaxed); - } - - events } /// See the docs in the crate root file. - // TODO: return iterator - pub fn wait_events(&self) -> RingBuf<Event> { - match self.events_receiver.recv() { - Ok(ev) => { - // if the received event is `Closed`, setting `is_closed` to true - match ev { - ::events::Event::Closed => { - use std::sync::atomic::Ordering::Relaxed; - self.is_closed.store(true, Relaxed); - }, - _ => () - }; - - // looing for other possible events in the queue - let mut result = self.poll_events(); - result.insert(0, ev); - result - }, - - Err(_) => { - use std::sync::atomic::Ordering::Relaxed; - self.is_closed.store(true, Relaxed); - RingBuf::new() - } + pub fn wait_events(&self) -> WaitEventsIterator { + WaitEventsIterator { + window: self, } } @@ -294,6 +260,50 @@ impl Window { } } +pub struct PollEventsIterator<'a> { + window: &'a Window, +} + +impl<'a> Iterator for PollEventsIterator<'a> { + type Item = Event; + + fn next(&mut self) -> Option<Event> { + use events::Event::Closed; + + match self.window.events_receiver.recv() { + Ok(Closed) => { + use std::sync::atomic::Ordering::Relaxed; + self.window.is_closed.store(true, Relaxed); + Some(Closed) + }, + Ok(ev) => Some(ev), + Err(_) => None + } + } +} + +pub struct WaitEventsIterator<'a> { + window: &'a Window, +} + +impl<'a> Iterator for WaitEventsIterator<'a> { + type Item = Event; + + fn next(&mut self) -> Option<Event> { + use events::Event::Closed; + + match self.window.events_receiver.recv() { + Ok(Closed) => { + use std::sync::atomic::Ordering::Relaxed; + self.window.is_closed.store(true, Relaxed); + Some(Closed) + }, + Ok(ev) => Some(ev), + Err(_) => None + } + } +} + #[unsafe_destructor] impl Drop for Window { fn drop(&mut self) { |