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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! Const equivalent of iterators with a specific `next` function signature.
//!
//! The docs for [`ConstIntoIter`] has more information on
//! const equivalents of [`IntoIterator`] and [`Iterator`].
//!
//! [`IntoIterator`]: core::iter::IntoIterator
//! [`Iterator`]: core::iter::Iterator
use PhantomData;
use ManuallyDrop;
use TypeEq;
pub use crate;
pub
pub use for_each;
pub use collect_const;
pub use eval;
pub use ;
pub use *;
pub use ;
pub use Step;
/// Const analog of the [`IntoIterator`] trait.
///
/// # Implementor
///
/// Implementors are expected to be one of these:
///
/// - [`IsIntoIterKind` kind](#isintoiterkind)
/// - [`IsIteratorKind` kind](#isiteratorkind)
/// - Standard library types, of the [`IsStdKind`] kind
///
/// ### Dropping
///
/// Because dropping in const is not supported, the `konst::iter` macros:
/// - drop the remaining items for iterators that impl `ConstIntoIter<ITEMS_NEED_DROP = true>`
/// - `mem::forget` the iterator
///
/// because of this,
/// iterators cannot manage heap allocations or other resources in their destructors.
///
/// ### `IsIntoIterKind`
///
/// These are user-defined types convertible to const iterators.
///
/// These implement `ConstIntoIter<Kind = `[`IsIntoIterKind`]`>`
/// and are expected to define this inherent method for converting to
/// a const iterator:
///
/// ```rust
/// # struct II;
/// # struct SomeIterator;
/// # impl II {
/// const fn const_into_iter(self) -> SomeIterator {
/// # loop{}
/// # }
/// # }
/// ```
///
/// [full example below](#non-iter-example)
///
/// ### `IsIteratorKind`
///
/// These are const iterator types.
///
/// These implement `ConstIntoIter<Kind = `[`IsIteratorKind`]`>`
/// and are expected to define this inherent method:
///
/// ```rust
/// # struct SomeIterator;
/// # type Item = u8;
/// # impl SomeIterator {
/// // Equivalent to `Iterator::next`
/// const fn next(&mut self) -> Option<Item> {
/// # loop{}
/// # }
/// # }
/// ```
/// Where `Item` can be any type.
///
/// These are other methods that you can optionaly define,
/// which most iterators from the `konst` crate define:
/// ```rust
/// # struct SomeIterator;
/// # struct SomeIteratorRev;
/// # type Item = u8;
/// # impl SomeIterator {
/// // equivalent to `DoubleEndedÃŒterator::mext_back`
/// const fn next_back(&mut self) -> Option<Item> {
/// # loop{}
/// // ... some code...
/// }
///
/// // Reverses the iterator, equivalent to `Iterator::rev`
/// const fn rev(self) -> SomeIteratorRev {
/// # loop{}
/// // ... some code...
/// }
///
/// // Clones the iterator, equivalent to `Clone::clone`
/// const fn copy(&self) -> Self {
/// # loop{}
/// // ... some code...
/// }
/// # }
/// ```
/// Where `SomeIteratorRev` should be a `ConstIntoIter<Kind = IsIteratorKind>`
/// which has the same inherent methods for iteration,
/// and returns the same `Item` type.
///
/// [full example below](#iter-example)
///
/// # Examples
///
/// <span id = "non-iter-example"></span>
/// ### Implementing for an into-iterator
///
/// ```rust
/// use konst::{iter, slice};
///
/// struct GetSlice<'a, T>{
/// slice: &'a [T],
/// up_to: usize,
/// }
///
/// impl<'a, T> iter::ConstIntoIter for GetSlice<'a, T> {
/// type Kind = iter::IsIntoIterKind;
/// type IntoIter = konst::slice::Iter<'a, T>;
/// type Item = &'a T;
/// const ITEMS_NEED_DROP: bool = false;
/// }
///
/// impl<'a, T> GetSlice<'a, T> {
/// const fn const_into_iter(self) -> konst::slice::Iter<'a, T> {
/// slice::iter(slice::slice_up_to(self.slice, self.up_to))
/// }
/// }
///
/// const fn sum_powers(up_to: usize) -> u64 {
/// let gs = GetSlice{slice: &[1, 2, 4, 8, 16, 32, 64, 128], up_to};
///
/// iter::eval!(gs,fold(0, |l, &r| l + r))
/// }
///
/// assert_eq!(sum_powers(0), 0);
/// assert_eq!(sum_powers(1), 1);
/// assert_eq!(sum_powers(2), 3);
/// assert_eq!(sum_powers(3), 7);
/// assert_eq!(sum_powers(4), 15);
/// assert_eq!(sum_powers(5), 31);
///
/// ```
///
/// <span id = "iter-example"></span>
/// ### Implementing for an iterator
///
/// ```rust
/// use konst::iter::{self, ConstIntoIter};
///
/// struct Countdown(u8);
///
/// impl ConstIntoIter for Countdown {
/// type Kind = iter::IsIteratorKind;
/// type IntoIter = Self;
/// type Item = u8;
/// const ITEMS_NEED_DROP: bool = false;
/// }
///
/// impl Countdown {
/// const fn next(&mut self) -> Option<u8> {
/// konst::option::map!(self.0.checked_sub(1), |ret| {
/// self.0 = ret;
/// ret
/// })
/// }
/// }
///
/// const fn sum(initial: u8) -> u16 {
/// iter::eval!(Countdown(initial),fold(0u16, |accum, elem| accum + elem as u16))
/// }
///
/// assert_eq!(sum(0), 0);
/// assert_eq!(sum(1), 0);
/// assert_eq!(sum(2), 1);
/// assert_eq!(sum(3), 3);
/// assert_eq!(sum(4), 6);
/// assert_eq!(sum(5), 10);
///
/// ```
///
/// ### Implementing for a double-ended iterator
///
/// ```rust
/// use konst::iter;
///
/// assert_eq!(HOURS, [1, 2, 3, 4, 5, 6, 12, 11, 10, 9, 8, 7]);
///
/// const HOURS: [u8; 12] = {
/// let mut arr = [0; 12];
/// let hours = Hours::new();
///
/// iter::for_each!{(i, hour) in 0..6,zip(hours.copy()) =>
/// arr[i] = hour;
/// }
/// iter::for_each!{(i, hour) in 6..12,zip(hours.rev()) =>
/// arr[i] = hour;
/// }
///
/// arr
/// };
///
///
/// struct Hours{
/// start: u8,
/// end: u8,
/// }
///
/// impl iter::ConstIntoIter for Hours {
/// type Kind = iter::IsIteratorKind;
/// type IntoIter = Self;
/// type Item = u8;
/// const ITEMS_NEED_DROP: bool = false;
/// }
///
/// impl Hours {
/// const fn new() -> Self {
/// Self {start: 1, end: 13}
/// }
///
/// const fn next(&mut self) -> Option<u8> {
/// if self.start == self.end {
/// None
/// } else {
/// let ret = self.start;
/// self.start += 1;
/// Some(ret)
/// }
/// }
///
/// const fn next_back(&mut self) -> Option<u8> {
/// if self.start == self.end {
/// None
/// } else {
/// self.end -= 1;
/// Some(self.end)
/// }
/// }
///
/// const fn rev(self) -> HoursRev {
/// HoursRev(self)
/// }
///
/// /// Since `Clone::clone` isn't const callable on stable,
/// /// clonable iterators must define an inherent method to be cloned
/// const fn copy(&self) -> Self {
/// Self {..*self}
/// }
/// }
///
/// struct HoursRev(Hours);
///
/// impl iter::ConstIntoIter for HoursRev {
/// type Kind = iter::IsIteratorKind;
/// type IntoIter = Self;
/// type Item = u8;
/// const ITEMS_NEED_DROP: bool = false;
/// }
///
/// impl HoursRev {
/// const fn next(&mut self) -> Option<u8> {
/// self.0.next_back()
/// }
///
/// const fn next_back(&mut self) -> Option<u8> {
/// self.0.next()
/// }
///
/// const fn rev(self) -> Hours {
/// self.0
/// }
///
/// const fn copy(&self) -> Self {
/// Self(self.0.copy())
/// }
/// }
///
///
/// ```
///
/// Wrapper for `ConstIntoIter` implementors,
/// that defines different methods depending on the
/// value of `K`.
///
/// You're not intended to use this directly, the intended way to convert a type
/// into a const iterator is with the [`into_iter`] macro.
////////////////////////////////////////////////////////////////////////////////
/// Coerces the argument to a type that has a `.const_into_iter()` method
///
/// There's 3 different ways to use this function
/// - [passing std types](#std-example): which wraps them in
/// [`IntoIterWrapper<T, IsStdKind>`](crate::iter::IntoIterWrapper)
/// - [passing non-iterator user-defined types](#user-example): returning the argument back
/// - [passing iterator types](#iterator-example): which wraps them in
/// [`IntoIterWrapper<T, IsIteratorKind>`](crate::iter::IntoIterWrapper)
///
/// Note: `konst::iter::coerce(value).const_into_iter()` is equivalent to
/// `konst::iter::into_iter!(value)`
///
///
/// # Examples
///
/// <span id="std-example"></span>
/// ### Std argument
///
/// ```rust
/// use konst::{
/// iter::{self, IsStdKind, IntoIterWrapper},
/// range::RangeIter
/// };
/// use std::ops::Range;
///
/// // Coerce wraps `ConstIntoIter<Kind = IsStdKind>` types in an `IntoIterWrapper`
/// let wrapper: IntoIterWrapper<Range<u32>, IsStdKind> = iter::coerce(0..3);
///
/// // ... which then converts the std type into an iterator with `.const_into_iter()`
/// let mut iter: RangeIter<u32> = wrapper.const_into_iter();
///
/// assert_eq!(iter.next(), Some(0));
/// assert_eq!(iter.next(), Some(1));
/// assert_eq!(iter.next(), Some(2));
/// assert_eq!(iter.next(), None);
///
/// ```
///
/// <span id="user-example"></span>
/// ### User-defined argument
///
/// ```rust
/// use konst::{
/// iter::{self, ConstIntoIter, IsIntoIterKind},
/// range::RangeInclusiveIter as RangeIncIter,
/// };
///
/// struct Double(u32);
///
/// impl ConstIntoIter for Double {
/// type Kind = IsIntoIterKind;
/// type Item = u32;
/// type IntoIter = RangeIncIter<u32>;
///
/// // items are Copy, so they don't need to be dropped
/// const ITEMS_NEED_DROP: bool = false;
/// }
///
/// impl Double {
/// pub const fn const_into_iter(self) -> RangeIncIter<u32> {
/// iter::into_iter!(0 ..= self.0 * 2)
/// }
/// }
///
/// // `coerce` is an identity function for `ConstIntoIter<Kind = IsStdKind>` arguments
/// let double: Double = iter::coerce(Double(2));
///
/// // ... then it's converted into an iterator with `.const_into_iter()`
/// let mut iter: RangeIncIter<u32> = double.const_into_iter();
///
/// assert_eq!(iter.next(), Some(0));
/// assert_eq!(iter.next(), Some(1));
/// assert_eq!(iter.next(), Some(2));
/// assert_eq!(iter.next(), Some(3));
/// assert_eq!(iter.next(), Some(4));
/// assert_eq!(iter.next(), None);
///
/// ```
///
/// <span id="iterator-example"></span>
/// ### Iterator argument
///
/// ```rust
/// use konst::{
/// iter::{self, ConstIntoIter, IsIteratorKind, IntoIterWrapper},
/// range::RangeFromIter,
/// };
///
/// let mut iter: RangeFromIter<u32> = iter::into_iter!(3..);
///
/// // Coerce wraps `ConstIntoIter<Kind = IsIteratorKind>` types in a `IntoIterWrapper`
/// let wrapper: IntoIterWrapper<RangeFromIter<u32>, IsIteratorKind> = iter::coerce(iter);
///
/// // ... which then unwraps back into the iterator with `.const_into_iter()`
/// let mut iter: RangeFromIter<u32> = wrapper.const_into_iter();
///
/// assert_eq!(iter.next(), Some(3));
/// assert_eq!(iter.next(), Some(4));
/// assert_eq!(iter.next(), Some(5));
/// assert_eq!(iter.next(), Some(6));
/// ```
///
pub const
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/// Trait for the types that are assignable to [`ConstIntoIter::Kind`]
///
/// Computes the type returned by [`coerce`]:
/// - if [`T::Kind`](ConstIntoIter::Kind)` == `[`IsStdKind`]:
/// this evaluates to `IntoIterWrapper<T, IsStdKind>`.
/// - if [`T::Kind`](ConstIntoIter::Kind)` == `[`IsIntoIterKind`]:
/// this evaluates to `T`.
/// - if [`T::Kind`](ConstIntoIter::Kind)` == `[`IsIteratorKind`]:
/// this evaluates to `IntoIterWrapper<T, IsIteratorKind>`.
pub type CoerceTo<T> = CoerceTo;