1pub use smallstr::SmallString;
6
7#[macro_export]
8macro_rules! wrap_smallstr {
10 ($name:ty) => {
11 impl From<String> for $name {
12 #[inline]
13 fn from(key: String) -> Self {
14 Self {
15 inner: SmallString::from(key),
16 }
17 }
18 }
19
20 impl<'a> From<&'a String> for $name {
21 #[inline]
22 fn from(key: &'a String) -> Self {
23 Self {
24 inner: SmallString::from(key.as_str()),
25 }
26 }
27 }
28
29 impl From<Box<str>> for $name {
30 #[inline]
31 fn from(key: Box<str>) -> Self {
32 Self {
33 inner: SmallString::from(key),
34 }
35 }
36 }
37
38 impl<'a> From<&'a str> for $name {
39 #[inline]
40 fn from(key: &'a str) -> Self {
41 Self {
42 inner: SmallString::from(key),
43 }
44 }
45 }
46
47 impl Default for $name {
48 fn default() -> Self {
49 Self {
50 inner: SmallString::new(),
51 }
52 }
53 }
54
55 impl fmt::Display for $name {
56 #[inline]
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 fmt::Display::fmt(&self.inner, f)
59 }
60 }
61
62 impl fmt::Debug for $name {
63 #[inline]
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 fmt::Debug::fmt(&self.inner, f)
66 }
67 }
68
69 impl AsRef<str> for $name {
70 #[inline]
71 fn as_ref(&self) -> &str {
72 self.inner.as_ref()
73 }
74 }
75
76 impl AsMut<str> for $name {
77 #[inline]
78 fn as_mut(&mut self) -> &mut str {
79 self.inner.as_mut()
80 }
81 }
82
83 impl Borrow<str> for $name {
84 #[inline]
85 fn borrow(&self) -> &str {
86 self.inner.borrow()
87 }
88 }
89
90 impl BorrowMut<str> for $name {
91 #[inline]
92 fn borrow_mut(&mut self) -> &mut str {
93 self.inner.borrow_mut()
94 }
95 }
96
97 impl AsRef<[u8]> for $name {
98 #[inline]
99 fn as_ref(&self) -> &[u8] {
100 self.inner.as_ref()
101 }
102 }
103
104 impl Deref for $name {
105 type Target = str;
106
107 #[inline]
108 fn deref(&self) -> &Self::Target {
109 self.inner.deref()
110 }
111 }
112
113 impl DerefMut for $name {
114 #[inline]
115 fn deref_mut(&mut self) -> &mut Self::Target {
116 self.inner.deref_mut()
117 }
118 }
119
120 impl Serialize for $name {
121 #[inline]
122 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
123 serializer.serialize_str(&self.inner)
124 }
125 }
126
127 impl<'de> Deserialize<'de> for $name {
128 #[inline]
129 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
130 struct KeyVisitor;
131
132 impl Visitor<'_> for KeyVisitor {
133 type Value = $name;
134
135 #[inline]
136 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
137 f.write_str("a string")
138 }
139
140 #[inline]
141 fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
142 Ok(v.into())
143 }
144
145 #[inline]
146 fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
147 Ok(v.into())
148 }
149 }
150
151 deserializer.deserialize_str(KeyVisitor)
152 }
153 }
154
155 impl Extend<char> for $name {
156 #[inline]
157 fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) {
158 let iter = iter.into_iter();
159 let (lo, _) = iter.size_hint();
160
161 self.reserve(lo);
162
163 for ch in iter {
164 self.push(ch);
165 }
166 }
167 }
168
169 impl<'a> Extend<&'a char> for $name {
170 #[inline]
171 fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) {
172 self.extend(iter.into_iter().cloned());
173 }
174 }
175
176 impl<'a> Extend<Cow<'a, str>> for $name {
177 #[inline]
178 fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) {
179 for s in iter {
180 self.push_str(&s);
181 }
182 }
183 }
184
185 impl<'a> Extend<&'a str> for $name {
186 #[inline]
187 fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) {
188 for s in iter {
189 self.push_str(s);
190 }
191 }
192 }
193
194 impl Extend<String> for $name {
195 #[inline]
196 fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
197 for s in iter {
198 self.push_str(&s);
199 }
200 }
201 }
202
203 impl FromIterator<char> for $name {
204 #[inline]
205 fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Self {
206 let mut s = SmallString::new();
207 s.extend(iter);
208 Self { inner: s }
209 }
210 }
211
212 impl<'a> FromIterator<&'a char> for $name {
213 #[inline]
214 fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> Self {
215 let mut s = SmallString::new();
216 s.extend(iter.into_iter().cloned());
217 Self { inner: s }
218 }
219 }
220
221 impl<'a> FromIterator<Cow<'a, str>> for $name {
222 #[inline]
223 fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> Self {
224 let mut s = SmallString::new();
225 s.extend(iter);
226 Self { inner: s }
227 }
228 }
229
230 impl<'a> FromIterator<&'a str> for $name {
231 #[inline]
232 fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> Self {
233 let mut s = SmallString::new();
234 s.extend(iter);
235 Self { inner: s }
236 }
237 }
238
239 impl FromIterator<String> for $name {
240 #[inline]
241 fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
242 let mut s = SmallString::new();
243 s.extend(iter);
244 Self { inner: s }
245 }
246 }
247
248 impl $name {
249 #[inline]
251 pub fn new() -> Self {
252 Self {
253 inner: SmallString::new(),
254 }
255 }
256
257 #[inline]
261 pub fn with_capacity(n: usize) -> Self {
262 Self {
263 inner: SmallString::with_capacity(n),
264 }
265 }
266
267 #[inline]
269 pub fn len(&self) -> usize {
270 self.inner.len()
271 }
272
273 #[inline]
275 pub fn is_empty(&self) -> bool {
276 self.inner.is_empty()
277 }
278
279 #[inline]
281 pub fn capacity(&self) -> usize {
282 self.inner.capacity()
283 }
284
285 #[inline]
287 pub fn push(&mut self, ch: char) {
288 self.inner.push(ch)
289 }
290
291 #[inline]
293 pub fn push_str(&mut self, s: &str) {
294 self.inner.push_str(s)
295 }
296
297 #[inline]
301 pub fn pop(&mut self) -> Option<char> {
302 self.inner.pop()
303 }
304
305 #[inline]
313 pub fn truncate(&mut self, len: usize) {
314 self.inner.truncate(len)
315 }
316
317 #[inline]
319 pub fn as_str(&self) -> &str {
320 self.inner.as_str()
321 }
322
323 #[inline]
325 pub fn as_mut_str(&mut self) -> &mut str {
326 self.inner.as_mut_str()
327 }
328
329 #[inline]
331 pub fn clear(&mut self) {
332 self.inner.clear()
333 }
334
335 #[inline]
341 pub fn remove(&mut self, idx: usize) -> char {
342 self.inner.remove(idx)
343 }
344
345 #[inline]
351 pub fn insert(&mut self, idx: usize, ch: char) {
352 self.inner.insert(idx, ch)
353 }
354
355 #[inline]
361 pub fn insert_str(&mut self, idx: usize, s: &str) {
362 self.inner.insert_str(idx, s)
363 }
364
365 #[inline]
371 pub fn retain<F: FnMut(char) -> bool>(&mut self, f: F) {
372 self.inner.retain(f)
373 }
374
375 #[inline]
381 pub fn reserve(&mut self, additional: usize) {
382 self.inner.reserve(additional)
383 }
384
385 #[inline]
388 pub fn reserve_exact(&mut self, additional: usize) {
389 self.inner.reserve_exact(additional)
390 }
391
392 #[inline]
397 pub fn shrink_to_fit(&mut self) {
398 self.inner.shrink_to_fit()
399 }
400 }
401
402 impl Index<Range<usize>> for $name {
403 type Output = str;
404
405 #[inline]
406 fn index(&self, index: Range<usize>) -> &str {
407 &self.as_str()[index]
408 }
409 }
410
411 impl IndexMut<Range<usize>> for $name {
412 #[inline]
413 fn index_mut(&mut self, index: Range<usize>) -> &mut str {
414 &mut self.as_mut_str()[index]
415 }
416 }
417
418 impl Index<RangeFrom<usize>> for $name {
419 type Output = str;
420
421 #[inline]
422 fn index(&self, index: RangeFrom<usize>) -> &str {
423 &self.as_str()[index]
424 }
425 }
426
427 impl IndexMut<RangeFrom<usize>> for $name {
428 #[inline]
429 fn index_mut(&mut self, index: RangeFrom<usize>) -> &mut str {
430 &mut self.as_mut_str()[index]
431 }
432 }
433
434 impl Index<RangeTo<usize>> for $name {
435 type Output = str;
436
437 #[inline]
438 fn index(&self, index: RangeTo<usize>) -> &str {
439 &self.as_str()[index]
440 }
441 }
442
443 impl IndexMut<RangeTo<usize>> for $name {
444 #[inline]
445 fn index_mut(&mut self, index: RangeTo<usize>) -> &mut str {
446 &mut self.as_mut_str()[index]
447 }
448 }
449
450 impl Index<RangeFull> for $name {
451 type Output = str;
452
453 #[inline]
454 fn index(&self, index: RangeFull) -> &str {
455 &self.as_str()[index]
456 }
457 }
458
459 impl IndexMut<RangeFull> for $name {
460 #[inline]
461 fn index_mut(&mut self, index: RangeFull) -> &mut str {
462 &mut self.as_mut_str()[index]
463 }
464 }
465
466 impl std::fmt::Write for $name {
467 #[inline]
468 fn write_str(&mut self, s: &str) -> std::fmt::Result {
469 self.inner.write_str(s)
470 }
471
472 #[inline]
473 fn write_char(&mut self, c: char) -> std::fmt::Result {
474 self.inner.write_char(c)
475 }
476
477 #[inline]
478 fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> std::fmt::Result {
479 self.inner.write_fmt(args)
480 }
481 }
482 };
483}