From edfbfd7690dfce95a4b283ed21c6fe2ec8b33036 Mon Sep 17 00:00:00 2001 From: Dan Spencer Date: Tue, 28 Jul 2015 01:34:59 -0600 Subject: [PATCH] Remove lifetime from EventPump, move into `sdl2::sdl` module The lifetime on EventPump isn't really needed anymore. --- src/sdl2/event.rs | 53 +--------------------------------------- src/sdl2/keyboard/mod.rs | 8 +++--- src/sdl2/render.rs | 2 +- src/sdl2/sdl.rs | 48 +++++++++++++++++++++++++++++++++++- src/sdl2/video.rs | 2 +- 5 files changed, 54 insertions(+), 59 deletions(-) diff --git a/src/sdl2/event.rs b/src/sdl2/event.rs index bdb77786..123836be 100644 --- a/src/sdl2/event.rs +++ b/src/sdl2/event.rs @@ -24,7 +24,6 @@ use mouse::{Mouse, MouseState}; use keyboard::Scancode; use get_error; use SdlResult; -use Sdl; use sys::event as ll; @@ -1001,44 +1000,7 @@ unsafe fn wait_event_timeout(timeout: u32) -> Option { else { None } } -static mut IS_EVENT_PUMP_ALIVE: bool = false; - -/// A thread-safe type that encapsulates SDL event-pumping functions. -pub struct EventPump<'sdl> { - _sdl: PhantomData<&'sdl ()>, - - // Prevents the event pump from moving to other threads. - // SDL events can only be pumped on the main thread. - _nosend: PhantomData<*mut ()> -} - -impl<'sdl> EventPump<'sdl> { - /// Obtains the SDL event pump. - #[inline] - pub fn new(_sdl: &'sdl Sdl) -> SdlResult> { - // Called on the main SDL thread. - - unsafe { - if IS_EVENT_PUMP_ALIVE { - Err(format!("an `EventPump` instance is already alive - there can only be one `EventPump` in use at a time.")) - } else { - // Initialize the events subsystem, just in case none of the other subsystems have done it yet. - let result = ::sys::sdl::SDL_InitSubSystem(::sys::sdl::SDL_INIT_EVENTS); - - if result == 0 { - IS_EVENT_PUMP_ALIVE = true; - - Ok(EventPump { - _sdl: PhantomData, - _nosend: PhantomData, - }) - } else { - Err(get_error()) - } - } - } - } - +impl ::EventPump { /// Query if an event type is enabled. pub fn is_event_enabled(&self, event_type: EventType) -> bool { let result = unsafe { ll::SDL_EventState(event_type as u32, ll::SDL_QUERY) }; @@ -1130,19 +1092,6 @@ impl<'sdl> EventPump<'sdl> { } } -impl<'sdl> Drop for EventPump<'sdl> { - #[inline] - fn drop(&mut self) { - // Called on the main SDL thread. - - unsafe { - assert!(IS_EVENT_PUMP_ALIVE); - ::sys::sdl::SDL_QuitSubSystem(::sys::sdl::SDL_INIT_EVENTS); - IS_EVENT_PUMP_ALIVE = false; - } - } -} - /// An iterator that calls `EventPump::poll_event()`. #[must_use = "iterators are lazy and do nothing unless consumed"] pub struct EventPollIterator<'a> { diff --git a/src/sdl2/keyboard/mod.rs b/src/sdl2/keyboard/mod.rs index 7bef03e3..ddd3e1e4 100644 --- a/src/sdl2/keyboard/mod.rs +++ b/src/sdl2/keyboard/mod.rs @@ -2,7 +2,7 @@ use num::{ToPrimitive, FromPrimitive}; use std::ptr; use std::marker::PhantomData; -use event::EventPump; +use EventPump; use rect::Rect; use video::Window; @@ -55,7 +55,7 @@ impl<'a> KeyboardState<'a> { /// ```no_run /// use sdl2::keyboard::Scancode; /// - /// fn is_a_pressed(e: &sdl2::event::EventPump) -> bool { + /// fn is_a_pressed(e: &sdl2::EventPump) -> bool { /// e.keyboard_state().is_scancode_pressed(Scancode::A) /// } /// ``` @@ -79,11 +79,11 @@ impl<'a> KeyboardState<'a> { /// use sdl2::keyboard::Scancode; /// use std::collections::HashSet; /// - /// fn pressed_scancode_set(e: &sdl2::event::EventPump) -> HashSet { + /// fn pressed_scancode_set(e: &sdl2::EventPump) -> HashSet { /// e.keyboard_state().pressed_scancodes().collect() /// } /// - /// fn pressed_keycode_set(e: &sdl2::event::EventPump) -> HashSet { + /// fn pressed_keycode_set(e: &sdl2::EventPump) -> HashSet { /// e.keyboard_state().pressed_scancodes() /// .filter_map(Keycode::from_scancode) /// .collect() diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index 9a56cc18..3db40418 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -28,7 +28,7 @@ //! None of the draw methods in `Renderer` are expected to fail. //! If they do, a panic is raised and the program is aborted. -use event::EventPump; +use EventPump; use video::{Window, WindowProperties, WindowPropertiesGetters}; use surface; use surface::{Surface, SurfaceRef}; diff --git a/src/sdl2/sdl.rs b/src/sdl2/sdl.rs index 3cb7fb91..b1070ce2 100644 --- a/src/sdl2/sdl.rs +++ b/src/sdl2/sdl.rs @@ -2,7 +2,6 @@ use std::ffi::{CStr, CString}; use std::rc::Rc; use sys::sdl as ll; -use event::EventPump; use util::CStringExt; #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] @@ -191,6 +190,53 @@ subsystem!(TimerSubsystem, ll::SDL_INIT_TIMER, sync); // The event queue can be read from other threads. subsystem!(EventSubsystem, ll::SDL_INIT_EVENTS, sync); +static mut IS_EVENT_PUMP_ALIVE: bool = false; + +/// A thread-safe type that encapsulates SDL event-pumping functions. +pub struct EventPump { + _sdldrop: Rc +} + +impl EventPump { + /// Obtains the SDL event pump. + #[inline] + fn new(sdl: &Sdl) -> SdlResult { + // Called on the main SDL thread. + + unsafe { + if IS_EVENT_PUMP_ALIVE { + Err(format!("an `EventPump` instance is already alive - there can only be one `EventPump` in use at a time.")) + } else { + // Initialize the events subsystem, just in case none of the other subsystems have done it yet. + let result = ll::SDL_InitSubSystem(ll::SDL_INIT_EVENTS); + + if result == 0 { + IS_EVENT_PUMP_ALIVE = true; + + Ok(EventPump { + _sdldrop: sdl.sdldrop.clone() + }) + } else { + Err(get_error()) + } + } + } + } +} + +impl Drop for EventPump { + #[inline] + fn drop(&mut self) { + // Called on the main SDL thread. + + unsafe { + assert!(IS_EVENT_PUMP_ALIVE); + ll::SDL_QuitSubSystem(ll::SDL_INIT_EVENTS); + IS_EVENT_PUMP_ALIVE = false; + } + } +} + /// Initializes the SDL library. /// This must be called before using any other SDL function. /// diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index 4cfebc35..6fc5731b 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -11,7 +11,7 @@ use render::RendererBuilder; use surface::SurfaceRef; use pixels; use VideoSubsystem; -use event::EventPump; +use EventPump; use SdlResult; use num::FromPrimitive; use util::CStringExt;