bulks 0.6.6

Amazing bulks! They are like iterators, but in bulk, and therefore support collection into arrays.
Documentation
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
use crate::Bulk;
use crate::DoubleEndedBulk;
use crate::IntoBulk;

use array_trait::length;
use array_trait::length::LengthValue;
pub(crate) use private::IntoContained as IntoContained;
#[allow(unused)]
pub(crate) use private::IntoContainedIter as IntoContainedIter;
pub(crate) use private::ContainedIntoIter as ContainedIntoIter;

pub struct Contained<I>
where
    I: Iterator
{
    iter: I
}

impl<I> Iterator for Contained<I>
where
    I: Iterator
{
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item>
    {
        self.iter.next()
    }
}
impl<I> ExactSizeIterator for Contained<I>
where
    I: Iterator
{
    fn len(&self) -> usize
    {
        Bulk::len(self)
    }
    fn is_empty(&self) -> bool
    {
        Bulk::is_empty(self)
    }
}
impl<I> DoubleEndedIterator for Contained<I>
where
    I: DoubleEndedIterator
{
    fn next_back(&mut self) -> Option<Self::Item>
    {
        self.iter.next_back()
    }
}

impl<I> Contained<I>
where
    I: Iterator
{
    /// # Safety
    /// 
    /// This creates a bulk that is possibly only invalid.
    /// 
    /// Always wrap this bulk in another bulk so that its length is limited.
    pub(crate) const unsafe fn new(iter: I) -> Self
    {
        Self {
            iter
        }
    }
}

impl<I> Bulk for Contained<I>
where
    I: Iterator
{
    default type MinLength = <I as private::InfiniteSpec>::Length;
    default type MaxLength = <I as private::InfiniteSpec>::Length;

    #[inline]
    default fn len(&self) -> usize
    {
        self.iter.size_hint().1.unwrap_or(usize::MAX)
    }

    #[inline]
    default fn is_empty(&self) -> bool
    {
        Bulk::len(self) == 0
    }

    fn first(mut self) -> Option<Self::Item>
    where
        Self: Sized
    {
        self.iter.next()
    }
    fn last(self) -> Option<Self::Item>
    where
        Self: Sized
    {
        self.iter.last()
    }
    fn nth<L>(mut self, n: L) -> Option<Self::Item>
    where
        Self: Sized,
        L: LengthValue
    {
        self.iter.nth(length::value::len(n))
    }
    
    #[inline]
    default fn for_each<F>(self, _f: F)
    where
        Self: Sized,
        F: FnMut(Self::Item)
    {
        panic!("Possibly infinite iterator.")
    }
    
    #[inline]
    default fn try_for_each<F, R>(self, _f: F) -> R
    where
        Self: Sized,
        F: FnMut(Self::Item) -> R,
        R: core::ops::Try<Output = ()>
    {
        panic!("Possibly infinite iterator.")
    }
}
impl<I> Bulk for Contained<I>
where
    I: ExactSizeIterator
{
    type MinLength = <I as private::InfiniteSpec>::Length;
    type MaxLength = <I as private::InfiniteSpec>::Length;

    #[inline]
    fn len(&self) -> usize
    {
        self.iter.len()
    }

    #[inline]
    fn is_empty(&self) -> bool
    {
        self.iter.is_empty()
    }
    
    #[inline]
    fn for_each<F>(self, f: F)
    where
        Self: Sized,
        F: FnMut(Self::Item)
    {
        self.iter.for_each(f);
    }
    
    #[inline]
    fn try_for_each<F, R>(mut self, f: F) -> R
    where
        Self: Sized,
        F: FnMut(Self::Item) -> R,
        R: core::ops::Try<Output = ()>
    {
        self.iter.try_for_each(f)
    }
}
impl<I> DoubleEndedBulk for Contained<I>
where
    I: DoubleEndedIterator
{
    #[inline]
    default fn rev_for_each<F>(self, _f: F)
    where
        Self: Sized,
        F: FnMut(Self::Item)
    {
        panic!("Possibly infinite iterator.")
    }
    
    #[inline]
    default fn try_rev_for_each<F, R>(self, _f: F) -> R
    where
        Self: Sized,
        F: FnMut(Self::Item) -> R,
        R: core::ops::Try<Output = ()>
    {
        panic!("Possibly infinite iterator.")
    }
}
impl<I> DoubleEndedBulk for Contained<I>
where
    I: DoubleEndedIterator + ExactSizeIterator
{
    #[inline]
    fn rev_for_each<F>(self, f: F)
    where
        Self: Sized,
        F: FnMut(Self::Item)
    {
        self.iter.rev().for_each(f);
    }
    
    #[inline]
    fn try_rev_for_each<F, R>(self, f: F) -> R
    where
        Self: Sized,
        F: FnMut(Self::Item) -> R,
        R: core::ops::Try<Output = ()>
    {
        self.iter.rev().try_for_each(f)
    }
}

mod private
{
    use array_trait::{length::Length, same::Same};

    use crate::{Contained, IntoBulk, util::InfiniteIterator};

    pub trait InfiniteSpec
    {
        type Length: Length<Elem = ()> + ?Sized;
    }
    impl<I> InfiniteSpec for I
    {
        default type Length = [()];
    }
    impl<I> InfiniteSpec for I
    where
        I: InfiniteIterator
    {
        type Length = [(); usize::MAX];
    }

    /// # Safety
    /// 
    /// This creates an iterator that is possibly only invalid.
    /// 
    /// Always wrap this bulk in another bulk so that its length is limited.
    #[allow(unused)]
    pub unsafe trait IntoContainedIter: IntoIterator
    {
        type IntoContainedIter: ExactSizeIterator<Item = Self::Item>;

        /// # Safety
        /// 
        /// This creates a bulk that is possibly only invalid.
        /// 
        /// Always wrap this bulk in another bulk so that its length is limited.
        unsafe fn into_contained_iter(self) -> Self::IntoContainedIter;
    }
    unsafe impl<T> IntoContainedIter for T
    where
        T: IntoIterator
    {
        default type IntoContainedIter = Contained<T::IntoIter>;

        default unsafe fn into_contained_iter(self) -> Self::IntoContainedIter
        {
            unsafe {
                Contained::new(self.into_iter()).same().ok().unwrap()
            }
        }
    }
    unsafe impl<T> IntoContainedIter for T
    where
        T: IntoIterator<IntoIter: ExactSizeIterator>
    {
        type IntoContainedIter = T::IntoIter;

        unsafe fn into_contained_iter(self) -> Self::IntoContainedIter
        {
            self.into_iter()
        }
    }

    /// # Safety
    /// 
    /// This creates a bulk that is possibly only invalid.
    /// 
    /// Always wrap this bulk in another bulk so that its length is limited.
    pub const unsafe trait IntoContained: IntoIterator
    {
        type IntoContained: ~const IntoBulk<Item = Self::Item, IntoIter: ExactSizeIterator<Item = Self::Item>>;

        /// # Safety
        /// 
        /// This creates a bulk that is possibly only invalid.
        /// 
        /// Always wrap this bulk in another bulk so that its length is limited.
        unsafe fn into_contained(self) -> Self::IntoContained;
    }
    unsafe impl<T> IntoContained for T
    where
        T: IntoIterator
    {
        default type IntoContained = Contained<T::IntoIter>;

        default unsafe fn into_contained(self) -> Self::IntoContained
        {
            unsafe {
                Contained::new(self.into_iter()).same().ok().unwrap()
            }
        }
    }
    unsafe impl<T> const IntoContained for T
    where
        T: ~const IntoBulk
    {
        type IntoContained = T;

        unsafe fn into_contained(self) -> Self::IntoContained
        {
            self
        }
    }

    /// # Safety
    /// 
    /// Assumes that the possibly infinite iterator is contained by another exact length iterator.
    /// 
    /// This is ok to use with adapters such as zip and take.
    pub unsafe trait ContainedIntoIter: IntoIterator<IntoIter: ExactSizeIterator>
    {
        type ContainedIntoIter: Iterator<Item = Self::Item>;

        unsafe fn contained_into_iter(self) -> Self::ContainedIntoIter;
    }
    unsafe impl<T> ContainedIntoIter for T
    where
        T: IntoIterator<IntoIter: ExactSizeIterator>
    {
        default type ContainedIntoIter = T::IntoIter;

        default unsafe fn contained_into_iter(self) -> Self::ContainedIntoIter
        {
            self.into_iter().same().ok().unwrap()
        }
    }
    unsafe impl<I> ContainedIntoIter for Contained<I>
    where
        I: Iterator
    {
        type ContainedIntoIter = I;

        unsafe fn contained_into_iter(self) -> Self::ContainedIntoIter
        {
            self.iter
        }
    }

    pub trait EitherIntoBulk<B>: IntoIterator
    where
        B: IntoIterator
    {
        type EitherIntoBulk: IntoIterator;
    }
    impl<T, B> EitherIntoBulk<B> for T
    where
        T: IntoIterator,
        B: IntoIterator
    {
        default type EitherIntoBulk = T;
    }
    impl<T, B> EitherIntoBulk<B> for T
    where
        T: IntoIterator,
        B: IntoBulk
    {
        type EitherIntoBulk = B;
    }
}

#[rustc_on_unimplemented(
    message = "value of type `{Self}` cannot be zipped with `{B}` in bulk",
    label = "neither `{Self}` nor `{B}` can be converted into bulks",
)]
pub trait EitherIntoBulk<B>: private::EitherIntoBulk<B, EitherIntoBulk: IntoBulk>
where
    B: IntoIterator
{
    
}
impl<T, B> EitherIntoBulk<B> for T
where
    T: private::EitherIntoBulk<B, EitherIntoBulk: IntoBulk>,
    B: IntoIterator
{
    
}

#[rustc_on_unimplemented(
    message = "value of type `{Self}` cannot be zipped with `{B}` in bulk",
    label = "value of type `{Self}` cannot be zipped with `{B}` in bulk",
)]
pub const trait IntoContainedBy<B>: ~const IntoContained + EitherIntoBulk<B> + Sized
where
    B: IntoIterator
{

}
impl<T, B> const IntoContainedBy<B> for T
where
    T: ~const IntoContained + EitherIntoBulk<B>,
    B: IntoIterator
{

}