    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
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176

    use serde::{Serialize, Serializer};
    use serde_untagged::UntaggedEnumVisitor;
    use std::borrow::Borrow;
    use std::cmp::Ordering;
    use std::collections::HashSet;
    use std::ffi::OsStr;
    use std::fmt;
    use std::hash::{Hash, Hasher};
    use std::ops::Deref;
    use std::path::Path;
    use std::ptr;
    use std::str;
    use std::sync::Mutex;
    use std::sync::OnceLock;

    pub static INTERNED_DEFAULT: InternedString = InternedString { inner: "default" };

    fn interned_storage() -> std::sync::MutexGuard<'static, HashSet<&'static str>> {
        static STRING_CACHE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
        STRING_CACHE
            .get_or_init(|| {
                let mut out: HashSet<&'static str> = Default::default();
                out.insert(INTERNED_DEFAULT.as_str());
                Mutex::new(out)
            })
            .lock()
            .unwrap()
    }

    #[derive(Clone, Copy)]
    pub struct InternedString {
        inner: &'static str,
    }

    impl<'a> From<&'a str> for InternedString {
        fn from(item: &'a str) -> Self {
            InternedString::new(item)
        }
    }

    impl<'a> From<&'a String> for InternedString {
        fn from(item: &'a String) -> Self {
            InternedString::new(item)
        }
    }

    impl From<String> for InternedString {
        fn from(item: String) -> Self {
            InternedString::new(&item)
        }
    }

    impl PartialEq for InternedString {
        fn eq(&self, other: &InternedString) -> bool {
            ptr::eq(self.as_str(), other.as_str())
        }
    }

    impl PartialEq<str> for InternedString {
        fn eq(&self, other: &str) -> bool {
            *self == other
        }
    }

    impl<'a> PartialEq<&'a str> for InternedString {
        fn eq(&self, other: &&str) -> bool {
            **self == **other
        }
    }

    impl Eq for InternedString {}

    impl InternedString {
        pub fn new(str: &str) -> InternedString {
            let mut cache = interned_storage();
            let s = cache.get(str).copied().unwrap_or_else(|| {
                let s = str.to_string().leak();
                cache.insert(s);
                s
            });

            InternedString { inner: s }
        }

        pub fn as_str(&self) -> &'static str {
            self.inner
        }
    }

    impl Deref for InternedString {
        type Target = str;

        fn deref(&self) -> &'static str {
            self.as_str()
        }
    }

    impl AsRef<str> for InternedString {
        fn as_ref(&self) -> &str {
            self.as_str()
        }
    }

    impl AsRef<OsStr> for InternedString {
        fn as_ref(&self) -> &OsStr {
            self.as_str().as_ref()
        }
    }

    impl AsRef<Path> for InternedString {
        fn as_ref(&self) -> &Path {
            self.as_str().as_ref()
        }
    }

    impl Hash for InternedString {
        // N.B., we can't implement this as `identity(self).hash(state)`,
        // because we use this for on-disk fingerprints and so need
        // stability across Cargo invocations.
        fn hash<H: Hasher>(&self, state: &mut H) {
            self.as_str().hash(state);
        }
    }

    impl Borrow<str> for InternedString {
        // If we implement Hash as `identity(self).hash(state)`,
        // then this will need to be removed.
        fn borrow(&self) -> &str {
            self.as_str()
        }
    }

    impl fmt::Debug for InternedString {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            fmt::Debug::fmt(self.as_str(), f)
        }
    }

    impl fmt::Display for InternedString {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            fmt::Display::fmt(self.as_str(), f)
        }
    }

    impl Ord for InternedString {
        fn cmp(&self, other: &InternedString) -> Ordering {
            self.as_str().cmp(other.as_str())
        }
    }

    impl PartialOrd for InternedString {
        fn partial_cmp(&self, other: &InternedString) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }

    impl Serialize for InternedString {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_str(self.inner)
        }
    }

    impl<'de> serde::Deserialize<'de> for InternedString {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            UntaggedEnumVisitor::new()
                .expecting("an String like thing")
                .string(|value| Ok(InternedString::new(value)))
                .deserialize(deserializer)
        }
    }