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
 20impl JsonSchema for SharedString {
 21    fn schema_name() -> String {
 22        String::schema_name()
 23    }
 24
 25    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
 26        String::json_schema(gen)
 27    }
 28}
 29
 30impl Default for SharedString {
 31    fn default() -> Self {
 32        Self(ArcCow::Owned(Arc::default()))
 33    }
 34}
 35
 36impl AsRef<str> for SharedString {
 37    fn as_ref(&self) -> &str {
 38        &self.0
 39    }
 40}
 41
 42impl Borrow<str> for SharedString {
 43    fn borrow(&self) -> &str {
 44        self.as_ref()
 45    }
 46}
 47
 48impl std::fmt::Debug for SharedString {
 49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 50        self.0.fmt(f)
 51    }
 52}
 53
 54impl std::fmt::Display for SharedString {
 55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 56        write!(f, "{}", self.0.as_ref())
 57    }
 58}
 59
 60impl PartialEq<String> for SharedString {
 61    fn eq(&self, other: &String) -> bool {
 62        self.as_ref() == other
 63    }
 64}
 65
 66impl PartialEq<SharedString> for String {
 67    fn eq(&self, other: &SharedString) -> bool {
 68        self == other.as_ref()
 69    }
 70}
 71
 72impl PartialEq<str> for SharedString {
 73    fn eq(&self, other: &str) -> bool {
 74        self.as_ref() == other
 75    }
 76}
 77
 78impl<'a> PartialEq<&'a str> for SharedString {
 79    fn eq(&self, other: &&'a str) -> bool {
 80        self.as_ref() == *other
 81    }
 82}
 83
 84impl From<&SharedString> for SharedString {
 85    fn from(value: &SharedString) -> Self {
 86        value.clone()
 87    }
 88}
 89
 90impl From<SharedString> for Arc<str> {
 91    fn from(val: SharedString) -> Self {
 92        match val.0 {
 93            ArcCow::Borrowed(borrowed) => Arc::from(borrowed),
 94            ArcCow::Owned(owned) => owned.clone(),
 95        }
 96    }
 97}
 98
 99impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
100    fn from(value: T) -> Self {
101        Self(value.into())
102    }
103}
104
105impl From<SharedString> for String {
106    fn from(val: SharedString) -> Self {
107        val.0.to_string()
108    }
109}
110
111impl Serialize for SharedString {
112    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
113    where
114        S: serde::Serializer,
115    {
116        serializer.serialize_str(self.as_ref())
117    }
118}
119
120impl<'de> Deserialize<'de> for SharedString {
121    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
122    where
123        D: serde::Deserializer<'de>,
124    {
125        let s = String::deserialize(deserializer)?;
126        Ok(SharedString::from(s))
127    }
128}