use derive_more::{Deref, DerefMut}; use serde::{Deserialize, Serialize}; use std::{borrow::Borrow, sync::Arc}; use util::arc_cow::ArcCow; /// A shared string is an immutable string that can be cheaply cloned in GPUI /// tasks. Essentially an abstraction over an `Arc` and `&'static str`, #[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)] pub struct SharedString(ArcCow<'static, str>); impl Default for SharedString { fn default() -> Self { Self(ArcCow::Owned("".into())) } } impl AsRef for SharedString { fn as_ref(&self) -> &str { &self.0 } } impl Borrow for SharedString { fn borrow(&self) -> &str { self.as_ref() } } impl std::fmt::Debug for SharedString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } impl std::fmt::Display for SharedString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0.as_ref()) } } impl PartialEq for SharedString { fn eq(&self, other: &String) -> bool { self.as_ref() == other } } impl PartialEq for String { fn eq(&self, other: &SharedString) -> bool { self == other.as_ref() } } impl PartialEq for SharedString { fn eq(&self, other: &str) -> bool { self.as_ref() == other } } impl<'a> PartialEq<&'a str> for SharedString { fn eq(&self, other: &&'a str) -> bool { self.as_ref() == *other } } impl From for Arc { fn from(val: SharedString) -> Self { match val.0 { ArcCow::Borrowed(borrowed) => Arc::from(borrowed), ArcCow::Owned(owned) => owned.clone(), } } } impl>> From for SharedString { fn from(value: T) -> Self { Self(value.into()) } } impl From for String { fn from(val: SharedString) -> Self { val.0.to_string() } } impl Serialize for SharedString { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { serializer.serialize_str(self.as_ref()) } } impl<'de> Deserialize<'de> for SharedString { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, { let s = String::deserialize(deserializer)?; Ok(SharedString::from(s)) } }