Skip to main content

gpui_shared_string/
gpui_shared_string.rs

1use std::{
2    borrow::{Borrow, Cow},
3    iter,
4    sync::Arc,
5};
6
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9use smol_str::SmolStr;
10
11/// A shared string is an immutable string that can be cheaply cloned in GPUI
12/// tasks. Essentially an abstraction over an `Arc<str>` and `&'static str`,
13/// currently backed by a [`SmolStr`].
14#[derive(Eq, PartialOrd, Ord, Clone)]
15pub struct SharedString(SmolStr);
16
17impl PartialEq for SharedString {
18    fn eq(&self, other: &Self) -> bool {
19        let (this, other_str) = (self.as_str(), other.as_str());
20        (this.as_ptr() == other_str.as_ptr() && this.len() == other_str.len()) || self.0 == other.0
21    }
22}
23
24impl std::hash::Hash for SharedString {
25    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
26        self.0.hash(state);
27    }
28}
29
30impl std::ops::Deref for SharedString {
31    type Target = str;
32
33    fn deref(&self) -> &Self::Target {
34        self.0.as_str()
35    }
36}
37
38impl SharedString {
39    /// Creates a static [`SharedString`] from a `&'static str`.
40    pub const fn new_static(str: &'static str) -> Self {
41        Self(SmolStr::new_static(str))
42    }
43
44    /// Creates a [`SharedString`].
45    pub fn new(str: impl AsRef<str>) -> Self {
46        SharedString(SmolStr::new(str))
47    }
48
49    /// Get a &str from the underlying string.
50    pub fn as_str(&self) -> &str {
51        &self.0
52    }
53}
54
55impl JsonSchema for SharedString {
56    fn inline_schema() -> bool {
57        String::inline_schema()
58    }
59
60    fn schema_name() -> Cow<'static, str> {
61        String::schema_name()
62    }
63
64    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
65        String::json_schema(generator)
66    }
67}
68
69impl Default for SharedString {
70    fn default() -> Self {
71        Self::new_static("")
72    }
73}
74
75impl AsRef<str> for SharedString {
76    fn as_ref(&self) -> &str {
77        &self.0
78    }
79}
80
81impl Borrow<str> for SharedString {
82    fn borrow(&self) -> &str {
83        self.as_ref()
84    }
85}
86
87impl std::fmt::Debug for SharedString {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        self.0.fmt(f)
90    }
91}
92
93impl std::fmt::Display for SharedString {
94    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95        write!(f, "{}", self.0.as_str())
96    }
97}
98
99impl PartialEq<String> for SharedString {
100    fn eq(&self, other: &String) -> bool {
101        self.as_ref() == other
102    }
103}
104
105impl PartialEq<SharedString> for String {
106    fn eq(&self, other: &SharedString) -> bool {
107        self == other.as_ref()
108    }
109}
110
111impl PartialEq<str> for SharedString {
112    fn eq(&self, other: &str) -> bool {
113        self.as_ref() == other
114    }
115}
116
117impl<'a> PartialEq<&'a str> for SharedString {
118    fn eq(&self, other: &&'a str) -> bool {
119        self.as_ref() == *other
120    }
121}
122
123impl From<&SharedString> for SharedString {
124    #[inline]
125    fn from(s: &SharedString) -> SharedString {
126        s.clone()
127    }
128}
129
130impl From<&str> for SharedString {
131    #[inline]
132    fn from(s: &str) -> SharedString {
133        SharedString(SmolStr::from(s))
134    }
135}
136
137impl From<char> for SharedString {
138    #[inline]
139    fn from(c: char) -> SharedString {
140        SharedString(SmolStr::from_iter(iter::once(c)))
141    }
142}
143
144impl From<&mut str> for SharedString {
145    #[inline]
146    fn from(s: &mut str) -> SharedString {
147        SharedString(SmolStr::from(s))
148    }
149}
150
151impl From<&String> for SharedString {
152    #[inline]
153    fn from(s: &String) -> SharedString {
154        SharedString(SmolStr::from(s))
155    }
156}
157
158impl From<String> for SharedString {
159    #[inline(always)]
160    fn from(text: String) -> Self {
161        SharedString(SmolStr::from(text))
162    }
163}
164
165impl From<Box<str>> for SharedString {
166    #[inline]
167    fn from(s: Box<str>) -> SharedString {
168        SharedString(SmolStr::from(s))
169    }
170}
171
172impl From<Arc<str>> for SharedString {
173    #[inline]
174    fn from(s: Arc<str>) -> SharedString {
175        SharedString(SmolStr::from(s))
176    }
177}
178
179impl From<&Arc<str>> for SharedString {
180    #[inline]
181    fn from(s: &Arc<str>) -> SharedString {
182        SharedString(SmolStr::from(s.clone()))
183    }
184}
185
186impl<'a> From<Cow<'a, str>> for SharedString {
187    #[inline]
188    fn from(s: Cow<'a, str>) -> SharedString {
189        SharedString(SmolStr::from(s))
190    }
191}
192
193impl From<SharedString> for Arc<str> {
194    #[inline(always)]
195    fn from(text: SharedString) -> Self {
196        text.0.into()
197    }
198}
199
200impl From<SharedString> for String {
201    #[inline(always)]
202    fn from(text: SharedString) -> Self {
203        text.0.into()
204    }
205}
206
207impl Serialize for SharedString {
208    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
209    where
210        S: serde::Serializer,
211    {
212        serializer.serialize_str(self.as_ref())
213    }
214}
215
216impl<'de> Deserialize<'de> for SharedString {
217    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
218    where
219        D: serde::Deserializer<'de>,
220    {
221        SmolStr::deserialize(deserializer).map(SharedString)
222    }
223}