Repository: alacritty/alacritty
File: alacritty/src/event.rs (Rust)

Code before:
                },
                // Process events
                GlutinEvent::RedrawEventsCleared => {
                    *control_flow = ControlFlow::Wait;

                    let event_queue_empty = event_queue.is_empty();

                    #[cfg(not(any(target_os = "macos", windows)))]
                    {
                        let wayland_queue_empty = if event_loop.is_wayland() {
                            let num_dispatched = self
                                .display
                                .wayland_event_queue
                                .as_mut()
                                .unwrap()
                                .dispatch_pending(&mut (), |_, _, _| {})
                                .unwrap();
                            num_dispatched == 0
                        } else {
                            true
                        };

                        if event_queue_empty && wayland_queue_empty {
                            return;
                        }
                    }
                    #[cfg(any(target_os = "macos", windows))]
                    {
                        if event_queue_empty {
                            return;
                        }
                    }
                },
                // Remap DPR change event to remove lifetime
                GlutinEvent::WindowEvent {
                    event: WindowEvent::ScaleFactorChanged { scale_factor, new_inner_size },
                    ..
                } => {
                    *control_flow = ControlFlow::Poll;
                    let size = (new_inner_size.width, new_inner_size.height);
                    let event = GlutinEvent::UserEvent(Event::DPRChanged(scale_factor, size));
                    event_queue.push(event);
                    return;
                },
                // Transmute to extend lifetime, which exists only for `ScaleFactorChanged` event.
                // Since we remap that event to remove the lifetime, this is safe.
                event => unsafe {
                    *control_flow = ControlFlow::Poll;
                    event_queue.push(mem::transmute(event));
                    return;
                },

Review comment (refactor):
It might just be because of the terrible formatting which can be fixed separately, but these are relatively big platform-dependent blocks. Might be worth extracting this into a separate method?

Code after:
    #[cfg(any(target_os = "macos", windows))]
    fn event_queue_empty(&mut self) -> bool {
        self.event_queue.is_empty()
    }

    /// Run the event loop.
    pub fn run<T>(&mut self, terminal: Arc<FairMutex<Term<T>>>, mut event_loop: EventLoop<Event>)
    where
        T: EventListener,
    {
        event_loop.run_return(|event, event_loop, control_flow| {
            if self.config.debug.print_events {
                info!("glutin event: {:?}", event);
            }

            // Ignore all events we do not care about.
            if Self::skip_event(&event) {
                return;
            }

            match event {
                // Check for shutdown.
                GlutinEvent::UserEvent(Event::Exit) => {
                    *control_flow = ControlFlow::Exit;
                    return;
                },
                // Process events.
                GlutinEvent::RedrawEventsCleared => {
                    *control_flow = ControlFlow::Wait;

                    if self.event_queue_empty() {
                        return;
                    }
                },
                // Remap DPR change event to remove lifetime.
                GlutinEvent::WindowEvent {
                    event: WindowEvent::ScaleFactorChanged { scale_factor, new_inner_size },
                    ..
                } => {
                    *control_flow = ControlFlow::Poll;
                    let size = (new_inner_size.width, new_inner_size.height);
                    let event = GlutinEvent::UserEvent(Event::DPRChanged(scale_factor, size));
                    self.event_queue.push(event);
                    return;
                },
                // Transmute to extend lifetime, which exists only for `ScaleFactorChanged` event.
                // Since we remap that event to remove the lifetime, this is safe.
                event => unsafe {
                    *control_flow = ControlFlow::Poll;
                    self.event_queue.push(mem::transmute(event));
                    return;