shared_string.rs

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