1use derive_more::{Deref, DerefMut};
2
3use serde::{Deserialize, Serialize};
4use std::{borrow::Borrow, sync::Arc};
5use util::arc_cow::ArcCow;
6
7/// A shared string is an immutable string that can be cheaply cloned in GPUI
8/// tasks. Essentially an abstraction over an `Arc<str>` and `&'static str`,
9#[derive(Deref, DerefMut, Eq, PartialEq, PartialOrd, Ord, Hash, Clone)]
10pub struct SharedString(ArcCow<'static, str>);
11
12impl SharedString {
13 /// Creates a static [`SharedString`] from a `&'static str`.
14 pub const fn new_static(str: &'static str) -> Self {
15 Self(ArcCow::Borrowed(str))
16 }
17}
18
19impl Default for SharedString {
20 fn default() -> Self {
21 Self(ArcCow::Owned(Arc::default()))
22 }
23}
24
25impl AsRef<str> for SharedString {
26 fn as_ref(&self) -> &str {
27 &self.0
28 }
29}
30
31impl Borrow<str> for SharedString {
32 fn borrow(&self) -> &str {
33 self.as_ref()
34 }
35}
36
37impl std::fmt::Debug for SharedString {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 self.0.fmt(f)
40 }
41}
42
43impl std::fmt::Display for SharedString {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 write!(f, "{}", self.0.as_ref())
46 }
47}
48
49impl PartialEq<String> for SharedString {
50 fn eq(&self, other: &String) -> bool {
51 self.as_ref() == other
52 }
53}
54
55impl PartialEq<SharedString> for String {
56 fn eq(&self, other: &SharedString) -> bool {
57 self == other.as_ref()
58 }
59}
60
61impl PartialEq<str> for SharedString {
62 fn eq(&self, other: &str) -> bool {
63 self.as_ref() == other
64 }
65}
66
67impl<'a> PartialEq<&'a str> for SharedString {
68 fn eq(&self, other: &&'a str) -> bool {
69 self.as_ref() == *other
70 }
71}
72
73impl From<SharedString> for Arc<str> {
74 fn from(val: SharedString) -> Self {
75 match val.0 {
76 ArcCow::Borrowed(borrowed) => Arc::from(borrowed),
77 ArcCow::Owned(owned) => owned.clone(),
78 }
79 }
80}
81
82impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
83 fn from(value: T) -> Self {
84 Self(value.into())
85 }
86}
87
88impl From<SharedString> for String {
89 fn from(val: SharedString) -> Self {
90 val.0.to_string()
91 }
92}
93
94impl Serialize for SharedString {
95 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
96 where
97 S: serde::Serializer,
98 {
99 serializer.serialize_str(self.as_ref())
100 }
101}
102
103impl<'de> Deserialize<'de> for SharedString {
104 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
105 where
106 D: serde::Deserializer<'de>,
107 {
108 let s = String::deserialize(deserializer)?;
109 Ok(SharedString::from(s))
110 }
111}