Skip to main content

dear_implot3d/ui/
token.rs

1use std::marker::PhantomData;
2
3use crate::{debug_end_plot, sys};
4use dear_imgui_rs::Ui;
5
6use super::binding::Plot3DContextBinding;
7
8/// RAII token that ends the plot on drop
9///
10/// This token is returned by `Plot3DBuilder::build()` and automatically calls
11/// `ImPlot3D_EndPlot()` when it goes out of scope, ensuring proper cleanup.
12pub struct Plot3DToken<'ui> {
13    pub(crate) binding: Plot3DContextBinding,
14    pub(crate) imgui_alive: Option<dear_imgui_rs::ContextAliveToken>,
15    pub(crate) _lifetime: PhantomData<&'ui Ui>,
16}
17
18impl Drop for Plot3DToken<'_> {
19    fn drop(&mut self) {
20        if let Some(alive) = &self.imgui_alive {
21            assert!(
22                alive.is_alive(),
23                "dear-implot3d: ImGui context has been dropped"
24            );
25        }
26        self.binding.bind();
27        unsafe {
28            debug_end_plot();
29            sys::ImPlot3D_EndPlot();
30        }
31    }
32}