clang_ast/
intern.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use foldhash::HashSet;
use serde::de::{DeserializeSeed, Deserializer, Error, Visitor};
use std::cell::{Cell, RefCell};
use std::fmt;
use std::sync::Arc;

thread_local! {
    static REFCOUNT: Cell<usize> = const { Cell::new(0) };
    static INTERN: RefCell<HashSet<Arc<str>>> = RefCell::new(HashSet::default());
}

fn borrowed(string: &str) -> Arc<str> {
    do_intern(string)
}

fn owned(string: String) -> Arc<str> {
    do_intern(string)
}

fn do_intern(string: impl AsRef<str> + Into<Arc<str>>) -> Arc<str> {
    INTERN.with(|intern| {
        let mut intern = intern.borrow_mut();
        if let Some(arc) = intern.get(string.as_ref()) {
            Arc::clone(arc)
        } else {
            let arc: Arc<str> = string.into();
            intern.insert(Arc::clone(&arc));
            arc
        }
    })
}

pub(crate) struct Guard {
    _private: (),
}

pub(crate) fn activate() -> Guard {
    REFCOUNT.with(|refcount| refcount.set(refcount.get() + 1));
    Guard { _private: () }
}

impl Drop for Guard {
    fn drop(&mut self) {
        let prev = REFCOUNT.with(|refcount| refcount.replace(refcount.get() - 1));
        if prev == 1 {
            crate::loc::thread_local_reset();
            INTERN.with(|intern| intern.borrow_mut().clear());
        }
    }
}

pub(crate) struct InternVisitor;

impl<'de> Visitor<'de> for InternVisitor {
    type Value = Arc<str>;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a string")
    }

    fn visit_str<E>(self, string: &str) -> Result<Self::Value, E>
    where
        E: Error,
    {
        Ok(borrowed(string))
    }

    fn visit_string<E>(self, string: String) -> Result<Self::Value, E>
    where
        E: Error,
    {
        Ok(owned(string))
    }
}

impl<'de> DeserializeSeed<'de> for InternVisitor {
    type Value = Arc<str>;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(self)
    }
}