Skip to main content

gpui_shared_string_gpui_unofficial/
gpui_shared_string.rs

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