You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: lib.rs
    path of file: ./repos/Ambient/crates/timings/src
    
    the code of the file until where you have to start completion: use std::{collections::VecDeque, time::Duration};

use ambient_core::timing::{reporter, TimingEvent, TimingEventType};
use ambient_ecs::{components, Debuggable, FrameEvent, Resource, System, World, WorldContext};
use ambient_sys::time::Instant;
use winit::event::{DeviceEvent, Event, WindowEvent};

const MAX_SAMPLES: usize = 128;

#[derive(Clone, Copy, Debug)]
pub struct FrameTimings {
    event_times: [Option<Instant>; TimingEventType::COUNT],
}
impl FrameTimings {
    pub fn input_to_rendered(&self) -> Option<Duration> {
        self.event_times[TimingEventType::Input as usize]
            .zip(self.event_times[TimingEventType::RenderingFinished as usize])
            .map(|(input, rendered)| rendered - input)
    }
}
impl std::fmt::Display for FrameTimings {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut last = None;
        for (time, idx) in self
            .event_times
            .iter()
            .enumerate()
            .filter_map(|(idx, t)| t.zip(Some(idx)))
        {
            let event_type = TimingEventType::from_usize(idx).unwrap();
            if let Some(last) = last {
                write!(f, " <- {:?} -> ", time - last)?;
            }
            write!(f, "{:?}", event_type)?;
            last =
    
    fn run(&mut self, world: &mut World, _event: &FrameEvent) {
        // only process timing events in the app world so that they are available for the debugger
        if world.context() != WorldContext::App {
            return;
        }

        // timings of finished frames, to be added to the samples resource
        let mut pending_samples = Vec::new();

        let reporter = world.resource(reporter());
        for event in reporter.try_iter() {
            for f in self.frames.iter_mut() {
                f.process_event(event);
            }

            // the newest frame should always be accepting input
            if !self
                .frames
                .front()
                .map(Frame::is_accepting_input)
                .unwrap_or_default()
            {
                // the currently newest one is missing or not accepting input, so we need to add a new one
                self.frames.push_front(Default::default());
            }

            // pop finished frames
            while let Some(f) = self.frames.back() {
                if f.is_finished() {
                    let timings = self.frames.pop_back().unwrap().timings();
                    pending_samples.push(timings);
                } else {
                    break;
                }
            }
        }

        // add pending samples to the samples resource
        if !pending_samples.is_empty() {
            let samples = world.resource_mut(samples());
            for sample in pending_samples {
                while samples.len() + 1 >= MAX_SAMPLES {
                    samples.pop_front();
                }
                samples.push_back(sample);
            }
        }
    }