diff options
author | Pierre Krieger <pierre.krieger1708@gmail.com> | 2015-01-19 13:04:14 +0100 |
---|---|---|
committer | Pierre Krieger <pierre.krieger1708@gmail.com> | 2015-01-19 13:11:33 +0100 |
commit | 8fcd6767148eaa3a516c58d16238d842ef32d26a (patch) | |
tree | f22775f15792791306e02be014e6bd4778ea5ac9 | |
parent | bcda3631887cf84f09f418946c462211cd7a540c (diff) | |
download | glutin-8fcd6767148eaa3a516c58d16238d842ef32d26a.tar.gz glutin-8fcd6767148eaa3a516c58d16238d842ef32d26a.zip |
Iterators returned by wait_events and poll_events are now persistent
-rw-r--r-- | examples/fullscreen.rs | 2 | ||||
-rw-r--r-- | examples/multiwindow.rs | 2 | ||||
-rw-r--r-- | examples/window.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 38 |
4 files changed, 33 insertions, 11 deletions
diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs index 7c2ece8..c07edaa 100644 --- a/examples/fullscreen.rs +++ b/examples/fullscreen.rs @@ -47,6 +47,6 @@ fn main() { context.draw_frame((0.0, 1.0, 0.0, 1.0)); window.swap_buffers(); - println!("{:?}", window.wait_events().collect::<Vec<glutin::Event>>()); + println!("{:?}", window.wait_events().next()); } } diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs index cd57d2e..115d5f2 100644 --- a/examples/multiwindow.rs +++ b/examples/multiwindow.rs @@ -47,6 +47,6 @@ fn run(window: glutin::Window, color: (f32, f32, f32, f32)) { context.draw_frame(color); window.swap_buffers(); - window.wait_events().collect::<Vec<glutin::Event>>(); + window.wait_events().next(); } } diff --git a/examples/window.rs b/examples/window.rs index 37966c4..1d3b615 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -30,6 +30,6 @@ fn main() { context.draw_frame((0.0, 1.0, 0.0, 1.0)); window.swap_buffers(); - println!("{:?}", window.wait_events().collect::<Vec<glutin::Event>>()); + println!("{:?}", window.wait_events().next()); } } @@ -537,22 +537,22 @@ impl Window { self.window.set_inner_size(x, y) } - /// Returns an iterator to all the events that are currently in the window's events queue. + /// Returns an iterator that poll for the next event in the window's events queue. + /// Returns `None` if there is no event in the queue. /// /// Contrary to `wait_events`, this function never blocks. #[inline] pub fn poll_events(&self) -> PollEventsIterator { - PollEventsIterator { data: self.window.poll_events().into_iter() } + PollEventsIterator { window: self, data: self.window.poll_events().into_iter() } } - /// Waits for an event, then returns an iterator to all the events that are currently - /// in the window's events queue. + /// Returns an iterator that returns events one by one, blocking if necessary until one is + /// available. /// - /// If there are no events in queue when you call the function, - /// this function will block until there is one. + /// The iterator never returns `None`. #[inline] pub fn wait_events(&self) -> WaitEventsIterator { - WaitEventsIterator { data: self.window.wait_events().into_iter() } + WaitEventsIterator { window: self, data: self.window.wait_events().into_iter() } } /// Sets the context as the current context. @@ -697,13 +697,24 @@ impl gl_common::GlFunctionsSource for HeadlessContext { /// An iterator for the `poll_events` function. // 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<Event>, } +#[cfg(feature = "window")] impl<'a> Iterator for PollEventsIterator<'a> { type Item = Event; fn next(&mut self) -> Option<Event> { + 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() } } @@ -711,14 +722,25 @@ impl<'a> Iterator for PollEventsIterator<'a> { /// An iterator for the `wait_events` function. // 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<Event>, } +#[cfg(feature = "window")] impl<'a> Iterator for WaitEventsIterator<'a> { type Item = Event; fn next(&mut self) -> Option<Event> { - self.data.next() + 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() } } |