git.delta.rocks / jrsonnet / refs/commits / 9f4360cc82b9

difftreelog

refactor drop array iteration inlining

Yaroslav Bolyukin2023-07-26parent: #992351c.patch.diff
in: master

3 files changed

modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,6 +3,7 @@
 package.repository = "https://github.com/CertainLach/jrsonnet"
 members = ["crates/*", "bindings/jsonnet", "cmds/jrsonnet", "tests"]
 default-members = ["cmds/jrsonnet"]
+resolver = "2"
 
 [workspace.dependencies]
 jrsonnet-evaluator = { path = "./crates/jrsonnet-evaluator", version = "0.5.0-pre9" }
modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -185,36 +185,15 @@
 		pass!(self.get_lazy(index))
 	}
 
-	#[cfg(feature = "nightly")]
-	pub fn iter(&self) -> UnknownArrayIter<'_> {
-		pass_iter_call!(self.iter => UnknownArrayIter)
-	}
-	#[cfg(not(feature = "nightly"))]
 	pub fn iter(&self) -> impl ArrayLikeIter<Result<Val>> + '_ {
 		(0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))
 	}
 
 	/// Iterate over elements, returning lazy values.
-	#[cfg(feature = "nightly")]
-	pub fn iter_lazy(&self) -> UnknownArrayIterLazy<'_> {
-		pass_iter_call!(self.iter_lazy => UnknownArrayIterLazy)
-	}
-	#[cfg(not(feature = "nightly"))]
 	pub fn iter_lazy(&self) -> impl ArrayLikeIter<Thunk<Val>> + '_ {
 		(0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))
 	}
 
-	#[cfg(feature = "nightly")]
-	pub fn iter_cheap(&self) -> Option<UnknownArrayIterCheap<'_>> {
-		macro_rules! question {
-			($v:expr) => {
-				$v?
-			};
-		}
-		Some(pass_iter_call!(self.iter_cheap in question => UnknownArrayIterCheap))
-	}
-
-	#[cfg(not(feature = "nightly"))]
 	pub fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {
 		if self.is_cheap() {
 			Some((0..self.len()).map(|i| self.get_cheap(i).expect("length and is_cheap checked")))
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
1//! Those implementations are a bit sketchy, as this is mostly performance experiments
2//! of not yet finished nightly rust features
3
4use std::{cell::RefCell, iter, mem::replace, rc::Rc};1use std::{cell::RefCell, iter, mem::replace, rc::Rc};
52
17};14};
1815
19pub trait ArrayLike: Sized + Into<ArrValue> {16pub trait ArrayLike: Sized + Into<ArrValue> {
20 #[cfg(feature = "nightly")]
21 type Iter<'t>
22 where
23 Self: 't;
24 #[cfg(feature = "nightly")]
25 type IterLazy<'t>
26 where
27 Self: 't;
28 #[cfg(feature = "nightly")]
29 type IterCheap<'t>
30 where
31 Self: 't;
32
33 fn len(&self) -> usize;17 fn len(&self) -> usize;
34 fn is_empty(&self) -> bool {18 fn is_empty(&self) -> bool {
37 fn get(&self, index: usize) -> Result<Option<Val>>;21 fn get(&self, index: usize) -> Result<Option<Val>>;
38 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>>;22 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>>;
39 fn get_cheap(&self, index: usize) -> Option<Val>;23 fn get_cheap(&self, index: usize) -> Option<Val>;
40 #[cfg(feature = "nightly")]
41 #[allow(clippy::iter_not_returning_iterator)]
42 fn iter(&self) -> Self::Iter<'_>;
43 #[cfg(feature = "nightly")]
44 fn iter_lazy(&self) -> Self::IterLazy<'_>;
45 #[cfg(feature = "nightly")]
46 fn iter_cheap(&self) -> Option<Self::IterCheap<'_>>;
4724
48 fn reverse(self) -> ArrValue {25 fn reverse(self) -> ArrValue {
49 ArrValue::Reverse(Cc::new(ReverseArray(self.into())))26 ArrValue::Reverse(Cc::new(ReverseArray(self.into())))
59}36}
6037
61impl SliceArray {38impl SliceArray {
62 #[cfg(not(feature = "nightly"))]
63 fn iter(&self) -> impl Iterator<Item = Result<Val>> + '_ {39 fn iter(&self) -> impl Iterator<Item = Result<Val>> + '_ {
64 self.inner40 self.inner
65 .iter()41 .iter()
68 .step_by(self.step as usize)44 .step_by(self.step as usize)
69 }45 }
7046
71 #[cfg(not(feature = "nightly"))]
72 fn iter_lazy(&self) -> impl Iterator<Item = Thunk<Val>> + '_ {47 fn iter_lazy(&self) -> impl Iterator<Item = Thunk<Val>> + '_ {
73 self.inner48 self.inner
74 .iter_lazy()49 .iter_lazy()
77 .step_by(self.step as usize)52 .step_by(self.step as usize)
78 }53 }
7954
80 #[cfg(not(feature = "nightly"))]
81 fn iter_cheap(&self) -> Option<impl crate::arr::ArrayLikeIter<Val> + '_> {55 fn iter_cheap(&self) -> Option<impl crate::arr::ArrayLikeIter<Val> + '_> {
82 Some(56 Some(
83 self.inner57 self.inner
88 )62 )
89 }63 }
90}64}
91#[cfg(feature = "nightly")]
92type SliceArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
93#[cfg(feature = "nightly")]
94type SliceArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
95#[cfg(feature = "nightly")]
96type SliceArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
97impl ArrayLike for SliceArray {65impl ArrayLike for SliceArray {
98 #[cfg(feature = "nightly")]
99 type Iter<'t> = SliceArrayIter<'t>;
100 #[cfg(feature = "nightly")]
101 type IterLazy<'t> = SliceArrayLazyIter<'t>;
102 #[cfg(feature = "nightly")]
103 type IterCheap<'t> = SliceArrayCheapIter<'t>;
104
105 fn len(&self) -> usize {66 fn len(&self) -> usize {
106 iter::repeat(())67 iter::repeat(())
121 self.iter_cheap()?.nth(index)82 self.iter_cheap()?.nth(index)
122 }83 }
123
124 #[cfg(feature = "nightly")]
125 fn iter(&self) -> SliceArrayIter<'_> {
126 self.inner
127 .iter()
128 .skip(self.from as usize)
129 .take((self.to - self.from) as usize)
130 .step_by(self.step as usize)
131 }
132
133 #[cfg(feature = "nightly")]
134 fn iter_lazy(&self) -> SliceArrayLazyIter<'_> {
135 self.inner
136 .iter_lazy()
137 .skip(self.from as usize)
138 .take((self.to - self.from) as usize)
139 .step_by(self.step as usize)
140 }
141
142 #[cfg(feature = "nightly")]
143 fn iter_cheap(&self) -> Option<SliceArrayCheapIter<'_>> {
144 Some(
145 self.inner
146 .iter_cheap()?
147 .skip(self.from as usize)
148 .take((self.to - self.from) as usize)
149 .step_by(self.step as usize),
150 )
151 }
152}84}
153impl From<SliceArray> for ArrValue {85impl From<SliceArray> for ArrValue {
154 fn from(value: SliceArray) -> Self {86 fn from(value: SliceArray) -> Self {
15890
159#[derive(Trace, Debug, Clone)]91#[derive(Trace, Debug, Clone)]
160pub struct CharArray(pub Rc<Vec<char>>);92pub struct CharArray(pub Rc<Vec<char>>);
161#[cfg(feature = "nightly")]
162type CharArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
163#[cfg(feature = "nightly")]
164type CharArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
165#[cfg(feature = "nightly")]
166type CharArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
167impl ArrayLike for CharArray {93impl ArrayLike for CharArray {
168 #[cfg(feature = "nightly")]
169 type Iter<'t> = CharArrayIter<'t>;
170 #[cfg(feature = "nightly")]
171 type IterLazy<'t> = CharArrayLazyIter<'t>;
172 #[cfg(feature = "nightly")]
173 type IterCheap<'t> = CharArrayCheapIter<'t>;
174
175 fn len(&self) -> usize {94 fn len(&self) -> usize {
176 self.0.len()95 self.0.len()
190 .map(|v| Val::Str(StrValue::Flat(IStr::from(*v))))109 .map(|v| Val::Str(StrValue::Flat(IStr::from(*v))))
191 }110 }
192
193 #[cfg(feature = "nightly")]
194 fn iter(&self) -> CharArrayIter<'_> {
195 self.0
196 .iter()
197 .map(|v| Ok(Val::Str(StrValue::Flat(IStr::from(*v)))))
198 }
199
200 #[cfg(feature = "nightly")]
201 fn iter_lazy(&self) -> CharArrayLazyIter<'_> {
202 self.0
203 .iter()
204 .map(|v| Thunk::evaluated(Val::Str(StrValue::Flat(IStr::from(*v)))))
205 }
206
207 #[cfg(feature = "nightly")]
208 fn iter_cheap(&self) -> Option<CharArrayCheapIter<'_>> {
209 Some(
210 self.0
211 .iter()
212 .map(|v| Val::Str(StrValue::Flat(IStr::from(*v)))),
213 )
214 }
215}111}
216impl From<CharArray> for ArrValue {112impl From<CharArray> for ArrValue {
217 fn from(value: CharArray) -> Self {113 fn from(value: CharArray) -> Self {
221117
222#[derive(Trace, Debug, Clone)]118#[derive(Trace, Debug, Clone)]
223pub struct BytesArray(pub IBytes);119pub struct BytesArray(pub IBytes);
224#[cfg(feature = "nightly")]
225type BytesArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
226#[cfg(feature = "nightly")]
227type BytesArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
228#[cfg(feature = "nightly")]
229type BytesArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
230impl ArrayLike for BytesArray {120impl ArrayLike for BytesArray {
231 #[cfg(feature = "nightly")]
232 type Iter<'t> = BytesArrayIter<'t>;
233 #[cfg(feature = "nightly")]
234 type IterLazy<'t> = BytesArrayLazyIter<'t>;
235 #[cfg(feature = "nightly")]
236 type IterCheap<'t> = BytesArrayCheapIter<'t>;
237
238 fn len(&self) -> usize {121 fn len(&self) -> usize {
239 self.0.len()122 self.0.len()
251 self.0.get(index).map(|v| Val::Num(f64::from(*v)))134 self.0.get(index).map(|v| Val::Num(f64::from(*v)))
252 }135 }
253
254 #[cfg(feature = "nightly")]
255 fn iter(&self) -> BytesArrayIter<'_> {
256 self.0.iter().map(|v| Ok(Val::Num(f64::from(*v))))
257 }
258
259 #[cfg(feature = "nightly")]
260 fn iter_lazy(&self) -> BytesArrayLazyIter<'_> {
261 self.0
262 .iter()
263 .map(|v| Thunk::evaluated(Val::Num(f64::from(*v))))
264 }
265
266 #[cfg(feature = "nightly")]
267 fn iter_cheap(&self) -> Option<BytesArrayCheapIter<'_>> {
268 Some(self.0.iter().map(|v| Val::Num(f64::from(*v))))
269 }
270}136}
271impl From<BytesArray> for ArrValue {137impl From<BytesArray> for ArrValue {
272 fn from(value: BytesArray) -> Self {138 fn from(value: BytesArray) -> Self {
297 }))163 }))
298 }164 }
299}165}
300#[cfg(feature = "nightly")]
301type ExprArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
302#[cfg(feature = "nightly")]
303type ExprArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
304#[cfg(feature = "nightly")]
305type ExprArrayCheapIter<'t> = iter::Empty<Val>;
306impl ArrayLike for ExprArray {166impl ArrayLike for ExprArray {
307 #[cfg(feature = "nightly")]
308 type Iter<'t> = ExprArrayIter<'t>;
309 #[cfg(feature = "nightly")]
310 type IterLazy<'t> = ExprArrayLazyIter<'t>;
311 #[cfg(feature = "nightly")]
312 type IterCheap<'t> = ExprArrayCheapIter<'t>;
313
314 fn len(&self) -> usize {167 fn len(&self) -> usize {
315 self.0.cached.borrow().len()168 self.0.cached.borrow().len()
375 None228 None
376 }229 }
377
378 #[cfg(feature = "nightly")]
379 fn iter(&self) -> ExprArrayIter<'_> {
380 (0..self.len()).map(|i| self.get(i).transpose().expect("index checked"))
381 }
382 #[cfg(feature = "nightly")]
383 fn iter_lazy(&self) -> ExprArrayLazyIter<'_> {
384 (0..self.len()).map(|i| self.get_lazy(i).expect("index checked"))
385 }
386 #[cfg(feature = "nightly")]
387 fn iter_cheap(&self) -> Option<Self::IterCheap<'_>> {
388 None
389 }
390}230}
391impl From<ExprArray> for ArrValue {231impl From<ExprArray> for ArrValue {
392 fn from(value: ExprArray) -> Self {232 fn from(value: ExprArray) -> Self {
401 split: usize,241 split: usize,
402 len: usize,242 len: usize,
403}243}
404#[cfg(feature = "nightly")]
405
406type ExtendedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
407#[cfg(feature = "nightly")]
408type ExtendedArrayLazyIter<'t> =
409 impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
410#[cfg(feature = "nightly")]
411type ExtendedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
412impl ExtendedArray {244impl ExtendedArray {
413 pub fn new(a: ArrValue, b: ArrValue) -> Self {245 pub fn new(a: ArrValue, b: ArrValue) -> Self {
414 let a_len = a.len();246 let a_len = a.len();
459 }291 }
460}292}
461impl ArrayLike for ExtendedArray {293impl ArrayLike for ExtendedArray {
462 #[cfg(feature = "nightly")]
463 type Iter<'t> = ExtendedArrayIter<'t>;
464 #[cfg(feature = "nightly")]
465 type IterLazy<'t> = ExtendedArrayLazyIter<'t>;
466 #[cfg(feature = "nightly")]
467 type IterCheap<'t> = ExtendedArrayCheapIter<'t>;
468
469 fn get(&self, index: usize) -> Result<Option<Val>> {294 fn get(&self, index: usize) -> Result<Option<Val>> {
470 if self.split > index {295 if self.split > index {
493 }318 }
494 }319 }
495
496 #[cfg(feature = "nightly")]
497 fn iter(&self) -> ExtendedArrayIter<'_> {
498 WithExactSize(self.a.iter().chain(self.b.iter()), self.len)
499 }
500 #[cfg(feature = "nightly")]
501 fn iter_lazy(&self) -> ExtendedArrayLazyIter<'_> {
502 WithExactSize(self.a.iter_lazy().chain(self.b.iter_lazy()), self.len)
503 }
504 #[cfg(feature = "nightly")]
505 fn iter_cheap(&self) -> Option<ExtendedArrayCheapIter<'_>> {
506 let a = self.a.iter_cheap()?;
507 let b = self.b.iter_cheap()?;
508 Some(WithExactSize(a.chain(b), self.len))
509 }
510}320}
511impl From<ExtendedArray> for ArrValue {321impl From<ExtendedArray> for ArrValue {
512 fn from(value: ExtendedArray) -> Self {322 fn from(value: ExtendedArray) -> Self {
516326
517#[derive(Trace, Debug, Clone)]327#[derive(Trace, Debug, Clone)]
518pub struct LazyArray(pub Cc<Vec<Thunk<Val>>>);328pub struct LazyArray(pub Cc<Vec<Thunk<Val>>>);
519#[cfg(feature = "nightly")]
520type LazyArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
521#[cfg(feature = "nightly")]
522type LazyArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
523#[cfg(feature = "nightly")]
524type LazyArrayCheapIter<'t> = iter::Empty<Val>;
525impl ArrayLike for LazyArray {329impl ArrayLike for LazyArray {
526 #[cfg(feature = "nightly")]
527 type Iter<'t> = LazyArrayIter<'t>;
528
529 #[cfg(feature = "nightly")]
530 type IterLazy<'t> = LazyArrayLazyIter<'t>;
531
532 #[cfg(feature = "nightly")]
533 type IterCheap<'t> = LazyArrayCheapIter<'t>;
534
535 fn len(&self) -> usize {330 fn len(&self) -> usize {
536 self.0.len()331 self.0.len()
547 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {342 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
548 self.0.get(index).cloned()343 self.0.get(index).cloned()
549 }344 }
550 #[cfg(feature = "nightly")]
551 fn iter(&self) -> LazyArrayIter<'_> {
552 self.0.iter().map(Thunk::evaluate)
553 }
554 #[cfg(feature = "nightly")]
555 fn iter_lazy(&self) -> LazyArrayLazyIter<'_> {
556 self.0.iter().cloned()
557 }
558 #[cfg(feature = "nightly")]
559 fn iter_cheap(&self) -> Option<LazyArrayCheapIter<'_>> {
560 None
561 }
562}345}
563impl From<LazyArray> for ArrValue {346impl From<LazyArray> for ArrValue {
564 fn from(value: LazyArray) -> Self {347 fn from(value: LazyArray) -> Self {
568351
569#[derive(Trace, Debug, Clone)]352#[derive(Trace, Debug, Clone)]
570pub struct EagerArray(pub Cc<Vec<Val>>);353pub struct EagerArray(pub Cc<Vec<Val>>);
571#[cfg(feature = "nightly")]
572type EagerArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
573#[cfg(feature = "nightly")]
574type EagerArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
575#[cfg(feature = "nightly")]
576type EagerArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
577impl ArrayLike for EagerArray {354impl ArrayLike for EagerArray {
578 #[cfg(feature = "nightly")]
579 type Iter<'t> = EagerArrayIter<'t>;
580
581 #[cfg(feature = "nightly")]
582 type IterLazy<'t> = EagerArrayLazyIter<'t>;
583
584 #[cfg(feature = "nightly")]
585 type IterCheap<'t> = EagerArrayCheapIter<'t>;
586
587 fn len(&self) -> usize {355 fn len(&self) -> usize {
588 self.0.len()356 self.0.len()
600 self.0.get(index).cloned()368 self.0.get(index).cloned()
601 }369 }
602
603 #[cfg(feature = "nightly")]
604 fn iter(&self) -> EagerArrayIter<'_> {
605 self.0.iter().cloned().map(Ok)
606 }
607
608 #[cfg(feature = "nightly")]
609 fn iter_lazy(&self) -> EagerArrayLazyIter<'_> {
610 self.0.iter().cloned().map(Thunk::evaluated)
611 }
612
613 #[cfg(feature = "nightly")]
614 fn iter_cheap(&self) -> Option<EagerArrayCheapIter<'_>> {
615 Some(self.0.iter().cloned())
616 }
617}370}
618impl From<EagerArray> for ArrValue {371impl From<EagerArray> for ArrValue {
619 fn from(value: EagerArray) -> Self {372 fn from(value: EagerArray) -> Self {
648 }401 }
649}402}
650403
651#[cfg(feature = "nightly")]
652type RangeArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
653#[cfg(feature = "nightly")]
654type RangeArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
655#[cfg(feature = "nightly")]
656type RangeArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
657impl ArrayLike for RangeArray {404impl ArrayLike for RangeArray {
658 #[cfg(feature = "nightly")]
659 type Iter<'t> = RangeArrayIter<'t>;
660
661 #[cfg(feature = "nightly")]
662 type IterLazy<'t> = RangeArrayLazyIter<'t>;
663
664 #[cfg(feature = "nightly")]
665 type IterCheap<'t> = RangeArrayCheapIter<'t>;
666
667 fn len(&self) -> usize {405 fn len(&self) -> usize {
668 self.range().len()406 self.range().len()
683 self.range().nth(index).map(|i| Val::Num(f64::from(i)))421 self.range().nth(index).map(|i| Val::Num(f64::from(i)))
684 }422 }
685
686 #[cfg(feature = "nightly")]
687 fn iter(&self) -> RangeArrayIter<'_> {
688 self.range().map(|i| Ok(Val::Num(f64::from(i))))
689 }
690
691 #[cfg(feature = "nightly")]
692 fn iter_lazy(&self) -> RangeArrayLazyIter<'_> {
693 self.range()
694 .map(|i| Thunk::evaluated(Val::Num(f64::from(i))))
695 }
696
697 #[cfg(feature = "nightly")]
698 fn iter_cheap(&self) -> Option<RangeArrayCheapIter<'_>> {
699 Some(self.range().map(|i| Val::Num(f64::from(i))))
700 }
701}423}
702impl From<RangeArray> for ArrValue {424impl From<RangeArray> for ArrValue {
703 fn from(value: RangeArray) -> Self {425 fn from(value: RangeArray) -> Self {
708#[derive(Debug, Trace, Clone)]430#[derive(Debug, Trace, Clone)]
709pub struct ReverseArray(pub ArrValue);431pub struct ReverseArray(pub ArrValue);
710impl ArrayLike for ReverseArray {432impl ArrayLike for ReverseArray {
711 #[cfg(feature = "nightly")]
712 type Iter<'t> = iter::Rev<UnknownArrayIter<'t>>;
713
714 #[cfg(feature = "nightly")]
715 type IterLazy<'t> = iter::Rev<UnknownArrayIterLazy<'t>>;
716
717 #[cfg(feature = "nightly")]
718 type IterCheap<'t> = iter::Rev<UnknownArrayIterCheap<'t>>;
719
720 fn len(&self) -> usize {433 fn len(&self) -> usize {
721 self.0.len()434 self.0.len()
733 self.0.get_cheap(self.0.len() - index - 1)446 self.0.get_cheap(self.0.len() - index - 1)
734 }447 }
735
736 #[cfg(feature = "nightly")]
737 fn iter(&self) -> iter::Rev<UnknownArrayIter<'_>> {
738 self.0.iter().rev()
739 }
740
741 #[cfg(feature = "nightly")]
742 fn iter_lazy(&self) -> iter::Rev<UnknownArrayIterLazy<'_>> {
743 self.0.iter_lazy().rev()
744 }
745
746 #[cfg(feature = "nightly")]
747 fn iter_cheap(&self) -> Option<iter::Rev<UnknownArrayIterCheap<'_>>> {
748 Some(self.0.iter_cheap()?.rev())
749 }
750 fn reverse(self) -> ArrValue {448 fn reverse(self) -> ArrValue {
751 self.0449 self.0
752 }450 }
775 }))473 }))
776 }474 }
777}475}
778#[cfg(feature = "nightly")]
779type MappedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
780#[cfg(feature = "nightly")]
781type MappedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
782#[cfg(feature = "nightly")]
783type MappedArrayCheapIter<'t> = iter::Empty<Val>;
784impl ArrayLike for MappedArray {476impl ArrayLike for MappedArray {
785 #[cfg(feature = "nightly")]
786 type Iter<'t> = MappedArrayIter<'t>;
787 #[cfg(feature = "nightly")]
788 type IterLazy<'t> = MappedArrayLazyIter<'t>;
789 #[cfg(feature = "nightly")]
790 type IterCheap<'t> = MappedArrayCheapIter<'t>;
791
792 fn len(&self) -> usize {477 fn len(&self) -> usize {
793 self.0.cached.borrow().len()478 self.0.cached.borrow().len()
863 None548 None
864 }549 }
865
866 #[cfg(feature = "nightly")]
867 fn iter(&self) -> MappedArrayIter<'_> {
868 (0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))
869 }
870
871 #[cfg(feature = "nightly")]
872 fn iter_lazy(&self) -> MappedArrayLazyIter<'_> {
873 (0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))
874 }
875
876 #[cfg(feature = "nightly")]
877 fn iter_cheap(&self) -> Option<Self::IterCheap<'_>> {
878 None
879 }
880}550}
881impl From<MappedArray> for ArrValue {551impl From<MappedArray> for ArrValue {
882 fn from(value: MappedArray) -> Self {552 fn from(value: MappedArray) -> Self {
906 }576 }
907}577}
908578
909#[cfg(feature = "nightly")]
910type RepeatedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
911#[cfg(feature = "nightly")]
912type RepeatedArrayLazyIter<'t> =
913 impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
914#[cfg(feature = "nightly")]
915type RepeatedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
916impl ArrayLike for RepeatedArray {579impl ArrayLike for RepeatedArray {
917 #[cfg(feature = "nightly")]
918 type Iter<'t> = RepeatedArrayIter<'t>;
919 #[cfg(feature = "nightly")]
920 type IterLazy<'t> = RepeatedArrayLazyIter<'t>;
921 #[cfg(feature = "nightly")]
922 type IterCheap<'t> = RepeatedArrayCheapIter<'t>;
923
924 fn len(&self) -> usize {580 fn len(&self) -> usize {
925 self.0.total_len581 self.0.total_len
946 self.0.data.get_cheap(index % self.0.data.len())602 self.0.data.get_cheap(index % self.0.data.len())
947 }603 }
948
949 #[cfg(feature = "nightly")]
950 fn iter(&self) -> RepeatedArrayIter<'_> {
951 (0..self.0.total_len)
952 .map(|i| self.get(i))
953 .map(Result::transpose)
954 .map(Option::unwrap)
955 }
956
957 #[cfg(feature = "nightly")]
958 fn iter_lazy(&self) -> RepeatedArrayLazyIter<'_> {
959 (0..self.0.total_len)
960 .map(|i| self.get_lazy(i))
961 .map(Option::unwrap)
962 }
963
964 #[cfg(feature = "nightly")]
965 fn iter_cheap(&self) -> Option<RepeatedArrayCheapIter<'_>> {
966 if !self.0.data.is_cheap() {
967 return None;
968 }
969 Some(
970 (0..self.0.total_len)
971 .map(|i| self.get_cheap(i))
972 .map(Option::unwrap),
973 )
974 }
975}604}
976impl From<RepeatedArray> for ArrValue {605impl From<RepeatedArray> for ArrValue {
977 fn from(value: RepeatedArray) -> Self {606 fn from(value: RepeatedArray) -> Self {
978 Self::Repeated(value)607 Self::Repeated(value)
979 }608 }
980}609}
981
982#[cfg(feature = "nightly")]
983macro_rules! impl_iter_enum {
984 ($n:ident => $v:ident) => {
985 pub enum $n<'t> {
986 Bytes(<BytesArray as ArrayLike>::$v<'t>),
987 Expr(<ExprArray as ArrayLike>::$v<'t>),
988 Lazy(<LazyArray as ArrayLike>::$v<'t>),
989 Eager(<EagerArray as ArrayLike>::$v<'t>),
990 Range(<RangeArray as ArrayLike>::$v<'t>),
991 Slice(Box<<SliceArray as ArrayLike>::$v<'t>>),
992 Extended(Box<<ExtendedArray as ArrayLike>::$v<'t>>),
993 Reverse(Box<<ReverseArray as ArrayLike>::$v<'t>>),
994 Mapped(Box<<MappedArray as ArrayLike>::$v<'t>>),
995 Repeated(Box<<RepeatedArray as ArrayLike>::$v<'t>>),
996 }
997 };
998}
999610
1000macro_rules! pass {611macro_rules! pass {
1001 ($t:ident.$m:ident($($ident:ident),*)) => {612 ($t:ident.$m:ident($($ident:ident),*)) => {
1016}627}
1017pub(super) use pass;628pub(super) use pass;
1018
1019#[cfg(feature = "nightly")]
1020macro_rules! pass_iter_call {
1021 ($t:ident.$c:ident $(in $wrap:ident)? => $e:ident) => {
1022 match $t {
1023 ArrValue::Bytes(e) => $e::Bytes($($wrap!)?(e.$c())),
1024 ArrValue::Lazy(e) => $e::Lazy($($wrap!)?(e.$c())),
1025 ArrValue::Expr(e) => $e::Expr($($wrap!)?(e.$c())),
1026 ArrValue::Eager(e) => $e::Eager($($wrap!)?(e.$c())),
1027 ArrValue::Range(e) => $e::Range($($wrap!)?(e.$c())),
1028 ArrValue::Slice(e) => $e::Slice(Box::new($($wrap!)?(e.$c()))),
1029 ArrValue::Extended(e) => $e::Extended(Box::new($($wrap!)?(e.$c()))),
1030 ArrValue::Reverse(e) => $e::Reverse(Box::new($($wrap!)?(e.$c()))),
1031 ArrValue::Mapped(e) => $e::Mapped(Box::new($($wrap!)?(e.$c()))),
1032 ArrValue::Repeated(e) => $e::Repeated(Box::new($($wrap!)?(e.$c()))),
1033 }
1034 };
1035}
1036#[cfg(feature = "nightly")]
1037pub(super) use pass_iter_call;
1038
1039#[cfg(feature = "nightly")]
1040macro_rules! impl_iter {
1041 ($t:ident => $out:ty) => {
1042 impl Iterator for $t<'_> {
1043 type Item = $out;
1044
1045 fn next(&mut self) -> Option<Self::Item> {
1046 pass!(self.next())
1047 }
1048 fn nth(&mut self, count: usize) -> Option<Self::Item> {
1049 pass!(self.nth(count))
1050 }
1051 fn size_hint(&self) -> (usize, Option<usize>) {
1052 pass!(self.size_hint())
1053 }
1054 }
1055 impl DoubleEndedIterator for $t<'_> {
1056 fn next_back(&mut self) -> Option<Self::Item> {
1057 pass!(self.next_back())
1058 }
1059 fn nth_back(&mut self, count: usize) -> Option<Self::Item> {
1060 pass!(self.nth_back(count))
1061 }
1062 }
1063 impl ExactSizeIterator for $t<'_> {
1064 fn len(&self) -> usize {
1065 match self {
1066 Self::Bytes(e) => e.len(),
1067 Self::Expr(e) => e.len(),
1068 Self::Lazy(e) => e.len(),
1069 Self::Eager(e) => e.len(),
1070 Self::Range(e) => e.len(),
1071 Self::Slice(e) => e.len(),
1072 Self::Extended(e) => {
1073 e.size_hint().1.expect("overflow is checked in constructor")
1074 }
1075 Self::Reverse(e) => e.len(),
1076 Self::Mapped(e) => e.len(),
1077 Self::Repeated(e) => e.len(),
1078 }
1079 }
1080 }
1081 };
1082}
1083
1084#[cfg(feature = "nightly")]
1085impl_iter_enum!(UnknownArrayIter => Iter);
1086#[cfg(feature = "nightly")]
1087impl_iter!(UnknownArrayIter => Result<Val>);
1088#[cfg(feature = "nightly")]
1089impl_iter_enum!(UnknownArrayIterLazy => IterLazy);
1090#[cfg(feature = "nightly")]
1091impl_iter!(UnknownArrayIterLazy => Thunk<Val>);
1092#[cfg(feature = "nightly")]
1093impl_iter_enum!(UnknownArrayIterCheap => IterCheap);
1094#[cfg(feature = "nightly")]
1095impl_iter!(UnknownArrayIterCheap => Val);
1096629