<commit_before>/*!
Event Handling
 */

use std::ffi::CStr;
use std::mem;
use libc::{c_int, c_void, uint32_t};
use num::FromPrimitive;
use std::ptr;
use std::borrow::ToOwned;
use std::iter::FromIterator;
use std::marker::PhantomData;

use controller;
use controller::{Axis, Button};
use joystick;
use joystick::HatState;
use keyboard;
use keyboard::Mod;
use sys::keycode::SDL_Keymod;
use keyboard::Keycode;
use mouse;
use mouse::{Mouse, MouseState};
use keyboard::Scancode;
use get_error;
use SdlResult;
use ErrorMessage;

use sys::event as ll;

impl ::EventSubsystem {
    /// Removes all events in the event queue that match the specified event type.
    pub fn flush_event(&self, event_type: EventType) {
        unsafe { ll::SDL_FlushEvent(event_type as uint32_t) };
    }

    /// Removes all events in the event queue that match the specified type range.
    pub fn flush_events(&self, min_type: u32, max_type: u32) {
        unsafe { ll::SDL_FlushEvents(min_type, max_type) };
    }

    /// Reads the events at the front of the event queue, until the maximum amount
    /// of events is read.
    ///
    /// The events will _not_ be removed from the queue.
    ///
    /// # Example
    /// ```no_run
    /// use sdl2::event::Event;
    ///
    /// let sdl_context = sdl2::init().unwrap();
    /// let event_subsystem = sdl_context.event().unwrap();
    ///
    /// // Read up to 1024 events
    /// let events: Vec<Event> = event_subsystem.peek_events(1024);
    ///
    /// // Print each one
    /// for event in events {
    ///     println!("{:?}", event);
    /// }
    /// ```
    pub fn peek_events<B>(&self, max_amount: u32) -> B
    where B: FromIterator<Event>
    {
        unsafe {
            let mut events = Vec::with_capacity(max_amount as usize);

            let result = {
                let events_ptr = events.as_mut_ptr();

                ll::SDL_PeepEvents(
                    events_ptr,
                    max_amount as c_int,
                    ll::SDL_PEEKEVENT,
                    ll::SDL_FIRSTEVENT,
                    ll::SDL_LASTEVENT
                )
            };

            if result < 0 {
                // The only error possible is "Couldn't lock event queue"
                panic!(get_error());
            } else {
                events.set_len(max_amount as usize);

                events.into_iter().map(|event_raw| {<commit_msg>Don't return garbage events from peek_events<commit_after>/*!
Event Handling
 */

use std::ffi::CStr;
use std::mem;
use libc::{c_int, c_void, uint32_t};
use num::FromPrimitive;
use std::ptr;
use std::borrow::ToOwned;
use std::iter::FromIterator;
use std::marker::PhantomData;

use controller;
use controller::{Axis, Button};
use joystick;
use joystick::HatState;
use keyboard;
use keyboard::Mod;
use sys::keycode::SDL_Keymod;
use keyboard::Keycode;
use mouse;
use mouse::{Mouse, MouseState};
use keyboard::Scancode;
use get_error;
use SdlResult;
use ErrorMessage;

use sys::event as ll;

impl ::EventSubsystem {
    /// Removes all events in the event queue that match the specified event type.
    pub fn flush_event(&self, event_type: EventType) {
        unsafe { ll::SDL_FlushEvent(event_type as uint32_t) };
    }

    /// Removes all events in the event queue that match the specified type range.
    pub fn flush_events(&self, min_type: u32, max_type: u32) {
        unsafe { ll::SDL_FlushEvents(min_type, max_type) };
    }

    /// Reads the events at the front of the event queue, until the maximum amount
    /// of events is read.
    ///
    /// The events will _not_ be removed from the queue.
    ///
    /// # Example
    /// ```no_run
    /// use sdl2::event::Event;
    ///
    /// let sdl_context = sdl2::init().unwrap();
    /// let event_subsystem = sdl_context.event().unwrap();
    ///
    /// // Read up to 1024 events
    /// let events: Vec<Event> = event_subsystem.peek_events(1024);
    ///
    /// // Print each one
    /// for event in events {
    ///     println!("{:?}", event);
    /// }
    /// ```
    pub fn peek_events<B>(&self, max_amount: u32) -> B
    where B: FromIterator<Event>
    {
        unsafe {
            let mut events = Vec::with_capacity(max_amount as usize);

            let result = {
                let events_ptr = events.as_mut_ptr();

                ll::SDL_PeepEvents(
                    events_ptr,
                    max_amount as c_int,
                    ll::SDL_PEEKEVENT,
                    ll::SDL_FIRSTEVENT,
                    ll::SDL_LASTEVENT
                )
            };

            if result < 0 {
                // The only error possible is "Couldn't lock event queue"
                panic!(get_error());
            } else {
                events.set_len(result as usize);

                events.into_iter().map(|event_raw| {
{'fraction_non_alphanumeric': 0.12985663827134844, 'fraction_numerical': 0.008726366091834614, 'mean_word_length': 2.1018041237113403, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2177986', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1464, 'n_words': 413}