Repository: alacritty/alacritty
File: alacritty/src/display/mod.rs (Rust)

Code before:
    pub fn get_mut(&mut self) -> &mut T {
        self.0.as_mut().unwrap()
    }
}

impl<T> Deref for Replaceable<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.get()
    }
}

impl<T> DerefMut for Replaceable<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.get_mut()
    }
}

/// The frame timer state.
#[derive(Default)]
pub struct FrameTimerInfo {
    /// The optimal delay for the renderer.
    swap_delay: Option<Duration>,

    /// The time stamp of the previous contiguos frame.
    frame_timestamp: Option<Instant>,

    /// The refresh rate we've used to compute the `swap_delay`.
    refresh_interval: Duration,
}

impl FrameTimerInfo {
    /// Compute the delay that we should use to achieve the target frame
    /// rate.
    pub fn compute_delay(&mut self, refresh_interval: Duration) -> Duration {
        // If the refresh rate has changed, reset the timings.
        if self.refresh_interval != refresh_interval || self.swap_delay.is_none() {
            self.refresh_interval = refresh_interval;
            self.frame_timestamp = Some(Instant::now());
            self.swap_delay = Some(refresh_interval);
            return refresh_interval;
        }

        // At this point we know that `swap_delay` has some value.
        let swap_delay = self.swap_delay.unwrap();

        let new_swap_delay = match self.frame_timestamp {
            Some(last_frame_timestamp) => {
                // Compute the error.
                let error = self.refresh_interval.as_micros() as i64

Review comment (suggestion):
```suggestion
    /// The timestamp of the previous contiguous frame.
```

Besides the typo, what is "contiguous frame" supposed to mean here? Isn't it already implied by "previous" that we're talking about the frame right before this?

I think just this should be better:

```suggestion
    /// The timestamp of the previous frame.
```

Code after:
    pub fn replace_with<F: FnMut(T) -> T>(&mut self, f: F) {
        self.0 = self.0.take().map(f);
    }

    /// Get immutable access to the wrapped value.
    pub fn get(&self) -> &T {
        self.0.as_ref().unwrap()
    }

    /// Get mutable access to the wrapped value.
    pub fn get_mut(&mut self) -> &mut T {
        self.0.as_mut().unwrap()
    }
}

impl<T> Deref for Replaceable<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.get()
    }
}

impl<T> DerefMut for Replaceable<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.get_mut()
    }
}

/// The frame timer state.
pub struct FrameTimer {
    /// Base timestamp used to compute sync points.
    base: Instant,

    /// The last timestamp we synced to.
    last_synced_timestamp: Instant,

    /// The refresh rate we've used to compute sync timestamps.
    refresh_interval: Duration,
}

impl FrameTimer {
    pub fn new() -> Self {
        let now = Instant::now();
        Self { base: now, last_synced_timestamp: now, refresh_interval: Duration::ZERO }
    }

    /// Compute the delay that we should use to achieve the target frame
    /// rate.
    pub fn compute_timeout(&mut self, refresh_interval: Duration) -> Duration {
        let now = Instant::now();