1use core::{
2 convert::Infallible,
3 fmt::Display,
4 hash::Hash,
5 mem::take,
6 ops::{Add, AddAssign, Deref, Index},
7 slice::SliceIndex,
8 str::FromStr,
9};
10
11use alloc::string::{String, ToString};
12
13use crate::Str;
14
15impl Hash for Str {
16 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
17 self.deref().hash(state);
18 }
19}
20
21impl PartialEq for Str {
22 fn eq(&self, other: &Self) -> bool {
23 self.deref().eq(&**other)
24 }
25}
26
27impl PartialEq<&str> for Str {
28 fn eq(&self, other: &&str) -> bool {
29 self.deref().eq(*other)
30 }
31}
32
33impl PartialEq<str> for Str {
34 fn eq(&self, other: &str) -> bool {
35 self.deref().eq(other)
36 }
37}
38
39impl PartialEq<String> for Str {
40 fn eq(&self, other: &String) -> bool {
41 self.deref().eq(other)
42 }
43}
44
45impl PartialEq<Str> for &str {
46 fn eq(&self, other: &Str) -> bool {
47 (*self).eq(&**other)
48 }
49}
50
51impl PartialEq<Str> for str {
52 fn eq(&self, other: &Str) -> bool {
53 self.eq(&**other)
54 }
55}
56
57impl PartialEq<Str> for String {
58 fn eq(&self, other: &Str) -> bool {
59 self.eq(&**other)
60 }
61}
62
63impl<I: SliceIndex<str>> Index<I> for Str {
64 type Output = <I as SliceIndex<str>>::Output;
65 fn index(&self, index: I) -> &Self::Output {
66 self.deref().index(index)
67 }
68}
69
70impl FromStr for Str {
71 type Err = Infallible;
72 fn from_str(s: &str) -> Result<Self, Self::Err> {
73 Ok(Self::from(s.to_string()))
74 }
75}
76
77impl<S: AsRef<str>> FromIterator<S> for Str {
78 fn from_iter<T: IntoIterator<Item = S>>(iter: T) -> Self {
79 iter.into_iter()
80 .fold(Self::new(), |state, s| state + s.as_ref())
81 }
82}
83
84impl Eq for Str {}
85
86impl PartialOrd for Str {
87 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
88 Some(self.cmp(other))
89 }
90}
91
92impl Ord for Str {
93 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
94 self.deref().cmp(&**other)
95 }
96}
97
98impl Display for Str {
99 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
100 self.deref().fmt(f)
101 }
102}
103
104impl<'a> Extend<&'a str> for Str {
105 fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) {
106 self.handle(move |string| {
107 string.extend(iter);
108 });
109 }
110}
111
112impl Extend<String> for Str {
113 fn extend<T: IntoIterator<Item = String>>(&mut self, iter: T) {
114 self.handle(move |string| {
115 string.extend(iter);
116 });
117 }
118}
119
120impl Extend<Self> for Str {
121 fn extend<T: IntoIterator<Item = Self>>(&mut self, iter: T) {
122 self.handle(move |string| {
123 for s in iter {
124 string.push_str(&s);
125 }
126 });
127 }
128}
129
130impl<T> Add<T> for &Str
131where
132 T: AsRef<str>,
133{
134 type Output = Str;
135 fn add(self, rhs: T) -> Self::Output {
136 self.clone().add(rhs)
137 }
138}
139
140impl<T> Add<T> for Str
141where
142 T: AsRef<str>,
143{
144 type Output = Self;
145 fn add(self, rhs: T) -> Self::Output {
146 let rhs = rhs.as_ref();
147 (self.into_string() + rhs).into()
148 }
149}
150
151impl<T> AddAssign<T> for Str
152where
153 T: AsRef<str>,
154{
155 fn add_assign(&mut self, rhs: T) {
156 let rhs = rhs.as_ref();
157
158 let string = take(self).into_string();
159 *self = (string + rhs).into();
160 }
161}
162
163#[cfg(feature = "serde")]
164mod serde {
165 use core::ops::Deref;
166
167 use super::Str;
168 use alloc::string::{String, ToString};
169 use serde::{Deserialize, Deserializer, Serialize, de::Visitor};
170 struct StrVisitor;
171
172 impl Serialize for Str {
173 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
174 where
175 S: serde::Serializer,
176 {
177 self.deref().serialize(serializer)
178 }
179 }
180
181 impl Visitor<'_> for StrVisitor {
182 type Value = Str;
183
184 fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
185 formatter.write_str("a string")
186 }
187
188 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> {
189 Ok(v.to_string().into())
190 }
191
192 fn visit_string<E>(self, v: String) -> Result<Self::Value, E> {
193 Ok(v.into())
194 }
195 }
196
197 impl<'de> Deserialize<'de> for Str {
198 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
199 where
200 D: Deserializer<'de>,
201 {
202 deserializer.deserialize_string(StrVisitor)
203 }
204 }
205}