From 49e3fbdd718a31eb62c80f2a0789098303eae159 Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Sat, 25 Oct 2014 11:22:52 +0200 Subject: Win32: use platform-specific iterators instead --- src/lib.rs | 34 ++++----------------- src/win32/mod.rs | 90 +++++++++++++++++++++++++++++++------------------------- 2 files changed, 56 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index da2e83d..adc6e07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -565,7 +565,7 @@ impl Window { /// Contrary to `wait_events`, this function never blocks. #[inline] pub fn poll_events(&self) -> PollEventsIterator { - PollEventsIterator { window: self, data: self.window.poll_events().into_iter() } + PollEventsIterator(self.window.poll_events()) } /// Returns an iterator that returns events one by one, blocking if necessary until one is @@ -574,7 +574,7 @@ impl Window { /// The iterator never returns `None`. #[inline] pub fn wait_events(&self) -> WaitEventsIterator { - WaitEventsIterator { window: self, data: self.window.wait_events().into_iter() } + WaitEventsIterator(self.window.wait_events()) } /// Sets the context as the current context. @@ -727,24 +727,13 @@ impl gl_common::GlFunctionsSource for HeadlessContext { // Implementation note: we retreive the list once, then serve each element by one by one. // This may change in the future. #[cfg(feature = "window")] -pub struct PollEventsIterator<'a> { - window: &'a Window, - data: RingBufIter, -} +pub struct PollEventsIterator<'a>(winimpl::PollEventsIterator<'a>); #[cfg(feature = "window")] impl<'a> Iterator for PollEventsIterator<'a> { type Item = Event; fn next(&mut self) -> Option { - if let Some(ev) = self.data.next() { - return Some(ev); - } - - let PollEventsIterator { window, data } = self.window.poll_events(); - self.window = window; - self.data = data; - - self.data.next() + self.0.next() } } @@ -752,24 +741,13 @@ impl<'a> Iterator for PollEventsIterator<'a> { // Implementation note: we retreive the list once, then serve each element by one by one. // This may change in the future. #[cfg(feature = "window")] -pub struct WaitEventsIterator<'a> { - window: &'a Window, - data: RingBufIter, -} +pub struct WaitEventsIterator<'a>(winimpl::WaitEventsIterator<'a>); #[cfg(feature = "window")] impl<'a> Iterator for WaitEventsIterator<'a> { type Item = Event; fn next(&mut self) -> Option { - if let Some(ev) = self.data.next() { - return Some(ev); - } - - let WaitEventsIterator { window, data } = self.window.wait_events(); - self.window = window; - self.data = data; - - self.next() + self.0.next() } } diff --git a/src/win32/mod.rs b/src/win32/mod.rs index 8104fc8..ea7e0ff 100644 --- a/src/win32/mod.rs +++ b/src/win32/mod.rs @@ -205,50 +205,16 @@ impl Window { } /// See the docs in the crate root file. - // TODO: return iterator - pub fn poll_events(&self) -> RingBuf { - 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 { - 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, } } @@ -298,6 +264,50 @@ impl Window { } } +pub struct PollEventsIterator<'a> { + window: &'a Window, +} + +impl<'a> Iterator for PollEventsIterator<'a> { + type Item = Event; + + fn next(&mut self) -> Option { + 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 { + 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) { -- cgit v1.2.3