macro_toolset/string/
general.rs

1//! All implementations of [`StringExtT`] are here
2
3use std::{rc::Rc, sync::Arc};
4
5use super::{StringExtT, StringT};
6use crate::impl_for_shared_ref;
7
8pub mod iterator;
9pub mod string;
10pub mod tuple;
11
12impl StringT for () {
13    #[inline]
14    fn encode_to_buf(self, _string: &mut Vec<u8>) {}
15
16    #[inline]
17    fn encode_to_buf_with_separator(self, _string: &mut Vec<u8>, _separator: &str) {}
18
19    #[inline]
20    fn encode_to_bytes_buf(self, _string: &mut bytes::BytesMut) {}
21
22    #[inline]
23    fn encode_to_bytes_buf_with_separator(self, _string: &mut bytes::BytesMut, _separator: &str) {}
24}
25
26impl StringExtT for () {}
27
28impl<T: StringT> StringT for Box<T> {
29    #[inline]
30    fn encode_to_buf(self, string: &mut Vec<u8>) {
31        (*self).encode_to_buf(string);
32    }
33
34    #[inline]
35    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
36        (*self).encode_to_buf_with_separator(string, separator);
37    }
38
39    #[inline]
40    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
41        (*self).encode_to_bytes_buf(string);
42    }
43
44    #[inline]
45    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
46        (*self).encode_to_bytes_buf_with_separator(string, separator);
47    }
48}
49
50impl<T: StringExtT> StringExtT for Box<T> {}
51
52impl<T: StringT> StringT for Option<T> {
53    #[inline]
54    fn encode_to_buf(self, string: &mut Vec<u8>) {
55        if let Some(inner) = self {
56            inner.encode_to_buf(string);
57        }
58    }
59
60    #[inline]
61    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
62        if let Some(inner) = self {
63            inner.encode_to_buf_with_separator(string, separator);
64        }
65    }
66
67    #[inline]
68    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
69        if let Some(inner) = self {
70            inner.encode_to_bytes_buf(string);
71        }
72    }
73
74    #[inline]
75    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
76        if let Some(inner) = self {
77            inner.encode_to_bytes_buf_with_separator(string, separator);
78        }
79    }
80}
81
82impl<T: StringExtT> StringExtT for Option<T> {
83    #[inline]
84    /// With prefix.
85    fn with_prefix<P: StringExtT + Copy>(self, prefix: P) -> impl StringExtT {
86        self.map(|inner| tuple::SeplessTuple {
87            inner: (prefix, inner),
88        })
89    }
90
91    #[inline]
92    /// With suffix.
93    fn with_suffix<S: StringExtT + Copy>(self, suffix: S) -> impl StringExtT {
94        self.map(|inner| tuple::SeplessTuple {
95            inner: (inner, suffix),
96        })
97    }
98}
99
100impl<T: StringT, E> StringT for Result<T, E> {
101    #[inline]
102    fn encode_to_buf(self, string: &mut Vec<u8>) {
103        if let Ok(inner) = self {
104            inner.encode_to_buf(string);
105        }
106    }
107
108    #[inline]
109    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
110        if let Ok(inner) = self {
111            inner.encode_to_buf_with_separator(string, separator);
112        }
113    }
114
115    #[inline]
116    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
117        if let Ok(inner) = self {
118            inner.encode_to_bytes_buf(string);
119        }
120    }
121
122    #[inline]
123    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
124        if let Ok(inner) = self {
125            inner.encode_to_bytes_buf_with_separator(string, separator);
126        }
127    }
128}
129
130impl<T: StringExtT, E> StringExtT for Result<T, E> {
131    #[inline]
132    /// With prefix.
133    fn with_prefix<P: StringExtT + Copy>(self, prefix: P) -> impl StringExtT {
134        self.map(|inner| tuple::SeplessTuple {
135            inner: (prefix, inner),
136        })
137    }
138
139    #[inline]
140    /// With suffix.
141    fn with_suffix<S: StringExtT + Copy>(self, suffix: S) -> impl StringExtT {
142        self.map(|inner| tuple::SeplessTuple {
143            inner: (inner, suffix),
144        })
145    }
146}
147
148impl_for_shared_ref!(REF: T => Arc<T> T => Rc<T>);