1#[cfg(feature = "std")]
4use std::vec::IntoIter;
5
6#[cfg(all(not(feature = "std"), feature = "alloc"))]
7use alloc::vec::IntoIter;
8
9use core::{
10 fmt,
11 iter::Map,
12 slice::{self, Iter, IterMut},
13};
14
15use non_zero_size::Size;
16
17use non_empty_iter::{NonEmptyAdapter, NonEmptyIterator};
18
19use crate::slice::{NonEmptyBytes, NonEmptySlice};
20
21#[cfg(any(feature = "std", feature = "alloc"))]
23pub type IntoNonEmptyIter<T> = NonEmptyAdapter<IntoIter<T>>;
24
25pub type NonEmptyIter<'a, T> = NonEmptyAdapter<Iter<'a, T>>;
27
28pub type NonEmptyIterMut<'a, T> = NonEmptyAdapter<IterMut<'a, T>>;
30
31pub type NonEmptySliceFn<'a, T> = fn(&'a [T]) -> &'a NonEmptySlice<T>;
36
37pub type NonEmptyMutSliceFn<'a, T> = fn(&'a mut [T]) -> &'a mut NonEmptySlice<T>;
42
43#[derive(Debug)]
50pub struct Chunks<'a, T> {
51 slice: &'a NonEmptySlice<T>,
52 size: Size,
53}
54
55impl<'a, T> Chunks<'a, T> {
56 pub const fn new(slice: &'a NonEmptySlice<T>, size: Size) -> Self {
58 Self { slice, size }
59 }
60}
61
62impl<'a, T> IntoIterator for Chunks<'a, T> {
63 type Item = &'a NonEmptySlice<T>;
64
65 type IntoIter = Map<slice::Chunks<'a, T>, NonEmptySliceFn<'a, T>>;
66
67 fn into_iter(self) -> Self::IntoIter {
68 self.slice
69 .as_slice()
70 .chunks(self.size.get())
71 .map(|chunk| unsafe { NonEmptySlice::from_slice_unchecked(chunk) })
73 }
74}
75
76unsafe impl<T> NonEmptyIterator for Chunks<'_, T> {}
77
78#[derive(Debug)]
85pub struct ChunksMut<'a, T> {
86 slice: &'a mut NonEmptySlice<T>,
87 size: Size,
88}
89
90impl<'a, T> ChunksMut<'a, T> {
91 pub const fn new(slice: &'a mut NonEmptySlice<T>, size: Size) -> Self {
93 Self { slice, size }
94 }
95}
96
97impl<'a, T> IntoIterator for ChunksMut<'a, T> {
98 type Item = &'a mut NonEmptySlice<T>;
99
100 type IntoIter = Map<slice::ChunksMut<'a, T>, NonEmptyMutSliceFn<'a, T>>;
101
102 fn into_iter(self) -> Self::IntoIter {
103 self.slice
104 .as_mut_slice()
105 .chunks_mut(self.size.get())
106 .map(|chunk| unsafe { NonEmptySlice::from_mut_slice_unchecked(chunk) })
108 }
109}
110
111unsafe impl<T> NonEmptyIterator for ChunksMut<'_, T> {}
112
113#[derive(Debug)]
120pub struct RChunks<'a, T> {
121 slice: &'a NonEmptySlice<T>,
122 size: Size,
123}
124
125impl<'a, T> RChunks<'a, T> {
126 pub const fn new(slice: &'a NonEmptySlice<T>, size: Size) -> Self {
128 Self { slice, size }
129 }
130}
131
132unsafe impl<T> NonEmptyIterator for RChunks<'_, T> {}
133
134impl<'a, T> IntoIterator for RChunks<'a, T> {
135 type Item = &'a NonEmptySlice<T>;
136
137 type IntoIter = Map<slice::RChunks<'a, T>, NonEmptySliceFn<'a, T>>;
138
139 fn into_iter(self) -> Self::IntoIter {
140 self.slice
141 .as_slice()
142 .rchunks(self.size.get())
143 .map(|chunk| unsafe { NonEmptySlice::from_slice_unchecked(chunk) })
145 }
146}
147
148#[derive(Debug)]
155pub struct RChunksMut<'a, T> {
156 slice: &'a mut NonEmptySlice<T>,
157 size: Size,
158}
159
160impl<'a, T> RChunksMut<'a, T> {
161 pub const fn new(slice: &'a mut NonEmptySlice<T>, size: Size) -> Self {
163 Self { slice, size }
164 }
165}
166
167impl<'a, T> IntoIterator for RChunksMut<'a, T> {
168 type Item = &'a mut NonEmptySlice<T>;
169
170 type IntoIter = Map<slice::RChunksMut<'a, T>, NonEmptyMutSliceFn<'a, T>>;
171
172 fn into_iter(self) -> Self::IntoIter {
173 self.slice
174 .as_mut_slice()
175 .rchunks_mut(self.size.get())
176 .map(|chunk| unsafe { NonEmptySlice::from_mut_slice_unchecked(chunk) })
178 }
179}
180
181unsafe impl<T> NonEmptyIterator for RChunksMut<'_, T> {}
182
183#[derive(Debug)]
193pub struct ChunksExact<'a, T> {
194 slice: &'a NonEmptySlice<T>,
195 size: Size,
196}
197
198impl<'a, T> ChunksExact<'a, T> {
199 pub const fn new(slice: &'a NonEmptySlice<T>, size: Size) -> Self {
201 Self { slice, size }
202 }
203}
204
205impl<'a, T> IntoIterator for ChunksExact<'a, T> {
206 type Item = &'a NonEmptySlice<T>;
207
208 type IntoIter = Map<slice::ChunksExact<'a, T>, NonEmptySliceFn<'a, T>>;
209
210 fn into_iter(self) -> Self::IntoIter {
211 self.slice
212 .as_slice()
213 .chunks_exact(self.size.get())
214 .map(|chunk| unsafe { NonEmptySlice::from_slice_unchecked(chunk) })
216 }
217}
218
219unsafe impl<T> NonEmptyIterator for ChunksExact<'_, T> {}
220
221#[derive(Debug)]
231pub struct ChunksExactMut<'a, T> {
232 slice: &'a mut NonEmptySlice<T>,
233 size: Size,
234}
235
236impl<'a, T> ChunksExactMut<'a, T> {
237 pub const fn new(slice: &'a mut NonEmptySlice<T>, size: Size) -> Self {
239 Self { slice, size }
240 }
241}
242
243impl<'a, T> IntoIterator for ChunksExactMut<'a, T> {
244 type Item = &'a mut NonEmptySlice<T>;
245
246 type IntoIter = Map<slice::ChunksExactMut<'a, T>, NonEmptyMutSliceFn<'a, T>>;
247
248 fn into_iter(self) -> Self::IntoIter {
249 self.slice
250 .as_mut_slice()
251 .chunks_exact_mut(self.size.get())
252 .map(|chunk| unsafe { NonEmptySlice::from_mut_slice_unchecked(chunk) })
254 }
255}
256
257unsafe impl<T> NonEmptyIterator for ChunksExactMut<'_, T> {}
258
259#[derive(Debug)]
269pub struct RChunksExact<'a, T> {
270 slice: &'a NonEmptySlice<T>,
271 size: Size,
272}
273
274impl<'a, T> RChunksExact<'a, T> {
275 pub const fn new(slice: &'a NonEmptySlice<T>, size: Size) -> Self {
277 Self { slice, size }
278 }
279}
280
281impl<'a, T> IntoIterator for RChunksExact<'a, T> {
282 type Item = &'a NonEmptySlice<T>;
283
284 type IntoIter = Map<slice::RChunksExact<'a, T>, NonEmptySliceFn<'a, T>>;
285
286 fn into_iter(self) -> Self::IntoIter {
287 self.slice
288 .as_slice()
289 .rchunks_exact(self.size.get())
290 .map(|chunk| unsafe { NonEmptySlice::from_slice_unchecked(chunk) })
292 }
293}
294
295unsafe impl<T> NonEmptyIterator for RChunksExact<'_, T> {}
296
297#[derive(Debug)]
307pub struct RChunksExactMut<'a, T> {
308 slice: &'a mut NonEmptySlice<T>,
309 size: Size,
310}
311
312impl<'a, T> RChunksExactMut<'a, T> {
313 pub const fn new(slice: &'a mut NonEmptySlice<T>, size: Size) -> Self {
315 Self { slice, size }
316 }
317}
318
319impl<'a, T> IntoIterator for RChunksExactMut<'a, T> {
320 type Item = &'a mut NonEmptySlice<T>;
321
322 type IntoIter = Map<slice::RChunksExactMut<'a, T>, NonEmptyMutSliceFn<'a, T>>;
323
324 fn into_iter(self) -> Self::IntoIter {
325 self.slice
326 .as_mut_slice()
327 .rchunks_exact_mut(self.size.get())
328 .map(|chunk| unsafe { NonEmptySlice::from_mut_slice_unchecked(chunk) })
330 }
331}
332
333unsafe impl<T> NonEmptyIterator for RChunksExactMut<'_, T> {}
334
335#[derive(Debug)]
341pub struct Windows<'a, T> {
342 slice: &'a NonEmptySlice<T>,
343 size: Size,
344}
345
346impl<'a, T> Windows<'a, T> {
347 pub const fn new(slice: &'a NonEmptySlice<T>, size: Size) -> Self {
349 Self { slice, size }
350 }
351}
352
353impl<'a, T> IntoIterator for Windows<'a, T> {
354 type Item = &'a NonEmptySlice<T>;
355
356 type IntoIter = Map<slice::Windows<'a, T>, NonEmptySliceFn<'a, T>>;
357
358 fn into_iter(self) -> Self::IntoIter {
359 self.slice
360 .as_slice()
361 .windows(self.size.get())
362 .map(|window| unsafe { NonEmptySlice::from_slice_unchecked(window) })
364 }
365}
366
367unsafe impl<T> NonEmptyIterator for Windows<'_, T> {}
368
369pub struct ChunkBy<'a, T, P: FnMut(&T, &T) -> bool> {
376 slice: &'a NonEmptySlice<T>,
377 predicate: P,
378}
379
380impl<T: fmt::Debug, P: FnMut(&T, &T) -> bool> fmt::Debug for ChunkBy<'_, T, P> {
381 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
382 formatter
383 .debug_struct(stringify!(ChunkBy))
384 .field(stringify!(slice), &self.slice)
385 .finish()
386 }
387}
388
389impl<'a, T, P: FnMut(&T, &T) -> bool> ChunkBy<'a, T, P> {
390 pub const fn new(slice: &'a NonEmptySlice<T>, predicate: P) -> Self {
392 Self { slice, predicate }
393 }
394}
395
396impl<'a, T, P: FnMut(&T, &T) -> bool> IntoIterator for ChunkBy<'a, T, P> {
397 type Item = &'a NonEmptySlice<T>;
398
399 type IntoIter = Map<slice::ChunkBy<'a, T, P>, NonEmptySliceFn<'a, T>>;
400
401 fn into_iter(self) -> Self::IntoIter {
402 self.slice
403 .as_slice()
404 .chunk_by(self.predicate)
405 .map(|chunk| unsafe { NonEmptySlice::from_slice_unchecked(chunk) })
407 }
408}
409
410unsafe impl<T, P: FnMut(&T, &T) -> bool> NonEmptyIterator for ChunkBy<'_, T, P> {}
411
412pub struct ChunkByMut<'a, T, P: FnMut(&T, &T) -> bool> {
419 slice: &'a mut NonEmptySlice<T>,
420 predicate: P,
421}
422
423impl<T: fmt::Debug, P: FnMut(&T, &T) -> bool> fmt::Debug for ChunkByMut<'_, T, P> {
424 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
425 formatter
426 .debug_struct(stringify!(ChunkByMut))
427 .field(stringify!(slice), &self.slice)
428 .finish()
429 }
430}
431
432impl<'a, T, P: FnMut(&T, &T) -> bool> ChunkByMut<'a, T, P> {
433 pub const fn new(slice: &'a mut NonEmptySlice<T>, predicate: P) -> Self {
435 Self { slice, predicate }
436 }
437}
438
439impl<'a, T, P: FnMut(&T, &T) -> bool> IntoIterator for ChunkByMut<'a, T, P> {
440 type Item = &'a mut NonEmptySlice<T>;
441
442 type IntoIter = Map<slice::ChunkByMut<'a, T, P>, NonEmptyMutSliceFn<'a, T>>;
443
444 fn into_iter(self) -> Self::IntoIter {
445 self.slice
446 .as_mut_slice()
447 .chunk_by_mut(self.predicate)
448 .map(|chunk| unsafe { NonEmptySlice::from_mut_slice_unchecked(chunk) })
450 }
451}
452
453unsafe impl<T, P: FnMut(&T, &T) -> bool> NonEmptyIterator for ChunkByMut<'_, T, P> {}
454
455#[derive(Debug)]
462pub struct EscapeAscii<'a> {
463 bytes: &'a NonEmptyBytes,
464}
465
466impl<'a> EscapeAscii<'a> {
467 #[must_use]
469 pub const fn new(bytes: &'a NonEmptyBytes) -> Self {
470 Self { bytes }
471 }
472}
473
474impl<'a> IntoIterator for EscapeAscii<'a> {
475 type Item = u8;
476
477 type IntoIter = slice::EscapeAscii<'a>;
478
479 fn into_iter(self) -> Self::IntoIter {
480 self.bytes.as_slice().escape_ascii()
481 }
482}
483
484unsafe impl NonEmptyIterator for EscapeAscii<'_> {}