1use std::{
2 fmt::{Debug, Display},
3 ops::Deref,
4 sync::Arc,
5};
6
7use serde::{Deserialize, Serialize};
8
9use crate::RcStr;
10
11#[derive(Serialize, Deserialize)]
20pub struct ArcStr(Arc<str>);
21
22impl From<String> for ArcStr {
23 fn from(s: String) -> Self {
24 ArcStr(Arc::from(s))
25 }
26}
27
28impl From<&str> for ArcStr {
29 fn from(s: &str) -> Self {
30 ArcStr(Arc::from(s.to_string()))
31 }
32}
33
34impl Deref for ArcStr {
35 type Target = str;
36
37 fn deref(&self) -> &Self::Target {
38 &self.0
39 }
40}
41
42impl Clone for ArcStr {
43 fn clone(&self) -> Self {
44 ArcStr(self.0.clone())
45 }
46}
47
48impl Debug for ArcStr {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 write!(f, "{}", self.0)
51 }
52}
53
54impl Display for ArcStr {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 write!(f, "{}", self.0)
57 }
58}
59
60impl AsRef<str> for ArcStr {
61 fn as_ref(&self) -> &str {
62 &self.0
63 }
64}
65
66impl PartialEq for ArcStr {
67 fn eq(&self, other: &Self) -> bool {
68 self.0.eq(&other.0)
69 }
70}
71
72impl PartialEq<str> for ArcStr {
73 fn eq(&self, other: &str) -> bool {
74 self.0.as_ref().eq(other)
75 }
76}
77
78impl PartialEq<&str> for ArcStr {
79 fn eq(&self, other: &&str) -> bool {
80 self.0.as_ref().eq(*other)
81 }
82}
83
84impl From<ArcStr> for String {
85 fn from(val: ArcStr) -> Self {
86 val.0.to_string()
87 }
88}
89
90impl Default for ArcStr {
91 fn default() -> Self {
92 ArcStr(Arc::from(""))
93 }
94}
95
96impl PartialOrd for ArcStr {
97 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
98 Some(self.cmp(other))
99 }
100}
101
102impl Ord for ArcStr {
103 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
104 self.0.as_ref().cmp(other.0.as_ref())
105 }
106}
107
108impl Eq for ArcStr {}
109
110impl From<RcStr> for ArcStr {
111 fn from(rc: RcStr) -> Self {
112 ArcStr(Arc::from(rc.as_ref().to_string()))
113 }
114}