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

Code before:
            self.context.replace_with(|context| {
                context
                    .make_not_current()
                    .expect("failed to disable context")
                    .treat_as_possibly_current()
            });
        }
    }

    pub fn make_current(&mut self, recover: bool) {
        let is_current = self.context.get().is_current();
        if is_current && !recover {
            return;
        }

        let mut was_context_reset = if is_current {
            false
        } else {
            match self.context.make_current(&self.surface) {
                Err(err) if err.error_kind() == ErrorKind::ContextLost => {
                    info!("Context lost for window {:?}", self.window.id());
                    true
                },
                _ => false,
            }
        };

        was_context_reset |= self.renderer.was_context_reset();

        if was_context_reset && recover {
            let gl_display = self.context.display();
            let gl_config = self.context.config();
            let raw_window_handle = Some(self.window.raw_window_handle());
            let context =
                renderer::platform::create_gl_context(&gl_display, &gl_config, raw_window_handle)
                    .expect("failed to recreate context.");

            // Drop the old context and renderer.
            unsafe {
                ManuallyDrop::drop(&mut self.renderer);
                ManuallyDrop::drop(&mut self.context);
            }

            // Activate new context.
            let context = context.treat_as_possibly_current();
            self.context = ManuallyDrop::new(Replaceable::new(context));
            self.context
                .make_current(&self.surface)
                .expect("failed to reativate context after reset.");

            // Recreate renderer.

Review comment (suggestion):
```suggestion
        // Attempt to make the context current, if it is not.
        let mut was_context_reset = if is_current {
            false
        } else {
            match self.context.make_current(&self.surface) {
                Err(err) if err.error_kind() == ErrorKind::ContextLost => {
                    info!("Context lost for window {:?}", self.window.id());
                    true
                },
                _ => false,
            }
        };
```

I think a comment here is nice to explain that our goal here is not just checking if the context was reset, but actually performing the make_current call.

Code after:
            self.context.replace_with(|context| {
                context
                    .make_not_current()
                    .expect("failed to disable context")
                    .treat_as_possibly_current()
            });
        }
    }

    pub fn make_current(&mut self) {
        let is_current = self.context.get().is_current();

        // Attempt to make the context current if it's not.
        let context_loss = if is_current {
            self.renderer.was_context_reset()
        } else {
            match self.context.make_current(&self.surface) {
                Err(err) if err.error_kind() == ErrorKind::ContextLost => {
                    info!("Context lost for window {:?}", self.window.id());
                    true
                },
                _ => false,
            }
        };

        if !context_loss {
            return;
        }

        let gl_display = self.context.display();
        let gl_config = self.context.config();
        let raw_window_handle = Some(self.window.raw_window_handle());
        let context = platform::create_gl_context(&gl_display, &gl_config, raw_window_handle)
            .expect("failed to recreate context.");

        // Drop the old context and renderer.
        unsafe {
            ManuallyDrop::drop(&mut self.renderer);
            ManuallyDrop::drop(&mut self.context);
        }

        // Activate new context.
        let context = context.treat_as_possibly_current();
        self.context = ManuallyDrop::new(Replaceable::new(context));
        self.context.make_current(&self.surface).expect("failed to reativate context after reset.");

        // Recreate renderer.
        let renderer = Renderer::new(&self.context, self.renderer_preference)
            .expect("failed to recreate renderer after reset");
        self.renderer = ManuallyDrop::new(renderer);