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