cubecl_core/frontend/iter/traits/
indexed.rs1use crate::frontend::iter::{EnumerateExpand, RevExpand, SkipExpand, TakeExpand, ZipExpand};
2use crate::prelude::*;
3
4pub trait IndexedIteratorExpand: IteratorExpand {
9 fn __expand_len_method(&self, scope: &Scope) -> NativeExpand<usize>;
11
12 fn get(&mut self, scope: &Scope, index: NativeExpand<usize>) -> Self::Item;
14
15 fn __expand_zip_method<U>(
17 self,
18 scope: &Scope,
19 other: IterArg<U>,
20 ) -> ZipExpand<Self, U::IntoIter>
21 where
22 U: IntoIteratorExpand,
23 {
24 ZipExpand::new(self, other.0.__expand_into_iter_method(scope))
25 }
26
27 fn __expand_enumerate_method(self, _scope: &Scope) -> EnumerateExpand<Self> {
29 EnumerateExpand::new(self)
30 }
31
32 fn __expand_rev_method(self, _scope: &Scope) -> RevExpand<Self> {
34 RevExpand::new(self)
35 }
36
37 fn __expand_take_method(self, _scope: &Scope, n: NativeExpand<usize>) -> TakeExpand<Self> {
39 TakeExpand::new(self, n)
40 }
41
42 fn __expand_skip_method(self, _scope: &Scope, n: NativeExpand<usize>) -> SkipExpand<Self> {
44 SkipExpand::new(self, n)
45 }
46}
47
48pub(crate) fn expand_indexed<I: IndexedIteratorExpand>(
53 mut iter: I,
54 scope: &Scope,
55 body: &mut dyn FnMut(&Scope, I::Item),
56) {
57 let start = NativeExpand::<usize>::from_lit(scope, 0);
58 let end = iter.__expand_len_method(scope);
59 RangeExpand::new(start, end).expand(scope, &mut |scope, index| {
60 let item = iter.get(scope, index);
61 body(scope, item);
62 });
63}