macro_toolset/string_v2/general/
iterator.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Implementations for any `Iterator`.
//!
//! # Concepts
//!
//! Items of the iterator must implement `StringT` and are all considered
//! **independent** (like what we do for tuple, hmmm, tuple with fixed length
//! and with items that are all with the same type?).
//!
//! Since the compiler will complain that *upstream crates may add a new impl
//! of trait [`std::iter::Iterator`] for type `{I}`*, we cannot simply implement
//! `StringT` for `T` where `T`: `{Trait}` but concrete one instead.
//!
//! This crate has already implemented `StringT` for the following types:
//!
//! - [`std::iter::Map`]
//!
//! For array-or-slice-like types:
//!
//! - `Vec<T>`
//! - `[T; N]`
//! - `&[T]` or `&[T; N]`
//!
//!   Only when &T implements `StringT`.
//!   We have implemented `StringT` for most `&T` where T: Copy, though best
//!   effort.

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

wrapper! {
    #[derive(Debug)]
    /// Wrapper for any iterator with items implementing `StringT`.

    pub IterWrapper<I>(pub I)
}

#[macro_export]
/// See [`IterWrapper`].
macro_rules! str_iter_wrapper {
    ($inner:expr) => {
        $crate::string_v2::general::iterator::IterWrapper { inner: $inner }
    };
}

impl<I> StringT for IterWrapper<I>
where
    I: Iterator,
    I::Item: StringT,
{
    #[inline]
    fn encode_to_buf(self, string: &mut Vec<u8>) {
        for item in self.inner {
            item.encode_to_buf(string);
        }
    }

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

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
        for item in self.inner {
            item.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) {
        for item in self.inner {
            item.encode_to_bytes_buf_with_separator(string, separator);
        }
    }
}

impl<I> StringExtT for IterWrapper<I>
where
    I: Iterator,
    I::Item: StringT,
{
    #[inline]
    fn with_prefix<P: StringExtT + Copy>(self, prefix: P) -> impl StringExtT {
        self.inner.map(move |item| super::tuple::SeplessTuple {
            inner: (prefix, item),
        })
    }

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

impl<T, I, F> StringT for std::iter::Map<I, F>
where
    T: StringT,
    I: Iterator,
    F: FnMut(I::Item) -> T,
{
    #[inline]
    fn encode_to_buf(self, string: &mut Vec<u8>) {
        str_iter_wrapper!(self.into_iter()).encode_to_buf(string);
    }

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

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
        str_iter_wrapper!(self.into_iter()).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) {
        str_iter_wrapper!(self.into_iter()).encode_to_bytes_buf_with_separator(string, separator);
    }
}

impl<T, I, F> StringExtT for std::iter::Map<I, F>
where
    T: StringT,
    I: Iterator,
    F: FnMut(I::Item) -> T,
{
    #[inline]
    fn with_prefix<P: StringExtT + Copy>(self, prefix: P) -> impl StringExtT {
        self.into_iter()
            .map(move |item| super::tuple::SeplessTuple {
                inner: (prefix, item),
            })
    }

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

// ==============================================================================================

macro_rules! impl_for_array_or_slice_like {
    (STRING_T: $($tt:tt)*) => {
        $($tt)* {
            #[inline]
            fn encode_to_buf(self, string: &mut Vec<u8>) {
                str_iter_wrapper!(self.into_iter()).encode_to_buf(string);
            }

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

            #[inline]
            #[cfg(feature = "feat-string-ext-bytes")]
            fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
                str_iter_wrapper!(self.into_iter()).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) {
                str_iter_wrapper!(self.into_iter()).encode_to_bytes_buf_with_separator(string, separator);
            }
        }
    };
    (STRING_EXT_T: $($tt:tt)*) => {
        $($tt)* {
            #[inline]
            fn with_prefix<P: StringExtT + Copy>(self, prefix: P) -> impl StringExtT {
                self.into_iter()
                    .map(move |item| super::tuple::SeplessTuple {
                        inner: (prefix, item),
                    })
            }

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

impl_for_array_or_slice_like!(STRING_T: impl<T, const N: usize> StringT for [T; N] where T: StringT);
impl_for_array_or_slice_like!(STRING_EXT_T: impl<T, const N: usize> StringExtT for [T; N] where T: StringT);

impl_for_array_or_slice_like!(STRING_T: impl<T, const N: usize> StringT for &[T; N] where for<'a> &'a T: StringT);
impl_for_array_or_slice_like!(STRING_EXT_T: impl<T, const N: usize> StringExtT for &[T; N] where for<'a> &'a T: StringT);

impl_for_array_or_slice_like!(STRING_T: impl<T> StringT for &[T] where for<'a> &'a T: StringT);
impl_for_array_or_slice_like!(STRING_EXT_T: impl<T> StringExtT for &[T] where for<'a> &'a T: StringT);

impl_for_array_or_slice_like!(STRING_T: impl<T> StringT for Vec<T> where T: StringT);
impl_for_array_or_slice_like!(STRING_EXT_T: impl<T> StringExtT for Vec<T> where T: StringT);