1use core::{iter::Map, str};
4
5use non_empty_iter::NonEmptyIterator;
6
7use crate::{internal::Byte, str::NonEmptyStr};
8
9pub type NonEmptyStrFn<'s> = fn(&'s str) -> &'s NonEmptyStr;
14
15#[derive(Debug)]
21pub struct Bytes<'s> {
22 string: &'s NonEmptyStr,
23}
24
25impl<'s> Bytes<'s> {
26 #[must_use]
28 pub const fn new(string: &'s NonEmptyStr) -> Self {
29 Self { string }
30 }
31}
32
33impl<'s> IntoIterator for Bytes<'s> {
34 type Item = Byte;
35 type IntoIter = str::Bytes<'s>;
36
37 fn into_iter(self) -> Self::IntoIter {
38 self.string.as_str().bytes()
39 }
40}
41
42unsafe impl NonEmptyIterator for Bytes<'_> {}
43
44#[derive(Debug)]
50pub struct Chars<'s> {
51 string: &'s NonEmptyStr,
52}
53
54impl<'s> Chars<'s> {
55 #[must_use]
57 pub const fn new(string: &'s NonEmptyStr) -> Self {
58 Self { string }
59 }
60}
61
62impl<'s> IntoIterator for Chars<'s> {
63 type Item = char;
64 type IntoIter = str::Chars<'s>;
65
66 fn into_iter(self) -> Self::IntoIter {
67 self.string.as_str().chars()
68 }
69}
70
71unsafe impl NonEmptyIterator for Chars<'_> {}
72
73#[derive(Debug)]
79pub struct CharIndices<'s> {
80 string: &'s NonEmptyStr,
81}
82
83impl<'s> CharIndices<'s> {
84 #[must_use]
86 pub const fn new(string: &'s NonEmptyStr) -> Self {
87 Self { string }
88 }
89}
90
91impl<'s> IntoIterator for CharIndices<'s> {
92 type Item = (usize, char);
93 type IntoIter = str::CharIndices<'s>;
94
95 fn into_iter(self) -> Self::IntoIter {
96 self.string.as_str().char_indices()
97 }
98}
99
100unsafe impl NonEmptyIterator for CharIndices<'_> {}
101
102#[derive(Debug)]
111pub struct SplitWhitespace<'s> {
112 string: &'s NonEmptyStr,
113}
114
115impl<'s> SplitWhitespace<'s> {
116 #[must_use]
118 pub const fn new(string: &'s NonEmptyStr) -> Self {
119 Self { string }
120 }
121}
122
123impl<'s> IntoIterator for SplitWhitespace<'s> {
124 type Item = &'s NonEmptyStr;
125 type IntoIter = Map<str::SplitWhitespace<'s>, NonEmptyStrFn<'s>>;
126
127 fn into_iter(self) -> Self::IntoIter {
128 self.string
129 .as_str()
130 .split_whitespace()
131 .map(|string| unsafe { NonEmptyStr::from_str_unchecked(string) })
133 }
134}
135
136#[derive(Debug)]
148pub struct SplitAsciiWhitespace<'s> {
149 string: &'s NonEmptyStr,
150}
151
152impl<'s> SplitAsciiWhitespace<'s> {
153 #[must_use]
155 pub const fn new(string: &'s NonEmptyStr) -> Self {
156 Self { string }
157 }
158}
159
160impl<'s> IntoIterator for SplitAsciiWhitespace<'s> {
161 type Item = &'s NonEmptyStr;
162 type IntoIter = Map<str::SplitAsciiWhitespace<'s>, NonEmptyStrFn<'s>>;
163
164 fn into_iter(self) -> Self::IntoIter {
165 self.string
166 .as_str()
167 .split_ascii_whitespace()
168 .map(|string| unsafe { NonEmptyStr::from_str_unchecked(string) })
170 }
171}
172
173#[derive(Debug)]
182pub struct EncodeUtf16<'s> {
183 string: &'s NonEmptyStr,
184}
185
186impl<'s> EncodeUtf16<'s> {
187 #[must_use]
189 pub const fn new(string: &'s NonEmptyStr) -> Self {
190 Self { string }
191 }
192}
193
194impl<'s> IntoIterator for EncodeUtf16<'s> {
195 type Item = u16;
196 type IntoIter = str::EncodeUtf16<'s>;
197
198 fn into_iter(self) -> Self::IntoIter {
199 self.string.as_str().encode_utf16()
200 }
201}
202
203unsafe impl NonEmptyIterator for EncodeUtf16<'_> {}
204
205#[derive(Debug)]
211pub struct EscapeDebug<'s> {
212 string: &'s NonEmptyStr,
213}
214
215impl<'s> EscapeDebug<'s> {
216 #[must_use]
218 pub const fn new(string: &'s NonEmptyStr) -> Self {
219 Self { string }
220 }
221}
222
223impl<'s> IntoIterator for EscapeDebug<'s> {
224 type Item = char;
225 type IntoIter = str::EscapeDebug<'s>;
226
227 fn into_iter(self) -> Self::IntoIter {
228 self.string.as_str().escape_debug()
229 }
230}
231
232unsafe impl NonEmptyIterator for EscapeDebug<'_> {}
233
234#[derive(Debug)]
240pub struct EscapeDefault<'s> {
241 string: &'s NonEmptyStr,
242}
243
244impl<'s> EscapeDefault<'s> {
245 #[must_use]
247 pub const fn new(string: &'s NonEmptyStr) -> Self {
248 Self { string }
249 }
250}
251
252impl<'s> IntoIterator for EscapeDefault<'s> {
253 type Item = char;
254 type IntoIter = str::EscapeDefault<'s>;
255
256 fn into_iter(self) -> Self::IntoIter {
257 self.string.as_str().escape_default()
258 }
259}
260
261unsafe impl NonEmptyIterator for EscapeDefault<'_> {}
262
263#[derive(Debug)]
269pub struct EscapeUnicode<'s> {
270 string: &'s NonEmptyStr,
271}
272
273impl<'s> EscapeUnicode<'s> {
274 #[must_use]
276 pub const fn new(string: &'s NonEmptyStr) -> Self {
277 Self { string }
278 }
279}
280
281impl<'s> IntoIterator for EscapeUnicode<'s> {
282 type Item = char;
283 type IntoIter = str::EscapeUnicode<'s>;
284
285 fn into_iter(self) -> Self::IntoIter {
286 self.string.as_str().escape_unicode()
287 }
288}
289
290unsafe impl NonEmptyIterator for EscapeUnicode<'_> {}
291
292#[derive(Debug)]
298pub struct Lines<'s> {
299 string: &'s NonEmptyStr,
300}
301
302impl<'s> Lines<'s> {
303 #[must_use]
305 pub const fn new(string: &'s NonEmptyStr) -> Self {
306 Self { string }
307 }
308}
309
310impl<'s> IntoIterator for Lines<'s> {
311 type Item = &'s str;
312
313 type IntoIter = str::Lines<'s>;
314
315 fn into_iter(self) -> Self::IntoIter {
316 self.string.as_str().lines()
317 }
318}
319
320unsafe impl NonEmptyIterator for Lines<'_> {}