Skip to main content

gpui_shared_string/
gpui_shared_string.rs

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