macro_toolset/string_v2/
general.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! All implementations of [`StringExtT`] are here

use std::{rc::Rc, sync::Arc};

use super::{StringExtT, StringT};
use crate::impl_for_shared_ref;

pub mod iterator;
pub mod string;
pub mod tuple;

impl StringT for () {
    #[inline]
    fn encode_to_buf(self, _string: &mut Vec<u8>) {}

    #[inline]
    fn encode_to_buf_with_separator(self, _string: &mut Vec<u8>, _separator: &str) {}

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf(self, _string: &mut bytes::BytesMut) {}

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf_with_separator(self, _string: &mut bytes::BytesMut, _separator: &str) {}
}

impl StringExtT for () {}

impl<T: StringT> StringT for Box<T> {
    #[inline]
    fn encode_to_buf(self, string: &mut Vec<u8>) {
        (*self).encode_to_buf(string);
    }

    #[inline]
    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
        (*self).encode_to_buf_with_separator(string, separator);
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
        (*self).encode_to_bytes_buf(string);
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
        (*self).encode_to_bytes_buf_with_separator(string, separator);
    }
}

impl<T: StringExtT> StringExtT for Box<T> {}

impl<T: StringT> StringT for Option<T> {
    #[inline]
    fn encode_to_buf(self, string: &mut Vec<u8>) {
        if let Some(inner) = self {
            inner.encode_to_buf(string);
        }
    }

    #[inline]
    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
        if let Some(inner) = self {
            inner.encode_to_buf_with_separator(string, separator);
        }
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
        if let Some(inner) = self {
            inner.encode_to_bytes_buf(string);
        }
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
        if let Some(inner) = self {
            inner.encode_to_bytes_buf_with_separator(string, separator);
        }
    }
}

impl<T: StringExtT> StringExtT for Option<T> {
    #[inline]
    /// With prefix.
    fn with_prefix<P: StringExtT + Copy>(self, prefix: P) -> impl StringExtT {
        self.map(|inner| tuple::SeplessTuple {
            inner: (prefix, inner),
        })
    }

    #[inline]
    /// With suffix.
    fn with_suffix<S: StringExtT + Copy>(self, suffix: S) -> impl StringExtT {
        self.map(|inner| tuple::SeplessTuple {
            inner: (inner, suffix),
        })
    }
}

impl<T: StringT, E> StringT for Result<T, E> {
    #[inline]
    fn encode_to_buf(self, string: &mut Vec<u8>) {
        if let Ok(inner) = self {
            inner.encode_to_buf(string);
        }
    }

    #[inline]
    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
        if let Ok(inner) = self {
            inner.encode_to_buf_with_separator(string, separator);
        }
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
        if let Ok(inner) = self {
            inner.encode_to_bytes_buf(string);
        }
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
        if let Ok(inner) = self {
            inner.encode_to_bytes_buf_with_separator(string, separator);
        }
    }
}

impl<T: StringExtT, E> StringExtT for Result<T, E> {
    #[inline]
    /// With prefix.
    fn with_prefix<P: StringExtT + Copy>(self, prefix: P) -> impl StringExtT {
        self.map(|inner| tuple::SeplessTuple {
            inner: (prefix, inner),
        })
    }

    #[inline]
    /// With suffix.
    fn with_suffix<S: StringExtT + Copy>(self, suffix: S) -> impl StringExtT {
        self.map(|inner| tuple::SeplessTuple {
            inner: (inner, suffix),
        })
    }
}

impl_for_shared_ref!(REF: T => Arc<T> T => Rc<T>);