dear_imgui/
tokens.rs

1#[macro_export]
2/// This is a macro used internally by dear-imgui to create StackTokens
3/// representing various global state in Dear ImGui.
4///
5/// These tokens can either be allowed to drop or dropped manually
6/// by calling `end` on them. Preventing this token from dropping,
7/// or moving this token out of the block it was made in can have
8/// unintended side effects, including failed asserts in the Dear ImGui C++.
9///
10/// In general, if you're looking at this, don't overthink these -- just slap
11/// a `_token` as their binding name and allow them to drop.
12macro_rules! create_token {
13    (
14        $(#[$struct_meta:meta])*
15        $v:vis struct $token_name:ident<'ui>;
16
17        $(#[$end_meta:meta])*
18        drop { $on_drop:expr }
19    ) => {
20        #[must_use]
21        $(#[$struct_meta])*
22        pub struct $token_name<'a>(std::marker::PhantomData<&'a $crate::Ui>);
23
24        impl<'a> $token_name<'a> {
25            /// Creates a new token type.
26            pub(crate) fn new(_: &'a $crate::Ui) -> Self {
27                Self(std::marker::PhantomData)
28            }
29
30            $(#[$end_meta])*
31            #[inline]
32            pub fn end(self) {
33                // left empty for drop
34            }
35        }
36
37        impl Drop for $token_name<'_> {
38            fn drop(&mut self) {
39                // Execute provided drop expression; callers wrap unsafe if needed
40                $on_drop
41            }
42        }
43    }
44}