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
use std::borrow::Borrow;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Add, Deref};

use crate::Str;

pub type ArcStr = std::sync::Arc<str>;

/// Used to hold an immutable string.
///
/// It can construct as a const (by AtomicStr::ever).
#[derive(Debug, Clone, Eq)]
pub enum AtomicStr {
    Arc(ArcStr),
    Static(&'static str),
}

// unsafe impl Sync for AtomicStr {}

impl PartialEq for AtomicStr {
    #[inline]
    fn eq(&self, other: &AtomicStr) -> bool {
        self[..] == other[..]
    }
}

impl Add<&str> for AtomicStr {
    type Output = AtomicStr;
    #[inline]
    fn add(self, other: &str) -> AtomicStr {
        AtomicStr::from(&format!("{self}{other}"))
    }
}

impl Hash for AtomicStr {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            AtomicStr::Arc(s) => (s[..]).hash(state),
            AtomicStr::Static(s) => s.hash(state),
        }
    }
}

impl fmt::Display for AtomicStr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            AtomicStr::Arc(s) => write!(f, "{s}"),
            AtomicStr::Static(s) => write!(f, "{s}"),
        }
    }
}

// &'static str -> &strになってしまわないように
// あえて`impl<S: Into<Str>> From<S> for AtomicStr { ... }`はしない
impl From<&'static str> for AtomicStr {
    #[inline]
    fn from(s: &'static str) -> Self {
        AtomicStr::ever(s)
    }
}

impl From<&String> for AtomicStr {
    #[inline]
    fn from(s: &String) -> Self {
        AtomicStr::Arc((s[..]).into())
    }
}

impl From<String> for AtomicStr {
    #[inline]
    fn from(s: String) -> Self {
        AtomicStr::Arc((s[..]).into())
    }
}

impl From<&ArcStr> for AtomicStr {
    #[inline]
    fn from(s: &ArcStr) -> Self {
        AtomicStr::Arc(s.clone())
    }
}

impl From<ArcStr> for AtomicStr {
    #[inline]
    fn from(s: ArcStr) -> Self {
        AtomicStr::Arc(s)
    }
}

impl From<&AtomicStr> for AtomicStr {
    #[inline]
    fn from(s: &AtomicStr) -> Self {
        match s {
            AtomicStr::Arc(s) => AtomicStr::Arc(s.clone()),
            AtomicStr::Static(s) => AtomicStr::Static(s),
        }
    }
}

impl From<Str> for AtomicStr {
    #[inline]
    fn from(s: Str) -> Self {
        match s {
            Str::Rc(s) => AtomicStr::Arc((&s[..]).into()),
            Str::Static(s) => AtomicStr::Static(s),
        }
    }
}

impl From<&Str> for AtomicStr {
    #[inline]
    fn from(s: &Str) -> Self {
        match s {
            Str::Rc(s) => AtomicStr::Arc((&s[..]).into()),
            Str::Static(s) => AtomicStr::Static(s),
        }
    }
}

impl Deref for AtomicStr {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        self.borrow()
    }
}

impl Borrow<str> for AtomicStr {
    #[inline]
    fn borrow(&self) -> &str {
        match self {
            AtomicStr::Arc(s) => s.borrow(),
            AtomicStr::Static(s) => s,
        }
    }
}

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

impl AtomicStr {
    pub const fn ever(s: &'static str) -> Self {
        AtomicStr::Static(s)
    }

    pub fn arc(s: &str) -> Self {
        AtomicStr::Arc(s.into())
    }

    pub fn into_rc(self) -> ArcStr {
        match self {
            AtomicStr::Arc(s) => s,
            AtomicStr::Static(s) => ArcStr::from(s),
        }
    }

    pub fn is_uppercase(&self) -> bool {
        self.chars()
            .next()
            .map(|c| c.is_uppercase())
            .unwrap_or(false)
    }
}