arcdps_imgui/
tokens.rs

1#[macro_export]
2/// This is a macro used internally by imgui-rs to create StackTokens
3/// representing various global state in DearImGui.
4///
5/// These tokens can either be allowed to drop or dropped manually
6/// by called `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 DearImGui 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>($crate::__core::marker::PhantomData<crate::Ui<'a>>);
23
24        impl<'a> $token_name<'a> {
25            /// Creates a new token type.
26            pub(crate) fn new(_: &crate::Ui<'a>) -> 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                unsafe { $on_drop }
40            }
41        }
42    }
43}