gpui/
shared_string.rs

1use derive_more::{Deref, DerefMut};
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use std::{
6    borrow::{Borrow, Cow},
7    sync::Arc,
8};
9use util::arc_cow::ArcCow;
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#[derive(Deref, DerefMut, Eq, PartialEq, PartialOrd, Ord, Hash, Clone)]
14pub struct SharedString(ArcCow<'static, str>);
15
16impl SharedString {
17    /// Creates a static [`SharedString`] from a `&'static str`.
18    pub const fn new_static(str: &'static str) -> Self {
19        Self(ArcCow::Borrowed(str))
20    }
21
22    /// Creates a [`SharedString`] from anything that can become an `Arc<str>`
23    pub fn new(str: impl Into<Arc<str>>) -> Self {
24        SharedString(ArcCow::Owned(str.into()))
25    }
26
27    /// Get a &str from the underlying string.
28    pub fn as_str(&self) -> &str {
29        &self.0
30    }
31}
32
33impl JsonSchema for SharedString {
34    fn inline_schema() -> bool {
35        String::inline_schema()
36    }
37
38    fn schema_name() -> Cow<'static, str> {
39        String::schema_name()
40    }
41
42    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
43        String::json_schema(generator)
44    }
45}
46
47impl Default for SharedString {
48    fn default() -> Self {
49        Self(ArcCow::Owned(Arc::default()))
50    }
51}
52
53impl AsRef<str> for SharedString {
54    fn as_ref(&self) -> &str {
55        &self.0
56    }
57}
58
59impl Borrow<str> for SharedString {
60    fn borrow(&self) -> &str {
61        self.as_ref()
62    }
63}
64
65impl std::fmt::Debug for SharedString {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        self.0.fmt(f)
68    }
69}
70
71impl std::fmt::Display for SharedString {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        write!(f, "{}", self.0.as_ref())
74    }
75}
76
77impl PartialEq<String> for SharedString {
78    fn eq(&self, other: &String) -> bool {
79        self.as_ref() == other
80    }
81}
82
83impl PartialEq<SharedString> for String {
84    fn eq(&self, other: &SharedString) -> bool {
85        self == other.as_ref()
86    }
87}
88
89impl PartialEq<str> for SharedString {
90    fn eq(&self, other: &str) -> bool {
91        self.as_ref() == other
92    }
93}
94
95impl<'a> PartialEq<&'a str> for SharedString {
96    fn eq(&self, other: &&'a str) -> bool {
97        self.as_ref() == *other
98    }
99}
100
101impl From<&SharedString> for SharedString {
102    fn from(value: &SharedString) -> Self {
103        value.clone()
104    }
105}
106
107impl From<SharedString> for Arc<str> {
108    fn from(val: SharedString) -> Self {
109        match val.0 {
110            ArcCow::Borrowed(borrowed) => Arc::from(borrowed),
111            ArcCow::Owned(owned) => owned,
112        }
113    }
114}
115
116impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
117    fn from(value: T) -> Self {
118        Self(value.into())
119    }
120}
121
122impl From<SharedString> for String {
123    fn from(val: SharedString) -> Self {
124        val.0.to_string()
125    }
126}
127
128impl Serialize for SharedString {
129    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
130    where
131        S: serde::Serializer,
132    {
133        serializer.serialize_str(self.as_ref())
134    }
135}
136
137impl<'de> Deserialize<'de> for SharedString {
138    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
139    where
140        D: serde::Deserializer<'de>,
141    {
142        let s = String::deserialize(deserializer)?;
143        Ok(SharedString::from(s))
144    }
145}