clang_ast/
dedup.rs

1use std::cell::Cell;
2
3const REFCOUNT: ::std::thread::LocalKey<Cell<usize>> =
    {
        const __INIT: Cell<usize> = { Cell::new(0) };
        unsafe {
            ::std::thread::LocalKey::new(const {
                        if ::std::mem::needs_drop::<Cell<usize>>() {
                            |_|
                                {
                                    #[thread_local]
                                    static VAL:
                                        ::std::thread::local_impl::EagerStorage<Cell<usize>> =
                                        ::std::thread::local_impl::EagerStorage::new(__INIT);
                                    VAL.get()
                                }
                        } else {
                            |_|
                                {
                                    #[thread_local]
                                    static VAL: Cell<usize> = __INIT;
                                    &VAL
                                }
                        }
                    })
        }
    };thread_local! {
4    static REFCOUNT: Cell<usize> = const { Cell::new(0) };
5}
6
7pub(crate) struct Guard {
8    _private: (),
9}
10
11pub(crate) fn activate() -> Guard {
12    REFCOUNT.with(|refcount| refcount.set(refcount.get() + 1));
13    Guard { _private: () }
14}
15
16impl Drop for Guard {
17    fn drop(&mut self) {
18        let prev = REFCOUNT.with(|refcount| refcount.replace(refcount.get() - 1));
19        if prev == 1 {
20            crate::loc::thread_local_reset();
21        }
22    }
23}