qiniu_utils/
smallstr.rs

1//! smallstr
2//!
3//! Implements SmallString, a String-like container for small strings
4
5pub use smallstr::SmallString;
6
7#[macro_export]
8/// 创建一个新的数据结构,并将其作为 SmallString 的封装类型
9macro_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            /// Construct an empty string.
250            #[inline]
251            pub fn new() -> Self {
252                Self {
253                    inner: SmallString::new(),
254                }
255            }
256
257            /// Construct an empty string with enough capacity pre-allocated to store at least n bytes.
258            ///
259            /// Will create a heap allocation only if n is larger than the inline capacity.
260            #[inline]
261            pub fn with_capacity(n: usize) -> Self {
262                Self {
263                    inner: SmallString::with_capacity(n),
264                }
265            }
266
267            /// Returns the length of this string, in bytes.
268            #[inline]
269            pub fn len(&self) -> usize {
270                self.inner.len()
271            }
272
273            /// Returns `true` if this string is empty.
274            #[inline]
275            pub fn is_empty(&self) -> bool {
276                self.inner.is_empty()
277            }
278
279            /// Returns the number of bytes this string can hold without reallocating.
280            #[inline]
281            pub fn capacity(&self) -> usize {
282                self.inner.capacity()
283            }
284
285            /// Appends the given `char` to the end of this string.
286            #[inline]
287            pub fn push(&mut self, ch: char) {
288                self.inner.push(ch)
289            }
290
291            /// Appends the given string slice to the end of this string.
292            #[inline]
293            pub fn push_str(&mut self, s: &str) {
294                self.inner.push_str(s)
295            }
296
297            /// Removes the last character from this string and returns it.
298            ///
299            /// Returns `None` if the string is empty.
300            #[inline]
301            pub fn pop(&mut self) -> Option<char> {
302                self.inner.pop()
303            }
304
305            /// Shorten the string, keeping the first len bytes.
306            ///
307            /// This does not reallocate. If you want to shrink the string’s capacity, use shrink_to_fit after truncating.
308            ///
309            /// # Panics
310            ///
311            /// If `len` does not lie on a `char` boundary.
312            #[inline]
313            pub fn truncate(&mut self, len: usize) {
314                self.inner.truncate(len)
315            }
316
317            /// Extracts a string slice containing the entire string.
318            #[inline]
319            pub fn as_str(&self) -> &str {
320                self.inner.as_str()
321            }
322
323            /// Extracts a string slice containing the entire string.
324            #[inline]
325            pub fn as_mut_str(&mut self) -> &mut str {
326                self.inner.as_mut_str()
327            }
328
329            /// Removes all contents of the string.
330            #[inline]
331            pub fn clear(&mut self) {
332                self.inner.clear()
333            }
334
335            /// Removes a `char` from this string at a byte position and returns it.
336            ///
337            /// # Panics
338            ///
339            /// If `idx` does not lie on a `char` boundary.
340            #[inline]
341            pub fn remove(&mut self, idx: usize) -> char {
342                self.inner.remove(idx)
343            }
344
345            /// Inserts a `char` into this string at the given byte position.
346            ///
347            /// # Panics
348            ///
349            /// If `idx` does not lie on `char` boundaries.
350            #[inline]
351            pub fn insert(&mut self, idx: usize, ch: char) {
352                self.inner.insert(idx, ch)
353            }
354
355            /// Inserts a `&str` into this string at the given byte position.
356            ///
357            /// # Panics
358            ///
359            /// If `idx` does not lie on `char` boundaries.
360            #[inline]
361            pub fn insert_str(&mut self, idx: usize, s: &str) {
362                self.inner.insert_str(idx, s)
363            }
364
365            /// Retains only the characters specified by the predicate.
366            ///
367            /// In other words, removes all characters `c` such that `f(c)` returns `false`.
368            /// This method operates in place and preserves the order of retained
369            /// characters.
370            #[inline]
371            pub fn retain<F: FnMut(char) -> bool>(&mut self, f: F) {
372                self.inner.retain(f)
373            }
374
375            /// Ensures that this string's capacity is at least `additional` bytes larger
376            /// than its length.
377            ///
378            /// The capacity may be increased by more than `additional` bytes in order to
379            /// prevent frequent reallocations.
380            #[inline]
381            pub fn reserve(&mut self, additional: usize) {
382                self.inner.reserve(additional)
383            }
384
385            /// Ensures that this string's capacity is `additional` bytes larger than
386            /// its length.
387            #[inline]
388            pub fn reserve_exact(&mut self, additional: usize) {
389                self.inner.reserve_exact(additional)
390            }
391
392            /// Shrink the capacity of the string as much as possible.
393            ///
394            /// When possible, this will move the data from an external heap buffer
395            /// to the string's inline storage.
396            #[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}