macro_toolset/string/general/
string.rs1use std::{borrow::Cow, ops::Deref, rc::Rc, sync::Arc};
20
21use super::{StringExtT, StringT};
22use crate::{impl_for_shared_ref, impl_for_wrapper, wrapper};
23
24macro_rules! impl_for_string {
25 ($($ty:ty),*) => {
26 $(
27 impl_for_string!(@INTERNAL $ty);
29 impl_for_string!(@INTERNAL &$ty);
30 impl_for_string!(@INTERNAL &mut $ty);
31 impl_for_string!(@INTERNAL &&$ty);
32 impl_for_string!(@INTERNAL &&mut $ty);
33 impl_for_string!(@INTERNAL &mut &$ty);
34 impl_for_string!(@INTERNAL &mut &mut $ty);
35 impl_for_string!(@INTERNAL &&&$ty);
36 )*
37 };
38
39 (@INTERNAL $ty:ty) => {
40 impl StringT for $ty {
41 #[inline]
42 fn encode_to_buf(self, string: &mut Vec<u8>) {
43 string.extend(self.as_bytes());
44 }
45
46 #[inline]
47 fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
48 string.extend(self.as_bytes());
49 string.extend(separator.as_bytes());
50 }
51
52 #[inline]
53 fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
54 string.extend(self.as_bytes());
55 }
56
57 #[inline]
58 fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
59 string.extend(self.as_bytes());
60 string.extend(separator.as_bytes());
61 }
62 }
63
64 impl StringExtT for $ty {}
65 }
66}
67
68impl_for_string! {
69 &str,
70 Arc<str>,
71 Rc<str>,
72 Box<str>,
73 Cow<'_, str>,
74 String
75}
76
77impl StringT for char {
78 #[inline]
79 fn encode_to_buf(self, string: &mut Vec<u8>) {
80 match self.len_utf8() {
82 1 => string.push(self as u8),
83 _ => string.extend(self.encode_utf8(&mut [0; 4]).as_bytes()),
84 }
85 }
86
87 #[inline]
88 fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
89 self.encode_to_buf(string);
90 string.extend(separator.as_bytes());
91 }
92
93 #[inline]
94 fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
95 match self.len_utf8() {
97 1 => string.extend([self as u8]),
98 _ => string.extend(self.encode_utf8(&mut [0; 4]).as_bytes()),
99 }
100 }
101
102 fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
103 self.encode_to_bytes_buf(string);
104 string.extend(separator.as_bytes());
105 }
106}
107
108impl StringExtT for char {}
109
110impl_for_shared_ref!(COPIED: char);
111
112#[macro_export]
113macro_rules! str_wrapper {
131 (str = $data:expr) => {
132 $crate::string::general::string::StrWrapper { inner: $data }
133 };
134 (string = $data:expr) => {
135 $crate::string::general::string::StringWrapper { inner: $data }
136 };
137}
138
139wrapper! {
140 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
141 pub StrWrapper<T>(pub T)
148}
149
150wrapper! {
151 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
152 pub StringWrapper<T>(pub T)
159}
160
161impl_for_wrapper!(STRING_T: impl<T> StringT for StrWrapper<T> where T: Deref<Target = str>);
162impl_for_wrapper!(STRING_T: impl<T> StringT for StringWrapper<T> where T: Deref<Target = String>);
163impl_for_wrapper!(STRING_EXT_T: impl<T> StringExtT for StrWrapper<T> where T: Deref<Target = str>);
164impl_for_wrapper!(STRING_EXT_T: impl<T> StringExtT for StringWrapper<T> where T: Deref<Target = String>);