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 Arc<str> {
 85    fn from(val: SharedString) -> Self {
 86        match val.0 {
 87            ArcCow::Borrowed(borrowed) => Arc::from(borrowed),
 88            ArcCow::Owned(owned) => owned.clone(),
 89        }
 90    }
 91}
 92
 93impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
 94    fn from(value: T) -> Self {
 95        Self(value.into())
 96    }
 97}
 98
 99impl From<SharedString> for String {
100    fn from(val: SharedString) -> Self {
101        val.0.to_string()
102    }
103}
104
105impl Serialize for SharedString {
106    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
107    where
108        S: serde::Serializer,
109    {
110        serializer.serialize_str(self.as_ref())
111    }
112}
113
114impl<'de> Deserialize<'de> for SharedString {
115    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
116    where
117        D: serde::Deserializer<'de>,
118    {
119        let s = String::deserialize(deserializer)?;
120        Ok(SharedString::from(s))
121    }
122}