From a698146943bcd6df2a61bbfd56badf3018662709 Mon Sep 17 00:00:00 2001 From: Ty Overby Date: Thu, 1 Jan 2015 23:09:16 -0800 Subject: Change the way that events are represented. The bulk of this commit is changing instances of Vec to RingBuf which is optimized for the push_back() / pop_front() strategy that is used internaly in the event system. The glutin custom iterators are now just wrappers around the RingBuf iterator type. This will bring the running time of iterator traversal from O(n^2) to O(n) because shifting-on-delete won't be performed. --- src/lib.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 6a1c23b..3c780a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,7 @@ extern crate core_graphics; pub use events::*; use std::default::Default; +use std::collections::ring_buf::IntoIter as RingBufIter; #[cfg(all(not(target_os = "windows"), not(target_os = "linux"), not(target_os = "macos"), not(target_os = "android")))] use this_platform_is_not_supported; @@ -97,7 +98,7 @@ pub struct WindowBuilder<'a> { attribs: BuilderAttribs<'a> } -/// Attributes +/// Attributes struct BuilderAttribs<'a> { headless: bool, strict: bool, @@ -487,7 +488,7 @@ impl Window { /// Contrary to `wait_events`, this function never blocks. #[inline] pub fn poll_events(&self) -> PollEventsIterator { - PollEventsIterator { data: self.window.poll_events() } + PollEventsIterator { data: self.window.poll_events().into_iter() } } /// Waits for an event, then returns an iterator to all the events that are currently @@ -497,7 +498,7 @@ impl Window { /// this function will block until there is one. #[inline] pub fn wait_events(&self) -> WaitEventsIterator { - WaitEventsIterator { data: self.window.wait_events() } + WaitEventsIterator { data: self.window.wait_events().into_iter() } } /// Sets the context as the current context. @@ -637,12 +638,12 @@ 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. pub struct PollEventsIterator<'a> { - data: Vec, + data: RingBufIter, } impl<'a> Iterator for PollEventsIterator<'a> { fn next(&mut self) -> Option { - self.data.remove(0) + self.data.next() } } @@ -650,12 +651,12 @@ 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. pub struct WaitEventsIterator<'a> { - data: Vec, + data: RingBufIter, } impl<'a> Iterator for WaitEventsIterator<'a> { fn next(&mut self) -> Option { - self.data.remove(0) + self.data.next() } } @@ -664,13 +665,13 @@ impl<'a> Iterator for WaitEventsIterator<'a> { // This may change in the future. #[cfg(feature = "window")] pub struct AvailableMonitorsIter { - data: Vec, + data: RingBufIter, } #[cfg(feature = "window")] impl Iterator for AvailableMonitorsIter { fn next(&mut self) -> Option { - self.data.remove(0).map(|id| MonitorID(id)) + self.data.next().map(|id| MonitorID(id)) } } @@ -678,7 +679,7 @@ impl Iterator for AvailableMonitorsIter { #[cfg(feature = "window")] pub fn get_available_monitors() -> AvailableMonitorsIter { let data = winimpl::get_available_monitors(); - AvailableMonitorsIter{ data: data } + AvailableMonitorsIter{ data: data.into_iter() } } /// Returns the primary monitor of the system. -- cgit v1.2.3