git.delta.rocks / jrsonnet / refs/commits / 62ffdd8c4879

difftreelog

refactor more array specializations

Yaroslav Bolyukin2022-12-03parent: #aab1050.patch.diff
in: master

16 files changed

modifiedbindings/jsonnet/src/val_make.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -46,7 +46,7 @@
 /// Assign elements with [`jsonnet_json_array_append`].
 #[no_mangle]
 pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {
-	Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))
+	Box::into_raw(Box::new(Val::Arr(ArrValue::eager(Cc::new(Vec::new())))))
 }
 
 /// Make a `JsonnetJsonValue` representing an object.
modifiedbindings/jsonnet/src/val_modify.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -25,7 +25,7 @@
 			}
 
 			new.push(Thunk::evaluated(val.clone()));
-			*arr = Val::Arr(ArrValue::Lazy(Cc::new(new)));
+			*arr = Val::Arr(ArrValue::lazy(Cc::new(new)));
 		}
 		_ => panic!("should receive array"),
 	}
modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -50,7 +50,7 @@
 	/// Preserve order in object manifestification
 	#[cfg(feature = "exp-preserve-order")]
 	#[clap(long)]
-	preserve_order: bool,
+	pub preserve_order: bool,
 }
 impl ConfigureState for ManifestOpts {
 	type Guards = Box<dyn ManifestFormat>;
addedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -0,0 +1,222 @@
+use jrsonnet_gcmodule::{Cc, Trace};
+use jrsonnet_interner::IBytes;
+use jrsonnet_parser::LocExpr;
+
+use crate::{function::FuncVal, Context, Result, Thunk, Val};
+
+mod spec;
+use spec::*;
+
+/// Represents a Jsonnet array value.
+#[derive(Debug, Clone, Trace)]
+// may contrain other ArrValue
+#[trace(tracking(force))]
+pub enum ArrValue {
+	/// Layout optimized byte array.
+	Bytes(BytesArray),
+	/// Every element is lazy evaluated.
+	Lazy(LazyArray),
+	/// Every element is defined somewhere in source code
+	Expr(ExprArray),
+	/// Every field is already evaluated.
+	Eager(EagerArray),
+	/// Concatenation of two arrays of any kind.
+	Extended(Cc<ExtendedArray>),
+	/// Represents a integer array in form `[start, start + 1, ... end - 1, end]`.
+	/// This kind of arrays is generated by `std.range(start, end)` call, and used for loops.
+	Range(RangeArray),
+	/// Sliced array view.
+	Slice(Box<SliceArray>),
+	/// Reversed array view.
+	/// Returned by `std.reverse(other)` call
+	Reverse(Box<ReverseArray>),
+	/// Returned by `std.map` call
+	Mapped(MappedArray),
+}
+
+impl ArrValue {
+	pub fn empty() -> Self {
+		Self::Range(RangeArray::empty())
+	}
+
+	pub fn expr(ctx: Context, exprs: impl IntoIterator<Item = LocExpr>) -> Self {
+		Self::Expr(ExprArray::new(ctx, exprs))
+	}
+
+	pub fn lazy(thunks: Cc<Vec<Thunk<Val>>>) -> Self {
+		Self::Lazy(LazyArray(thunks))
+	}
+
+	pub fn eager(values: Cc<Vec<Val>>) -> Self {
+		Self::Eager(EagerArray(values))
+	}
+
+	pub fn bytes(bytes: IBytes) -> Self {
+		Self::Bytes(BytesArray(bytes))
+	}
+
+	#[must_use]
+	pub fn map(self, mapper: FuncVal) -> Self {
+		Self::Mapped(MappedArray::new(self, mapper))
+	}
+
+	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
+		// TODO: ArrValue::Picked(inner, indexes) for large arrays
+		let mut out = Vec::new();
+		for i in self.iter() {
+			let i = i?;
+			if filter(&i)? {
+				out.push(i);
+			};
+		}
+		Ok(Self::eager(Cc::new(out)))
+	}
+
+	pub fn extended(a: ArrValue, b: ArrValue) -> Self {
+		// TODO: benchmark for an optimal value, currently just a arbitrary choice
+		const ARR_EXTEND_THRESHOLD: usize = 100;
+
+		if a.len() + b.len() > ARR_EXTEND_THRESHOLD {
+			Self::Extended(Cc::new(ExtendedArray::new(a, b)))
+		} else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {
+			let mut out = Vec::with_capacity(a.len() + b.len());
+			out.extend(a);
+			out.extend(b);
+			Self::eager(Cc::new(out))
+		} else {
+			let mut out = Vec::with_capacity(a.len() + b.len());
+			out.extend(a.iter_lazy());
+			out.extend(b.iter_lazy());
+			Self::lazy(Cc::new(out))
+		}
+	}
+
+	pub fn range_exclusive(a: i32, b: i32) -> Self {
+		Self::Range(RangeArray::new_exclusive(a, b))
+	}
+	pub fn range_inclusive(a: i32, b: i32) -> Self {
+		Self::Range(RangeArray::new_inclusive(a, b))
+	}
+
+	#[must_use]
+	pub fn slice(
+		self,
+		from: Option<usize>,
+		to: Option<usize>,
+		step: Option<usize>,
+	) -> Option<Self> {
+		let len = self.len();
+		let from = from.unwrap_or(0);
+		let to = to.unwrap_or(len).min(len);
+		let step = step.unwrap_or(1);
+		if from >= to || step == 0 {
+			return None;
+		}
+
+		Some(Self::Slice(Box::new(SliceArray {
+			inner: self,
+			from: from as u32,
+			to: to as u32,
+			step: step as u32,
+		})))
+	}
+
+	/// Array length.
+	pub fn len(&self) -> usize {
+		pass!(self.len())
+	}
+
+	/// Is array contains no elements?
+	pub fn is_empty(&self) -> bool {
+		pass!(self.is_empty())
+	}
+
+	/// Get array element by index, evaluating it, if it is lazy.
+	///
+	/// Returns `None` on out-of-bounds condition.
+	pub fn get(&self, index: usize) -> Result<Option<Val>> {
+		pass!(self.get(index))
+	}
+
+	/// Returns None if get is either non cheap, or out of bounds
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		pass!(self.get_cheap(index))
+	}
+
+	/// Get array element by index, without evaluation.
+	///
+	/// Returns `None` on out-of-bounds condition.
+	pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		pass!(self.get_lazy(index))
+	}
+
+	/// Evaluate all array elements, returning new array.
+	pub fn evaluatedcc(&self) -> Result<Cc<Vec<Val>>> {
+		self.evaluated().map(Cc::new)
+	}
+	pub fn evaluated(&self) -> Result<Vec<Val>> {
+		pass!(self.evaluated())
+	}
+
+	/// Iterate over elements, evaluating them.
+	pub fn iter(&self) -> UnknownArrayIter<'_> {
+		pass_iter_call!(self.iter => UnknownArrayIter)
+	}
+
+	/// Iterate over elements, returning lazy values.
+	pub fn iter_lazy(&self) -> UnknownArrayIterLazy<'_> {
+		pass_iter_call!(self.iter_lazy => UnknownArrayIterLazy)
+	}
+
+	pub fn iter_cheap(&self) -> Option<UnknownArrayIterCheap<'_>> {
+		macro_rules! question {
+			($v:expr) => {
+				$v?
+			};
+		}
+		Some(pass_iter_call!(self.iter_cheap in question => UnknownArrayIterCheap))
+	}
+
+	/// Return a reversed view on current array.
+	#[must_use]
+	pub fn reversed(self) -> Self {
+		Self::Reverse(Box::new(ReverseArray(self)))
+	}
+
+	pub fn ptr_eq(a: &Self, b: &Self) -> bool {
+		match (a, b) {
+			(ArrValue::Bytes(a), ArrValue::Bytes(b)) => a.0 == b.0,
+			(ArrValue::Lazy(a), ArrValue::Lazy(b)) => Cc::ptr_eq(&a.0, &b.0),
+			(ArrValue::Expr(a), ArrValue::Expr(b)) => Cc::ptr_eq(&a.0, &b.0),
+			(ArrValue::Eager(a), ArrValue::Eager(b)) => Cc::ptr_eq(&a.0, &b.0),
+			(ArrValue::Extended(a), ArrValue::Extended(b)) => Cc::ptr_eq(&a, &b),
+			(ArrValue::Range(a), ArrValue::Range(b)) => a == b,
+			(ArrValue::Slice(_), ArrValue::Slice(_)) => false,
+			(ArrValue::Reverse(_), ArrValue::Reverse(_)) => false,
+			_ => false,
+		}
+	}
+
+	pub fn is_cheap(&self) -> bool {
+		match self {
+			ArrValue::Eager(_) | ArrValue::Range(..) | ArrValue::Bytes(_) => true,
+			ArrValue::Extended(v) => v.a.is_cheap() && v.b.is_cheap(),
+			ArrValue::Slice(r) => r.inner.is_cheap(),
+			ArrValue::Reverse(i) => i.0.is_cheap(),
+			ArrValue::Expr(_) | ArrValue::Lazy(_) | ArrValue::Mapped(_) => false,
+		}
+	}
+}
+impl From<Vec<Val>> for ArrValue {
+	fn from(value: Vec<Val>) -> Self {
+		Self::eager(Cc::new(value))
+	}
+}
+impl From<Vec<Thunk<Val>>> for ArrValue {
+	fn from(value: Vec<Thunk<Val>>) -> Self {
+		Self::lazy(Cc::new(value))
+	}
+}
+
+#[cfg(target_pointer_width = "64")]
+static_assertions::assert_eq_size!(ArrValue, [u8; 16]);
addedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -0,0 +1,841 @@
+use std::{
+	cell::RefCell,
+	iter::{self, Rev},
+	mem::replace,
+};
+
+use jrsonnet_gcmodule::{Cc, Trace};
+use jrsonnet_interner::IBytes;
+use jrsonnet_parser::LocExpr;
+
+use super::ArrValue;
+use crate::{
+	error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, tb, typed::Any,
+	val::ThunkValue, Context, Error, Result, Thunk, Val,
+};
+
+pub trait ArrayLike {
+	type Iter<'t>
+	where
+		Self: 't;
+	type IterLazy<'t>
+	where
+		Self: 't;
+	type IterCheap<'t>
+	where
+		Self: 't;
+
+	fn len(&self) -> usize;
+	fn is_empty(&self) -> bool {
+		self.len() == 0
+	}
+	fn get(&self, index: usize) -> Result<Option<Val>>;
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>>;
+	fn get_cheap(&self, index: usize) -> Option<Val>;
+	fn evaluated(&self) -> Result<Vec<Val>>;
+	#[allow(clippy::iter_not_returning_iterator)]
+	fn iter(&self) -> Self::Iter<'_>;
+	fn iter_lazy(&self) -> Self::IterLazy<'_>;
+	fn iter_cheap(&self) -> Option<Self::IterCheap<'_>>;
+}
+
+#[derive(Debug, Clone, Trace)]
+pub struct SliceArray {
+	pub(crate) inner: ArrValue,
+	pub(crate) from: u32,
+	pub(crate) to: u32,
+	pub(crate) step: u32,
+}
+type SliceArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+type SliceArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+type SliceArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
+impl ArrayLike for SliceArray {
+	type Iter<'t> = SliceArrayIter<'t>;
+
+	type IterLazy<'t> = SliceArrayLazyIter<'t>;
+
+	type IterCheap<'t> = SliceArrayCheapIter<'t>;
+
+	fn len(&self) -> usize {
+		iter::repeat(())
+			.take((self.to - self.from) as usize)
+			.step_by(self.step as usize)
+			.count()
+	}
+
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		self.iter().nth(index).transpose()
+	}
+
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		self.iter_lazy().nth(index)
+	}
+
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		self.iter_cheap()?.nth(index)
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		self.iter().collect()
+	}
+
+	fn iter(&self) -> SliceArrayIter<'_> {
+		self.inner
+			.iter()
+			.skip(self.from as usize)
+			.take((self.to - self.from) as usize)
+			.step_by(self.step as usize)
+	}
+
+	fn iter_lazy(&self) -> SliceArrayLazyIter<'_> {
+		self.inner
+			.iter_lazy()
+			.skip(self.from as usize)
+			.take((self.to - self.from) as usize)
+			.step_by(self.step as usize)
+	}
+
+	fn iter_cheap(&self) -> Option<SliceArrayCheapIter<'_>> {
+		Some(
+			self.inner
+				.iter_cheap()?
+				.skip(self.from as usize)
+				.take((self.to - self.from) as usize)
+				.step_by(self.step as usize),
+		)
+	}
+}
+
+#[derive(Trace, Debug, Clone)]
+pub struct BytesArray(pub IBytes);
+type BytesArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+type BytesArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+type BytesArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
+impl ArrayLike for BytesArray {
+	type Iter<'t> = BytesArrayIter<'t>;
+
+	type IterLazy<'t> = BytesArrayLazyIter<'t>;
+
+	type IterCheap<'t> = BytesArrayCheapIter<'t>;
+
+	fn len(&self) -> usize {
+		self.0.len()
+	}
+
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		Ok(self.get_cheap(index))
+	}
+
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		self.get_cheap(index).map(Thunk::evaluated)
+	}
+
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		self.0.get(index).map(|v| Val::Num(f64::from(*v)))
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		self.iter().collect()
+	}
+
+	fn iter(&self) -> BytesArrayIter<'_> {
+		self.0.iter().map(|v| Ok(Val::Num(f64::from(*v))))
+	}
+
+	fn iter_lazy(&self) -> BytesArrayLazyIter<'_> {
+		self.0
+			.iter()
+			.map(|v| Thunk::evaluated(Val::Num(f64::from(*v))))
+	}
+
+	fn iter_cheap(&self) -> Option<BytesArrayCheapIter<'_>> {
+		Some(self.0.iter().map(|v| Val::Num(f64::from(*v))))
+	}
+}
+
+#[derive(Debug, Trace, Clone)]
+enum ArrayThunk<T: 'static + Trace> {
+	Computed(Val),
+	Errored(Error),
+	Waiting(T),
+	Pending,
+}
+
+#[derive(Debug, Trace)]
+pub struct ExprArrayInner {
+	ctx: Context,
+	cached: RefCell<Vec<ArrayThunk<LocExpr>>>,
+}
+#[derive(Debug, Trace, Clone)]
+pub struct ExprArray(pub Cc<ExprArrayInner>);
+type ExprArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+type ExprArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+type ExprArrayCheapIter<'t> = iter::Empty<Val>;
+impl ExprArray {
+	pub fn new(ctx: Context, items: impl IntoIterator<Item = LocExpr>) -> Self {
+		Self(Cc::new(ExprArrayInner {
+			ctx,
+			cached: RefCell::new(items.into_iter().map(ArrayThunk::Waiting).collect()),
+		}))
+	}
+}
+impl ArrayLike for ExprArray {
+	type Iter<'t> = ExprArrayIter<'t>;
+
+	type IterLazy<'t> = ExprArrayLazyIter<'t>;
+
+	type IterCheap<'t> = ExprArrayCheapIter<'t>;
+
+	fn len(&self) -> usize {
+		self.0.cached.borrow().len()
+	}
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		if index >= self.len() {
+			return Ok(None);
+		}
+		match &self.0.cached.borrow()[index] {
+			ArrayThunk::Computed(c) => return Ok(Some(c.clone())),
+			ArrayThunk::Errored(e) => return Err(e.clone()),
+			ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),
+			ArrayThunk::Waiting(..) => {}
+		};
+
+		let ArrayThunk::Waiting(expr) = replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending) else {
+			unreachable!()
+		};
+
+		let new_value = match evaluate(self.0.ctx.clone(), &expr) {
+			Ok(v) => v,
+			Err(e) => {
+				self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());
+				return Err(e);
+			}
+		};
+		self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());
+		Ok(Some(new_value))
+	}
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		#[derive(Trace)]
+		struct ArrayElement {
+			arr_thunk: ExprArray,
+			index: usize,
+		}
+
+		impl ThunkValue for ArrayElement {
+			type Output = Val;
+
+			fn get(self: Box<Self>) -> Result<Self::Output> {
+				self.arr_thunk
+					.get(self.index)
+					.transpose()
+					.expect("index checked")
+			}
+		}
+
+		if index >= self.len() {
+			return None;
+		}
+		match &self.0.cached.borrow()[index] {
+			ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),
+			ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),
+			ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}
+		};
+
+		Some(Thunk::new(tb!(ArrayElement {
+			arr_thunk: self.clone(),
+			index,
+		})))
+	}
+	fn get_cheap(&self, _index: usize) -> Option<Val> {
+		None
+	}
+
+	fn iter(&self) -> ExprArrayIter<'_> {
+		(0..self.len()).map(|i| self.get(i).transpose().expect("index checked"))
+	}
+	fn iter_lazy(&self) -> ExprArrayLazyIter<'_> {
+		(0..self.len()).map(|i| self.get_lazy(i).expect("index checked"))
+	}
+	fn iter_cheap(&self) -> Option<ExprArrayCheapIter<'_>> {
+		None
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		self.iter().collect()
+	}
+}
+
+#[derive(Trace, Debug, Clone)]
+pub struct ExtendedArray {
+	pub a: ArrValue,
+	pub b: ArrValue,
+	split: usize,
+	len: usize,
+}
+type ExtendedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + 't;
+type ExtendedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + 't;
+type ExtendedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + 't;
+impl ExtendedArray {
+	pub fn new(a: ArrValue, b: ArrValue) -> Self {
+		let a_len = a.len();
+		let b_len = b.len();
+		Self {
+			a,
+			b,
+			split: a_len,
+			len: a_len.checked_add(b_len).expect("too large array value"),
+		}
+	}
+}
+impl ArrayLike for ExtendedArray {
+	type Iter<'t> = ExtendedArrayIter<'t>;
+
+	type IterLazy<'t> = ExtendedArrayLazyIter<'t>;
+
+	type IterCheap<'t> = ExtendedArrayCheapIter<'t>;
+
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		if self.split > index {
+			self.a.get(index)
+		} else {
+			self.b.get(index - self.split)
+		}
+	}
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		if self.split > index {
+			self.a.get_lazy(index)
+		} else {
+			self.b.get_lazy(index - self.split)
+		}
+	}
+
+	fn len(&self) -> usize {
+		self.len
+	}
+
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		if self.split > index {
+			self.a.get_cheap(index)
+		} else {
+			self.b.get_cheap(index - self.split)
+		}
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		let mut out = self.a.evaluated()?;
+		out.extend(self.b.evaluated()?.into_iter());
+		Ok(out)
+	}
+
+	fn iter(&self) -> ExtendedArrayIter<'_> {
+		self.a.iter().chain(self.b.iter())
+	}
+	fn iter_lazy(&self) -> ExtendedArrayLazyIter<'_> {
+		self.a.iter_lazy().chain(self.b.iter_lazy())
+	}
+	fn iter_cheap(&self) -> Option<ExtendedArrayCheapIter<'_>> {
+		let a = self.a.iter_cheap()?;
+		let b = self.b.iter_cheap()?;
+		Some(a.chain(b))
+	}
+}
+
+#[derive(Trace, Debug, Clone)]
+pub struct LazyArray(pub Cc<Vec<Thunk<Val>>>);
+type LazyArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+type LazyArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+type LazyArrayCheapIter<'t> = iter::Empty<Val>;
+impl ArrayLike for LazyArray {
+	type Iter<'t> = LazyArrayIter<'t>;
+
+	type IterLazy<'t> = LazyArrayLazyIter<'t>;
+
+	type IterCheap<'t> = LazyArrayCheapIter<'t>;
+
+	fn len(&self) -> usize {
+		self.0.len()
+	}
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		let Some(v) = self.0.get(index) else {
+			return Ok(None);
+		};
+		v.evaluate().map(Some)
+	}
+	fn get_cheap(&self, _index: usize) -> Option<Val> {
+		None
+	}
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		self.0.get(index).cloned()
+	}
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		let mut out = Vec::with_capacity(self.len());
+		for i in self.0.iter() {
+			out.push(i.evaluate()?);
+		}
+		Ok(out)
+	}
+	fn iter(&self) -> LazyArrayIter<'_> {
+		self.0.iter().map(Thunk::evaluate)
+	}
+	fn iter_lazy(&self) -> LazyArrayLazyIter<'_> {
+		self.0.iter().cloned()
+	}
+	fn iter_cheap(&self) -> Option<LazyArrayCheapIter<'_>> {
+		None
+	}
+}
+
+#[derive(Trace, Debug, Clone)]
+pub struct EagerArray(pub Cc<Vec<Val>>);
+type EagerArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+type EagerArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+type EagerArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
+impl ArrayLike for EagerArray {
+	type Iter<'t> = EagerArrayIter<'t>;
+
+	type IterLazy<'t> = EagerArrayLazyIter<'t>;
+
+	type IterCheap<'t> = EagerArrayCheapIter<'t>;
+
+	fn len(&self) -> usize {
+		self.0.len()
+	}
+
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		Ok(self.0.get(index).cloned())
+	}
+
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		self.0.get(index).cloned().map(Thunk::evaluated)
+	}
+
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		self.0.get(index).cloned()
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		Ok((*self.0).clone())
+	}
+
+	fn iter(&self) -> EagerArrayIter<'_> {
+		self.0.iter().cloned().map(Ok)
+	}
+
+	fn iter_lazy(&self) -> EagerArrayLazyIter<'_> {
+		self.0.iter().cloned().map(Thunk::evaluated)
+	}
+
+	fn iter_cheap(&self) -> Option<EagerArrayCheapIter<'_>> {
+		Some(self.0.iter().cloned())
+	}
+}
+
+/// Inclusive range type
+#[derive(Debug, Trace, Clone, PartialEq, Eq)]
+pub struct RangeArray {
+	start: i32,
+	end: i32,
+}
+struct RangeIter {
+	start: i32,
+	end: i32,
+}
+impl RangeIter {
+	fn finished(&self) -> bool {
+		self.end < self.start
+	}
+	fn finish(&mut self) {
+		self.start = 0;
+		self.end = -1;
+	}
+}
+impl Iterator for RangeIter {
+	type Item = i32;
+
+	fn next(&mut self) -> Option<Self::Item> {
+		if self.finished() {
+			return None;
+		}
+		let v = self.start;
+		if v == self.end {
+			self.finish();
+		} else {
+			self.start = v + 1;
+		}
+		Some(v)
+	}
+	fn nth(&mut self, n: usize) -> Option<Self::Item> {
+		let v = (self.start as usize) + n;
+		if v > self.end as usize {
+			self.finish();
+			None
+		} else {
+			self.start = v as i32;
+			self.next()
+		}
+	}
+	fn size_hint(&self) -> (usize, Option<usize>) {
+		let len = self.len();
+		(len, Some(len))
+	}
+}
+impl DoubleEndedIterator for RangeIter {
+	fn next_back(&mut self) -> Option<Self::Item> {
+		if self.finished() {
+			return None;
+		}
+		let v = self.end;
+		if v == self.start {
+			self.finish();
+		} else {
+			self.end = v - 1;
+		}
+		Some(v)
+	}
+	fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
+		let v = (self.end as usize) - n;
+		if v < self.start as usize {
+			self.finish();
+			None
+		} else {
+			self.end = v as i32;
+			self.next_back()
+		}
+	}
+}
+impl ExactSizeIterator for RangeIter {
+	fn len(&self) -> usize {
+		if self.finished() {
+			0
+		} else {
+			(self.end as isize - self.start as isize + 1) as usize
+		}
+	}
+}
+impl RangeArray {
+	pub fn empty() -> Self {
+		Self::new_exclusive(0, 0)
+	}
+	pub fn new_exclusive(start: i32, end: i32) -> Self {
+		end.checked_sub(1)
+			.map_or_else(Self::empty, |end| Self { start, end })
+	}
+	pub fn new_inclusive(start: i32, end: i32) -> Self {
+		Self { start, end }
+	}
+	fn range(&self) -> RangeIter {
+		RangeIter {
+			start: self.start,
+			end: self.end,
+		}
+	}
+}
+
+type RangeArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+type RangeArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+type RangeArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
+impl ArrayLike for RangeArray {
+	type Iter<'t> = RangeArrayIter<'t>;
+
+	type IterLazy<'t> = RangeArrayLazyIter<'t>;
+
+	type IterCheap<'t> = RangeArrayCheapIter<'t>;
+
+	fn len(&self) -> usize {
+		self.range().len()
+	}
+	fn is_empty(&self) -> bool {
+		self.range().finished()
+	}
+
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		Ok(self.get_cheap(index))
+	}
+
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		self.get_cheap(index).map(Thunk::evaluated)
+	}
+
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		self.range().nth(index).map(|i| Val::Num(f64::from(i)))
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		Ok(self.range().map(|i| Val::Num(f64::from(i))).collect())
+	}
+
+	fn iter(&self) -> RangeArrayIter<'_> {
+		self.range().map(|i| Ok(Val::Num(f64::from(i))))
+	}
+
+	fn iter_lazy(&self) -> RangeArrayLazyIter<'_> {
+		self.range()
+			.map(|i| Thunk::evaluated(Val::Num(f64::from(i))))
+	}
+
+	fn iter_cheap(&self) -> Option<RangeArrayCheapIter<'_>> {
+		Some(self.range().map(|i| Val::Num(f64::from(i))))
+	}
+}
+
+#[derive(Debug, Trace, Clone)]
+pub struct ReverseArray(pub ArrValue);
+impl ArrayLike for ReverseArray {
+	type Iter<'t> = Rev<UnknownArrayIter<'t>>;
+
+	type IterLazy<'t> = Rev<UnknownArrayIterLazy<'t>>;
+
+	type IterCheap<'t> = Rev<UnknownArrayIterCheap<'t>>;
+
+	fn len(&self) -> usize {
+		self.0.len()
+	}
+
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		self.0.get(self.0.len() - index - 1)
+	}
+
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		self.0.get_lazy(self.0.len() - index - 1)
+	}
+
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		self.0.get_cheap(self.0.len() - index - 1)
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		let mut v = self.0.evaluated()?;
+		v.reverse();
+		Ok(v)
+	}
+
+	fn iter(&self) -> Rev<UnknownArrayIter<'_>> {
+		self.0.iter().rev()
+	}
+
+	fn iter_lazy(&self) -> Rev<UnknownArrayIterLazy<'_>> {
+		self.0.iter_lazy().rev()
+	}
+
+	fn iter_cheap(&self) -> Option<Rev<UnknownArrayIterCheap<'_>>> {
+		Some(self.0.iter_cheap()?.rev())
+	}
+}
+
+#[derive(Trace, Debug)]
+pub struct MappedArrayInner {
+	inner: ArrValue,
+	cached: RefCell<Vec<ArrayThunk<()>>>,
+	mapper: FuncVal,
+}
+#[derive(Trace, Debug, Clone)]
+pub struct MappedArray(Cc<MappedArrayInner>);
+impl MappedArray {
+	pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {
+		let len = inner.len();
+		Self(Cc::new(MappedArrayInner {
+			inner,
+			cached: RefCell::new(vec![ArrayThunk::Waiting(()); len]),
+			mapper,
+		}))
+	}
+}
+type MappedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+type MappedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+type MappedArrayCheapIter<'t> = iter::Empty<Val>;
+impl ArrayLike for MappedArray {
+	type Iter<'t> = MappedArrayIter<'t>;
+	type IterLazy<'t> = MappedArrayLazyIter<'t>;
+	type IterCheap<'t> = MappedArrayCheapIter<'t>;
+
+	fn len(&self) -> usize {
+		self.0.cached.borrow().len()
+	}
+
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		if index >= self.len() {
+			return Ok(None);
+		}
+		match &self.0.cached.borrow()[index] {
+			ArrayThunk::Computed(c) => return Ok(Some(c.clone())),
+			ArrayThunk::Errored(e) => return Err(e.clone()),
+			ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),
+			ArrayThunk::Waiting(..) => {}
+		};
+
+		let ArrayThunk::Waiting(_) = replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending) else {
+			unreachable!()
+		};
+
+		let val = self
+			.0
+			.inner
+			.get(index)
+			.transpose()
+			.expect("index checked")
+			.and_then(|r| self.0.mapper.evaluate_simple(&(Any(r),)));
+
+		let new_value = match val {
+			Ok(v) => v,
+			Err(e) => {
+				self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());
+				return Err(e);
+			}
+		};
+		self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());
+		Ok(Some(new_value))
+	}
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		#[derive(Trace)]
+		struct ArrayElement {
+			arr_thunk: MappedArray,
+			index: usize,
+		}
+
+		impl ThunkValue for ArrayElement {
+			type Output = Val;
+
+			fn get(self: Box<Self>) -> Result<Self::Output> {
+				self.arr_thunk
+					.get(self.index)
+					.transpose()
+					.expect("index checked")
+			}
+		}
+
+		if index >= self.len() {
+			return None;
+		}
+		match &self.0.cached.borrow()[index] {
+			ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),
+			ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),
+			ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}
+		};
+
+		Some(Thunk::new(tb!(ArrayElement {
+			arr_thunk: self.clone(),
+			index,
+		})))
+	}
+
+	fn get_cheap(&self, _index: usize) -> Option<Val> {
+		None
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		self.iter().collect()
+	}
+
+	fn iter(&self) -> MappedArrayIter<'_> {
+		(0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))
+	}
+
+	fn iter_lazy(&self) -> MappedArrayLazyIter<'_> {
+		(0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))
+	}
+
+	fn iter_cheap(&self) -> Option<Self::IterCheap<'_>> {
+		None
+	}
+}
+// impl MappedArray
+
+macro_rules! impl_iter_enum {
+	($n:ident => $v:ident) => {
+		pub enum $n<'t> {
+			Bytes(<BytesArray as ArrayLike>::$v<'t>),
+			Expr(<ExprArray as ArrayLike>::$v<'t>),
+			Lazy(<LazyArray as ArrayLike>::$v<'t>),
+			Eager(<EagerArray as ArrayLike>::$v<'t>),
+			Range(<RangeArray as ArrayLike>::$v<'t>),
+			Slice(Box<<SliceArray as ArrayLike>::$v<'t>>),
+			Extended(Box<<ExtendedArray as ArrayLike>::$v<'t>>),
+			Reverse(Box<<ReverseArray as ArrayLike>::$v<'t>>),
+			Mapped(Box<<MappedArray as ArrayLike>::$v<'t>>),
+		}
+	};
+}
+
+macro_rules! pass {
+	($t:ident.$m:ident($($ident:ident),*)) => {
+		match $t {
+			Self::Bytes(e) => e.$m($($ident)*),
+			Self::Expr(e) => e.$m($($ident)*),
+			Self::Lazy(e) => e.$m($($ident)*),
+			Self::Eager(e) => e.$m($($ident)*),
+			Self::Range(e) => e.$m($($ident)*),
+			Self::Slice(e) => e.$m($($ident)*),
+			Self::Extended(e) => e.$m($($ident)*),
+			Self::Reverse(e) => e.$m($($ident)*),
+			Self::Mapped(e) => e.$m($($ident)*),
+		}
+	};
+}
+pub(super) use pass;
+
+macro_rules! pass_iter_call {
+	($t:ident.$c:ident $(in $wrap:ident)? => $e:ident) => {
+		match $t {
+			ArrValue::Bytes(e) => $e::Bytes($($wrap!)?(e.$c())),
+			ArrValue::Lazy(e) => $e::Lazy($($wrap!)?(e.$c())),
+			ArrValue::Expr(e) => $e::Expr($($wrap!)?(e.$c())),
+			ArrValue::Eager(e) => $e::Eager($($wrap!)?(e.$c())),
+			ArrValue::Range(e) => $e::Range($($wrap!)?(e.$c())),
+			ArrValue::Slice(e) => $e::Slice(Box::new($($wrap!)?(e.$c()))),
+			ArrValue::Extended(e) => $e::Extended(Box::new($($wrap!)?(e.$c()))),
+			ArrValue::Reverse(e) => $e::Reverse(Box::new($($wrap!)?(e.$c()))),
+			ArrValue::Mapped(e) => $e::Mapped(Box::new($($wrap!)?(e.$c()))),
+		}
+	};
+}
+pub(super) use pass_iter_call;
+
+macro_rules! impl_iter {
+	($t:ident => $out:ty) => {
+		impl Iterator for $t<'_> {
+			type Item = $out;
+
+			fn next(&mut self) -> Option<Self::Item> {
+				pass!(self.next())
+			}
+			fn nth(&mut self, count: usize) -> Option<Self::Item> {
+				pass!(self.nth(count))
+			}
+			fn size_hint(&self) -> (usize, Option<usize>) {
+				pass!(self.size_hint())
+			}
+		}
+		impl DoubleEndedIterator for $t<'_> {
+			fn next_back(&mut self) -> Option<Self::Item> {
+				pass!(self.next_back())
+			}
+			fn nth_back(&mut self, count: usize) -> Option<Self::Item> {
+				pass!(self.nth_back(count))
+			}
+		}
+		impl ExactSizeIterator for $t<'_> {
+			fn len(&self) -> usize {
+				match self {
+					Self::Bytes(e) => e.len(),
+					Self::Expr(e) => e.len(),
+					Self::Lazy(e) => e.len(),
+					Self::Eager(e) => e.len(),
+					Self::Range(e) => e.len(),
+					Self::Slice(e) => e.len(),
+					Self::Extended(e) => {
+						e.size_hint().1.expect("overflow is checked in constructor")
+					}
+					Self::Reverse(e) => e.len(),
+					Self::Mapped(e) => e.len(),
+				}
+			}
+		}
+	};
+}
+
+impl_iter_enum!(UnknownArrayIter => Iter);
+impl_iter_enum!(UnknownArrayIterLazy => IterLazy);
+impl_iter_enum!(UnknownArrayIterCheap => IterCheap);
+impl_iter!(UnknownArrayIter => Result<Val>);
+impl_iter!(UnknownArrayIterLazy => Thunk<Val>);
+impl_iter!(UnknownArrayIterCheap => Val);
modifiedcrates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -32,7 +32,7 @@
 		Destruct::Array { start, rest, end } => {
 			use jrsonnet_parser::DestructRest;
 
-			use crate::val::ArrValue;
+			use crate::arr::ArrValue;
 
 			#[derive(Trace)]
 			struct DataThunk {
@@ -110,7 +110,10 @@
 						fn get(self: Box<Self>) -> Result<Self::Output> {
 							let full = self.full.evaluate()?;
 							let to = full.len() - self.end;
-							Ok(Val::Arr(full.slice(Some(self.start), Some(to), None)))
+							Ok(Val::Arr(
+								full.slice(Some(self.start), Some(to), None)
+									.expect("arguments checked"),
+							))
 						}
 					}
 
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -10,19 +10,53 @@
 
 use self::destructure::destruct;
 use crate::{
+	arr::ArrValue,
 	destructure::evaluate_dest,
 	error::ErrorKind::*,
 	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
 	function::{CallLocation, FuncDesc, FuncVal},
 	tb, throw,
 	typed::Typed,
-	val::{ArrValue, CachedUnbound, IndexableVal, Thunk, ThunkValue},
+	val::{CachedUnbound, IndexableVal, Thunk, ThunkValue},
 	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,
 	Unbound, Val,
 };
 pub mod destructure;
 pub mod operator;
 
+pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {
+	fn is_trivial(expr: &LocExpr) -> bool {
+		match &*expr.0 {
+			Expr::Str(_)
+			| Expr::Num(_)
+			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,
+			Expr::Arr(a) => a.iter().all(is_trivial),
+			Expr::Parened(e) => is_trivial(e),
+			_ => false,
+		}
+	}
+	Some(match &*expr.0 {
+		Expr::Str(s) => Val::Str(s.clone()),
+		Expr::Num(n) => Val::Num(*n),
+		Expr::Literal(LiteralType::False) => Val::Bool(false),
+		Expr::Literal(LiteralType::True) => Val::Bool(true),
+		Expr::Literal(LiteralType::Null) => Val::Null,
+		Expr::Arr(n) => {
+			if n.iter().any(|e| !is_trivial(e)) {
+				return None;
+			}
+			Val::Arr(ArrValue::eager(Cc::new(
+				n.iter()
+					.map(evaluate_trivial)
+					.map(|e| e.expect("checked trivial"))
+					.collect(),
+			)))
+		}
+		Expr::Parened(e) => evaluate_trivial(e)?,
+		_ => return None,
+	})
+}
+
 pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {
 	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {
 		name,
@@ -100,7 +134,7 @@
 
 					let fctx = Pending::new();
 					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());
-					let value = Thunk::evaluated(Val::Arr(ArrValue::Lazy(Cc::new(vec![
+					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![
 						Thunk::evaluated(Val::Str(field.clone())),
 						Thunk::new(tb!(ObjectFieldThunk {
 							field: field.clone(),
@@ -380,6 +414,9 @@
 #[allow(clippy::too_many_lines)]
 pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {
 	use Expr::*;
+	if let Some(trivial) = evaluate_trivial(&expr) {
+		return Ok(trivial);
+	}
 	let LocExpr(expr, loc) = expr;
 	Ok(match &**expr {
 		Literal(LiteralType::This) => {
@@ -507,9 +544,9 @@
 			evaluate(ctx, &returned.clone())?
 		}
 		Arr(items) => {
-			let mut out = Vec::with_capacity(items.len());
-			for item in items {
-				// TODO: Implement ArrValue::Lazy with same context for every element?
+			if items.is_empty() {
+				Val::Arr(ArrValue::empty())
+			} else if items.len() == 1 {
 				#[derive(Trace)]
 				struct ArrayElement {
 					ctx: Context,
@@ -521,12 +558,15 @@
 						evaluate(self.ctx, &self.item)
 					}
 				}
-				out.push(Thunk::new(tb!(ArrayElement {
-					ctx: ctx.clone(),
-					item: item.clone(),
-				})));
+				Val::Arr(ArrValue::lazy(Cc::new(vec![Thunk::new(tb!(
+					ArrayElement {
+						ctx,
+						item: items[0].clone(),
+					}
+				))])))
+			} else {
+				Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))
 			}
-			Val::Arr(out.into())
 		}
 		ArrComp(expr, comp_specs) => {
 			let mut out = Vec::new();
@@ -534,7 +574,7 @@
 				out.push(evaluate(ctx, expr)?);
 				Ok(())
 			})?;
-			Val::Arr(ArrValue::Eager(Cc::new(out)))
+			Val::Arr(ArrValue::eager(Cc::new(out)))
 		}
 		Obj(body) => Val::Obj(evaluate_object(ctx, body)?),
 		ObjExtend(a, b) => evaluate_add_op(
@@ -615,7 +655,7 @@
 					|| s.import_resolved(resolved_path),
 				)?,
 				ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),
-				ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_resolved_bin(resolved_path)?)),
+				ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),
 				_ => unreachable!(),
 			}
 		}
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -3,8 +3,8 @@
 use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
 
 use crate::{
-	error::ErrorKind::*, evaluate, stdlib::std_format, throw, typed::Typed, val::equals, Context,
-	Result, Val,
+	arr::ArrValue, error::ErrorKind::*, evaluate, stdlib::std_format, throw, typed::Typed,
+	val::equals, Context, Result, Val,
 };
 
 pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
@@ -18,6 +18,8 @@
 	})
 }
 
+/// Arbitrary threshold
+
 pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {
 	use Val::*;
 	Ok(match (a, b) {
@@ -34,12 +36,8 @@
 		(o, Str(a)) => Str(format!("{}{a}", o.clone().to_string()?).into()),
 
 		(Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),
-		(Arr(a), Arr(b)) => {
-			let mut out = Vec::with_capacity(a.len() + b.len());
-			out.extend(a.iter_lazy());
-			out.extend(b.iter_lazy());
-			Arr(out.into())
-		}
+		(Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),
+
 		(Num(v1), Num(v2)) => Val::new_checked_num(v1 + v2)?,
 		_ => throw!(BinaryOperatorDoesNotOperateOnValues(
 			BinaryOpType::Add,
@@ -82,7 +80,7 @@
 	})
 }
 
-pub fn evaluate_compare_op(a: &Val, op: BinaryOpType, b: &Val) -> Result<Ordering> {
+pub fn evaluate_compare_op(a: &Val, b: &Val, op: BinaryOpType) -> Result<Ordering> {
 	use Val::*;
 	Ok(match (a, b) {
 		(Str(a), Str(b)) => a.cmp(b),
@@ -92,7 +90,7 @@
 			let bi = b.iter();
 
 			for (a, b) in ai.zip(bi) {
-				let ord = evaluate_compare_op(&a?, op, &b?)?;
+				let ord = evaluate_compare_op(&a?, &b?, op)?;
 				if !ord.is_eq() {
 					return Ok(ord);
 				}
@@ -117,10 +115,10 @@
 		(a, Eq, b) => Bool(equals(a, b)?),
 		(a, Neq, b) => Bool(!equals(a, b)?),
 
-		(a, Lt, b) => Bool(evaluate_compare_op(a, Lt, b)?.is_lt()),
-		(a, Gt, b) => Bool(evaluate_compare_op(a, Gt, b)?.is_gt()),
-		(a, Lte, b) => Bool(evaluate_compare_op(a, Lte, b)?.is_le()),
-		(a, Gte, b) => Bool(evaluate_compare_op(a, Gte, b)?.is_ge()),
+		(a, Lt, b) => Bool(evaluate_compare_op(a, b, Lt)?.is_lt()),
+		(a, Gt, b) => Bool(evaluate_compare_op(a, b, Gt)?.is_gt()),
+		(a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),
+		(a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),
 
 		(Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
 		(a, Mod, b) => evaluate_mod_op(a, b)?,
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -7,7 +7,7 @@
 	Deserialize, Serialize,
 };
 
-use crate::{error::Result, val::ArrValue, ObjValueBuilder, State, Val};
+use crate::{arr::ArrValue, error::Result, ObjValueBuilder, State, Val};
 
 impl<'de> Deserialize<'de> for Val {
 	fn deserialize<D>(deserializer: D) -> Result<Val, D::Error>
@@ -77,7 +77,7 @@
 			where
 				E: serde::de::Error,
 			{
-				Ok(Val::Arr(ArrValue::Bytes(v.into())))
+				Ok(Val::Arr(ArrValue::bytes(v.into())))
 			}
 
 			fn visit_none<E>(self) -> Result<Self::Value, E>
@@ -117,7 +117,7 @@
 					out.push(val);
 				}
 
-				Ok(Val::Arr(ArrValue::Eager(Cc::new(out))))
+				Ok(Val::Arr(ArrValue::eager(Cc::new(out))))
 			}
 
 			fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,5 +1,6 @@
 //! jsonnet interpreter implementation
 #![cfg_attr(feature = "nightly", feature(thread_local))]
+#![feature(type_alias_impl_trait)]
 #![deny(unsafe_op_in_unsafe_fn)]
 #![warn(
 	clippy::all,
@@ -43,6 +44,7 @@
 // For jrsonnet-macros
 extern crate self as jrsonnet_evaluator;
 
+mod arr;
 mod ctx;
 mod dynamic;
 pub mod error;
modifiedcrates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -14,7 +14,7 @@
 		|| format!("std.format of {str}"),
 		|| {
 			Ok(match vals {
-				Val::Arr(vals) => format_arr(&str, &vals.evaluated()?)?,
+				Val::Arr(vals) => format_arr(&str, &vals.evaluatedcc()?)?,
 				Val::Obj(obj) => format_obj(&str, &obj)?,
 				o => format_arr(&str, &[o])?,
 			})
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/typed/conversions.rs
1use std::ops::Deref;23use jrsonnet_gcmodule::Cc;4use jrsonnet_interner::{IBytes, IStr};5pub use jrsonnet_macros::Typed;6use jrsonnet_types::{ComplexValType, ValType};78use crate::{9	error::Result,10	function::{native::NativeDesc, FuncDesc, FuncVal},11	throw,12	typed::CheckType,13	val::{ArrValue, IndexableVal},14	ObjValue, ObjValueBuilder, Val,15};1617pub trait TypedObj: Typed {18	fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;19	fn parse(obj: &ObjValue) -> Result<Self>;20	fn into_object(self) -> Result<ObjValue> {21		let mut builder = ObjValueBuilder::new();22		self.serialize(&mut builder)?;23		Ok(builder.build())24	}25}2627pub trait Typed: Sized {28	const TYPE: &'static ComplexValType;29	fn into_untyped(typed: Self) -> Result<Val>;30	fn from_untyped(untyped: Val) -> Result<Self>;31}3233macro_rules! impl_int {34	($($ty:ty)*) => {$(35		impl Typed for $ty {36			const TYPE: &'static ComplexValType =37				&ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));38			fn from_untyped(value: Val) -> Result<Self> {39				<Self as Typed>::TYPE.check(&value)?;40				match value {41					Val::Num(n) => {42						#[allow(clippy::float_cmp)]43						if n.trunc() != n {44							throw!(45								"cannot convert number with fractional part to {}",46								stringify!($ty)47							)48						}49						Ok(n as Self)50					}51					_ => unreachable!(),52				}53			}54			fn into_untyped(value: Self) -> Result<Val> {55				Ok(Val::Num(value as f64))56			}57		}58	)*};59}6061impl_int!(i8 u8 i16 u16 i32 u32);6263macro_rules! impl_bounded_int {64	($($name:ident = $ty:ty)*) => {$(65		#[derive(Clone, Copy)]66		pub struct $name<const MIN: $ty, const MAX: $ty>($ty);67		impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {68			pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {69				if value >= MIN && value <= MAX {70					Some(Self(value))71				} else {72					None73				}74			}75			pub const fn value(self) -> $ty {76				self.077			}78		}79		impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {80			type Target = $ty;81			fn deref(&self) -> &Self::Target {82				&self.083			}84		}8586		impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {87			const TYPE: &'static ComplexValType =88				&ComplexValType::BoundedNumber(89					Some(MIN as f64),90					Some(MAX as f64),91				);9293			fn from_untyped(value: Val) -> Result<Self> {94				<Self as Typed>::TYPE.check(&value)?;95				match value {96					Val::Num(n) => {97						#[allow(clippy::float_cmp)]98						if n.trunc() != n {99							throw!(100								"cannot convert number with fractional part to {}",101								stringify!($ty)102							)103						}104						Ok(Self(n as $ty))105					}106					_ => unreachable!(),107				}108			}109110			fn into_untyped(value: Self) -> Result<Val> {111				Ok(Val::Num(value.0 as f64))112			}113		}114	)*};115}116117impl_bounded_int!(118	BoundedI8 = i8119	BoundedI16 = i16120	BoundedI32 = i32121	BoundedI64 = i64122	BoundedUsize = usize123);124125impl Typed for f64 {126	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);127128	fn into_untyped(value: Self) -> Result<Val> {129		Ok(Val::Num(value))130	}131132	fn from_untyped(value: Val) -> Result<Self> {133		<Self as Typed>::TYPE.check(&value)?;134		match value {135			Val::Num(n) => Ok(n),136			_ => unreachable!(),137		}138	}139}140141pub struct PositiveF64(pub f64);142impl Typed for PositiveF64 {143	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);144145	fn into_untyped(value: Self) -> Result<Val> {146		Ok(Val::Num(value.0))147	}148149	fn from_untyped(value: Val) -> Result<Self> {150		<Self as Typed>::TYPE.check(&value)?;151		match value {152			Val::Num(n) => Ok(Self(n)),153			_ => unreachable!(),154		}155	}156}157impl Typed for usize {158	// It is possible to store 54 bits of precision in f64, but leaving u32::MAX here for compatibility159	const TYPE: &'static ComplexValType =160		&ComplexValType::BoundedNumber(Some(0.0), Some(u32::MAX as f64));161162	fn into_untyped(value: Self) -> Result<Val> {163		if value > u32::MAX as Self {164			throw!("number is too large")165		}166		Ok(Val::Num(value as f64))167	}168169	fn from_untyped(value: Val) -> Result<Self> {170		<Self as Typed>::TYPE.check(&value)?;171		match value {172			Val::Num(n) => {173				#[allow(clippy::float_cmp)]174				if n.trunc() != n {175					throw!("cannot convert number with fractional part to usize")176				}177				Ok(n as Self)178			}179			_ => unreachable!(),180		}181	}182}183184impl Typed for IStr {185	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);186187	fn into_untyped(value: Self) -> Result<Val> {188		Ok(Val::Str(value))189	}190191	fn from_untyped(value: Val) -> Result<Self> {192		<Self as Typed>::TYPE.check(&value)?;193		match value {194			Val::Str(s) => Ok(s),195			_ => unreachable!(),196		}197	}198}199200impl Typed for String {201	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);202203	fn into_untyped(value: Self) -> Result<Val> {204		Ok(Val::Str(value.into()))205	}206207	fn from_untyped(value: Val) -> Result<Self> {208		<Self as Typed>::TYPE.check(&value)?;209		match value {210			Val::Str(s) => Ok(s.to_string()),211			_ => unreachable!(),212		}213	}214}215216impl Typed for char {217	const TYPE: &'static ComplexValType = &ComplexValType::Char;218219	fn into_untyped(value: Self) -> Result<Val> {220		Ok(Val::Str(value.to_string().into()))221	}222223	fn from_untyped(value: Val) -> Result<Self> {224		<Self as Typed>::TYPE.check(&value)?;225		match value {226			Val::Str(s) => Ok(s.chars().next().unwrap()),227			_ => unreachable!(),228		}229	}230}231232impl<T> Typed for Vec<T>233where234	T: Typed,235{236	const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);237238	fn into_untyped(value: Self) -> Result<Val> {239		let mut o = Vec::with_capacity(value.len());240		for i in value {241			o.push(T::into_untyped(i)?);242		}243		Ok(Val::Arr(o.into()))244	}245246	fn from_untyped(value: Val) -> Result<Self> {247		<Self as Typed>::TYPE.check(&value)?;248		match value {249			Val::Arr(a) => {250				let mut o = Self::with_capacity(a.len());251				for i in a.iter() {252					o.push(T::from_untyped(i?)?);253				}254				Ok(o)255			}256			_ => unreachable!(),257		}258	}259}260261/// To be used in Vec<Any>262/// Regular Val can't be used here, because it has wrong `TryFrom::Error` type263#[derive(Clone)]264pub struct Any(pub Val);265266impl Typed for Any {267	const TYPE: &'static ComplexValType = &ComplexValType::Any;268269	fn into_untyped(value: Self) -> Result<Val> {270		Ok(value.0)271	}272273	fn from_untyped(value: Val) -> Result<Self> {274		Ok(Self(value))275	}276}277278/// Specialization, provides faster `TryFrom<VecVal>` for Val279pub struct VecVal(pub Cc<Vec<Val>>);280281impl Typed for VecVal {282	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);283284	fn into_untyped(value: Self) -> Result<Val> {285		Ok(Val::Arr(ArrValue::Eager(value.0)))286	}287288	fn from_untyped(value: Val) -> Result<Self> {289		<Self as Typed>::TYPE.check(&value)?;290		match value {291			Val::Arr(a) => Ok(Self(a.evaluated()?)),292			_ => unreachable!(),293		}294	}295}296297/// Specialization298impl Typed for IBytes {299	const TYPE: &'static ComplexValType =300		&ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));301302	fn into_untyped(value: Self) -> Result<Val> {303		Ok(Val::Arr(ArrValue::Bytes(value)))304	}305306	fn from_untyped(value: Val) -> Result<Self> {307		if let Val::Arr(ArrValue::Bytes(bytes)) = value {308			return Ok(bytes);309		}310		<Self as Typed>::TYPE.check(&value)?;311		match value {312			Val::Arr(a) => {313				let mut out = Vec::with_capacity(a.len());314				for e in a.iter() {315					let r = e?;316					out.push(u8::from_untyped(r)?);317				}318				Ok(out.as_slice().into())319			}320			_ => unreachable!(),321		}322	}323}324325pub struct M1;326impl Typed for M1 {327	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));328329	fn into_untyped(_: Self) -> Result<Val> {330		Ok(Val::Num(-1.0))331	}332333	fn from_untyped(value: Val) -> Result<Self> {334		<Self as Typed>::TYPE.check(&value)?;335		Ok(Self)336	}337}338339macro_rules! decl_either {340	($($name: ident, $($id: ident)*);*) => {$(341		#[derive(Clone)]342		pub enum $name<$($id),*> {343			$($id($id)),*344		}345		impl<$($id),*> Typed for $name<$($id),*>346		where347			$($id: Typed,)*348		{349			const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);350351			fn into_untyped(value: Self) -> Result<Val> {352				match value {$(353					$name::$id(v) => $id::into_untyped(v)354				),*}355			}356357			fn from_untyped(value: Val) -> Result<Self> {358				$(359					if $id::TYPE.check(&value).is_ok() {360						$id::from_untyped(value).map(Self::$id)361					} else362				)* {363					<Self as Typed>::TYPE.check(&value)?;364					unreachable!()365				}366			}367		}368	)*}369}370decl_either!(371	Either1, A;372	Either2, A B;373	Either3, A B C;374	Either4, A B C D;375	Either5, A B C D E;376	Either6, A B C D E F;377	Either7, A B C D E F G378);379#[macro_export]380macro_rules! Either {381	($a:ty) => {Either1<$a>};382	($a:ty, $b:ty) => {Either2<$a, $b>};383	($a:ty, $b:ty, $c:ty) => {Either3<$a, $b, $c>};384	($a:ty, $b:ty, $c:ty, $d:ty) => {Either4<$a, $b, $c, $d>};385	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {Either5<$a, $b, $c, $d, $e>};386	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {Either6<$a, $b, $c, $d, $e, $f>};387	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {Either7<$a, $b, $c, $d, $e, $f, $g>};388}389pub use Either;390391pub type MyType = Either![u32, f64, String];392393impl Typed for ArrValue {394	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);395396	fn into_untyped(value: Self) -> Result<Val> {397		Ok(Val::Arr(value))398	}399400	fn from_untyped(value: Val) -> Result<Self> {401		<Self as Typed>::TYPE.check(&value)?;402		match value {403			Val::Arr(a) => Ok(a),404			_ => unreachable!(),405		}406	}407}408409impl Typed for FuncVal {410	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);411412	fn into_untyped(value: Self) -> Result<Val> {413		Ok(Val::Func(value))414	}415416	fn from_untyped(value: Val) -> Result<Self> {417		<Self as Typed>::TYPE.check(&value)?;418		match value {419			Val::Func(a) => Ok(a),420			_ => unreachable!(),421		}422	}423}424425impl Typed for Cc<FuncDesc> {426	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);427428	fn into_untyped(value: Self) -> Result<Val> {429		Ok(Val::Func(FuncVal::Normal(value)))430	}431432	fn from_untyped(value: Val) -> Result<Self> {433		<Self as Typed>::TYPE.check(&value)?;434		match value {435			Val::Func(FuncVal::Normal(desc)) => Ok(desc),436			Val::Func(_) => throw!("expected normal function, not builtin"),437			_ => unreachable!(),438		}439	}440}441442impl Typed for ObjValue {443	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);444445	fn into_untyped(value: Self) -> Result<Val> {446		Ok(Val::Obj(value))447	}448449	fn from_untyped(value: Val) -> Result<Self> {450		<Self as Typed>::TYPE.check(&value)?;451		match value {452			Val::Obj(a) => Ok(a),453			_ => unreachable!(),454		}455	}456}457458impl Typed for bool {459	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);460461	fn into_untyped(value: Self) -> Result<Val> {462		Ok(Val::Bool(value))463	}464465	fn from_untyped(value: Val) -> Result<Self> {466		<Self as Typed>::TYPE.check(&value)?;467		match value {468			Val::Bool(a) => Ok(a),469			_ => unreachable!(),470		}471	}472}473impl Typed for IndexableVal {474	const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[475		&ComplexValType::Simple(ValType::Arr),476		&ComplexValType::Simple(ValType::Str),477	]);478479	fn into_untyped(value: Self) -> Result<Val> {480		match value {481			IndexableVal::Str(s) => Ok(Val::Str(s)),482			IndexableVal::Arr(a) => Ok(Val::Arr(a)),483		}484	}485486	fn from_untyped(value: Val) -> Result<Self> {487		<Self as Typed>::TYPE.check(&value)?;488		value.into_indexable()489	}490}491492pub struct Null;493impl Typed for Null {494	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);495496	fn into_untyped(_: Self) -> Result<Val> {497		Ok(Val::Null)498	}499500	fn from_untyped(value: Val) -> Result<Self> {501		<Self as Typed>::TYPE.check(&value)?;502		Ok(Self)503	}504}505506pub struct NativeFn<D: NativeDesc>(D::Value);507impl<D: NativeDesc> Deref for NativeFn<D> {508	type Target = D::Value;509510	fn deref(&self) -> &Self::Target {511		&self.0512	}513}514impl<D: NativeDesc> Typed for NativeFn<D> {515	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);516517	fn into_untyped(_typed: Self) -> Result<Val> {518		throw!("can only convert functions from jsonnet to native")519	}520521	fn from_untyped(untyped: Val) -> Result<Self> {522		Ok(Self(523			untyped524				.as_func()525				.expect("shape is checked")526				.into_native::<D>(),527		))528	}529}
after · crates/jrsonnet-evaluator/src/typed/conversions.rs
1use std::ops::Deref;23use jrsonnet_gcmodule::Cc;4use jrsonnet_interner::{IBytes, IStr};5pub use jrsonnet_macros::Typed;6use jrsonnet_types::{ComplexValType, ValType};78use crate::{9	arr::ArrValue,10	error::Result,11	function::{native::NativeDesc, FuncDesc, FuncVal},12	throw,13	typed::CheckType,14	val::IndexableVal,15	ObjValue, ObjValueBuilder, Val,16};1718pub trait TypedObj: Typed {19	fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;20	fn parse(obj: &ObjValue) -> Result<Self>;21	fn into_object(self) -> Result<ObjValue> {22		let mut builder = ObjValueBuilder::new();23		self.serialize(&mut builder)?;24		Ok(builder.build())25	}26}2728pub trait Typed: Sized {29	const TYPE: &'static ComplexValType;30	fn into_untyped(typed: Self) -> Result<Val>;31	fn from_untyped(untyped: Val) -> Result<Self>;32}3334const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS + 1)) - 1) as f64;3536macro_rules! impl_int {37	($($ty:ty)*) => {$(38		impl Typed for $ty {39			const TYPE: &'static ComplexValType =40				&ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));41			fn from_untyped(value: Val) -> Result<Self> {42				<Self as Typed>::TYPE.check(&value)?;43				match value {44					Val::Num(n) => {45						#[allow(clippy::float_cmp)]46						if n.trunc() != n {47							throw!(48								"cannot convert number with fractional part to {}",49								stringify!($ty)50							)51						}52						Ok(n as Self)53					}54					_ => unreachable!(),55				}56			}57			fn into_untyped(value: Self) -> Result<Val> {58				Ok(Val::Num(value as f64))59			}60		}61	)*};62}6364impl_int!(i8 u8 i16 u16 i32 u32);6566macro_rules! impl_bounded_int {67	($($name:ident = $ty:ty)*) => {$(68		#[derive(Clone, Copy)]69		pub struct $name<const MIN: $ty, const MAX: $ty>($ty);70		impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {71			pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {72				if value >= MIN && value <= MAX {73					Some(Self(value))74				} else {75					None76				}77			}78			pub const fn value(self) -> $ty {79				self.080			}81		}82		impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {83			type Target = $ty;84			fn deref(&self) -> &Self::Target {85				&self.086			}87		}8889		impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {90			const TYPE: &'static ComplexValType =91				&ComplexValType::BoundedNumber(92					Some(MIN as f64),93					Some(MAX as f64),94				);9596			fn from_untyped(value: Val) -> Result<Self> {97				<Self as Typed>::TYPE.check(&value)?;98				match value {99					Val::Num(n) => {100						#[allow(clippy::float_cmp)]101						if n.trunc() != n {102							throw!(103								"cannot convert number with fractional part to {}",104								stringify!($ty)105							)106						}107						Ok(Self(n as $ty))108					}109					_ => unreachable!(),110				}111			}112113			fn into_untyped(value: Self) -> Result<Val> {114				Ok(Val::Num(value.0 as f64))115			}116		}117	)*};118}119120impl_bounded_int!(121	BoundedI8 = i8122	BoundedI16 = i16123	BoundedI32 = i32124	BoundedI64 = i64125	BoundedUsize = usize126);127128impl Typed for f64 {129	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);130131	fn into_untyped(value: Self) -> Result<Val> {132		Ok(Val::Num(value))133	}134135	fn from_untyped(value: Val) -> Result<Self> {136		<Self as Typed>::TYPE.check(&value)?;137		match value {138			Val::Num(n) => Ok(n),139			_ => unreachable!(),140		}141	}142}143144pub struct PositiveF64(pub f64);145impl Typed for PositiveF64 {146	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);147148	fn into_untyped(value: Self) -> Result<Val> {149		Ok(Val::Num(value.0))150	}151152	fn from_untyped(value: Val) -> Result<Self> {153		<Self as Typed>::TYPE.check(&value)?;154		match value {155			Val::Num(n) => Ok(Self(n)),156			_ => unreachable!(),157		}158	}159}160impl Typed for usize {161	const TYPE: &'static ComplexValType =162		&ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));163164	fn into_untyped(value: Self) -> Result<Val> {165		if value > MAX_SAFE_INTEGER as Self {166			throw!("number is too large")167		}168		Ok(Val::Num(value as f64))169	}170171	fn from_untyped(value: Val) -> Result<Self> {172		<Self as Typed>::TYPE.check(&value)?;173		match value {174			Val::Num(n) => {175				#[allow(clippy::float_cmp)]176				if n.trunc() != n {177					throw!("cannot convert number with fractional part to usize")178				}179				Ok(n as Self)180			}181			_ => unreachable!(),182		}183	}184}185186impl Typed for IStr {187	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);188189	fn into_untyped(value: Self) -> Result<Val> {190		Ok(Val::Str(value))191	}192193	fn from_untyped(value: Val) -> Result<Self> {194		<Self as Typed>::TYPE.check(&value)?;195		match value {196			Val::Str(s) => Ok(s),197			_ => unreachable!(),198		}199	}200}201202impl Typed for String {203	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);204205	fn into_untyped(value: Self) -> Result<Val> {206		Ok(Val::Str(value.into()))207	}208209	fn from_untyped(value: Val) -> Result<Self> {210		<Self as Typed>::TYPE.check(&value)?;211		match value {212			Val::Str(s) => Ok(s.to_string()),213			_ => unreachable!(),214		}215	}216}217218impl Typed for char {219	const TYPE: &'static ComplexValType = &ComplexValType::Char;220221	fn into_untyped(value: Self) -> Result<Val> {222		Ok(Val::Str(value.to_string().into()))223	}224225	fn from_untyped(value: Val) -> Result<Self> {226		<Self as Typed>::TYPE.check(&value)?;227		match value {228			Val::Str(s) => Ok(s.chars().next().unwrap()),229			_ => unreachable!(),230		}231	}232}233234impl<T> Typed for Vec<T>235where236	T: Typed,237{238	const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);239240	fn into_untyped(value: Self) -> Result<Val> {241		let mut o = Vec::with_capacity(value.len());242		for i in value {243			o.push(T::into_untyped(i)?);244		}245		Ok(Val::Arr(o.into()))246	}247248	fn from_untyped(value: Val) -> Result<Self> {249		<Self as Typed>::TYPE.check(&value)?;250		match value {251			Val::Arr(a) => {252				let mut o = Self::with_capacity(a.len());253				for i in a.iter() {254					o.push(T::from_untyped(i?)?);255				}256				Ok(o)257			}258			_ => unreachable!(),259		}260	}261}262263/// To be used in Vec<Any>264/// Regular Val can't be used here, because it has wrong `TryFrom::Error` type265#[derive(Clone)]266pub struct Any(pub Val);267268impl Typed for Any {269	const TYPE: &'static ComplexValType = &ComplexValType::Any;270271	fn into_untyped(value: Self) -> Result<Val> {272		Ok(value.0)273	}274275	fn from_untyped(value: Val) -> Result<Self> {276		Ok(Self(value))277	}278}279280/// Specialization, provides faster `TryFrom<VecVal>` for Val281pub struct VecVal(pub Cc<Vec<Val>>);282283impl Typed for VecVal {284	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);285286	fn into_untyped(value: Self) -> Result<Val> {287		Ok(Val::Arr(ArrValue::eager(value.0)))288	}289290	fn from_untyped(value: Val) -> Result<Self> {291		<Self as Typed>::TYPE.check(&value)?;292		match value {293			Val::Arr(a) => Ok(Self(a.evaluatedcc()?)),294			_ => unreachable!(),295		}296	}297}298299/// Specialization300impl Typed for IBytes {301	const TYPE: &'static ComplexValType =302		&ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));303304	fn into_untyped(value: Self) -> Result<Val> {305		Ok(Val::Arr(ArrValue::bytes(value)))306	}307308	fn from_untyped(value: Val) -> Result<Self> {309		if let Val::Arr(ArrValue::Bytes(bytes)) = value {310			return Ok(bytes.0);311		}312		<Self as Typed>::TYPE.check(&value)?;313		match value {314			Val::Arr(a) => {315				let mut out = Vec::with_capacity(a.len());316				for e in a.iter() {317					let r = e?;318					out.push(u8::from_untyped(r)?);319				}320				Ok(out.as_slice().into())321			}322			_ => unreachable!(),323		}324	}325}326327pub struct M1;328impl Typed for M1 {329	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));330331	fn into_untyped(_: Self) -> Result<Val> {332		Ok(Val::Num(-1.0))333	}334335	fn from_untyped(value: Val) -> Result<Self> {336		<Self as Typed>::TYPE.check(&value)?;337		Ok(Self)338	}339}340341macro_rules! decl_either {342	($($name: ident, $($id: ident)*);*) => {$(343		#[derive(Clone)]344		pub enum $name<$($id),*> {345			$($id($id)),*346		}347		impl<$($id),*> Typed for $name<$($id),*>348		where349			$($id: Typed,)*350		{351			const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);352353			fn into_untyped(value: Self) -> Result<Val> {354				match value {$(355					$name::$id(v) => $id::into_untyped(v)356				),*}357			}358359			fn from_untyped(value: Val) -> Result<Self> {360				$(361					if $id::TYPE.check(&value).is_ok() {362						$id::from_untyped(value).map(Self::$id)363					} else364				)* {365					<Self as Typed>::TYPE.check(&value)?;366					unreachable!()367				}368			}369		}370	)*}371}372decl_either!(373	Either1, A;374	Either2, A B;375	Either3, A B C;376	Either4, A B C D;377	Either5, A B C D E;378	Either6, A B C D E F;379	Either7, A B C D E F G380);381#[macro_export]382macro_rules! Either {383	($a:ty) => {Either1<$a>};384	($a:ty, $b:ty) => {Either2<$a, $b>};385	($a:ty, $b:ty, $c:ty) => {Either3<$a, $b, $c>};386	($a:ty, $b:ty, $c:ty, $d:ty) => {Either4<$a, $b, $c, $d>};387	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {Either5<$a, $b, $c, $d, $e>};388	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {Either6<$a, $b, $c, $d, $e, $f>};389	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {Either7<$a, $b, $c, $d, $e, $f, $g>};390}391pub use Either;392393pub type MyType = Either![u32, f64, String];394395impl Typed for ArrValue {396	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);397398	fn into_untyped(value: Self) -> Result<Val> {399		Ok(Val::Arr(value))400	}401402	fn from_untyped(value: Val) -> Result<Self> {403		<Self as Typed>::TYPE.check(&value)?;404		match value {405			Val::Arr(a) => Ok(a),406			_ => unreachable!(),407		}408	}409}410411impl Typed for FuncVal {412	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);413414	fn into_untyped(value: Self) -> Result<Val> {415		Ok(Val::Func(value))416	}417418	fn from_untyped(value: Val) -> Result<Self> {419		<Self as Typed>::TYPE.check(&value)?;420		match value {421			Val::Func(a) => Ok(a),422			_ => unreachable!(),423		}424	}425}426427impl Typed for Cc<FuncDesc> {428	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);429430	fn into_untyped(value: Self) -> Result<Val> {431		Ok(Val::Func(FuncVal::Normal(value)))432	}433434	fn from_untyped(value: Val) -> Result<Self> {435		<Self as Typed>::TYPE.check(&value)?;436		match value {437			Val::Func(FuncVal::Normal(desc)) => Ok(desc),438			Val::Func(_) => throw!("expected normal function, not builtin"),439			_ => unreachable!(),440		}441	}442}443444impl Typed for ObjValue {445	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);446447	fn into_untyped(value: Self) -> Result<Val> {448		Ok(Val::Obj(value))449	}450451	fn from_untyped(value: Val) -> Result<Self> {452		<Self as Typed>::TYPE.check(&value)?;453		match value {454			Val::Obj(a) => Ok(a),455			_ => unreachable!(),456		}457	}458}459460impl Typed for bool {461	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);462463	fn into_untyped(value: Self) -> Result<Val> {464		Ok(Val::Bool(value))465	}466467	fn from_untyped(value: Val) -> Result<Self> {468		<Self as Typed>::TYPE.check(&value)?;469		match value {470			Val::Bool(a) => Ok(a),471			_ => unreachable!(),472		}473	}474}475impl Typed for IndexableVal {476	const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[477		&ComplexValType::Simple(ValType::Arr),478		&ComplexValType::Simple(ValType::Str),479	]);480481	fn into_untyped(value: Self) -> Result<Val> {482		match value {483			IndexableVal::Str(s) => Ok(Val::Str(s)),484			IndexableVal::Arr(a) => Ok(Val::Arr(a)),485		}486	}487488	fn from_untyped(value: Val) -> Result<Self> {489		<Self as Typed>::TYPE.check(&value)?;490		value.into_indexable()491	}492}493494pub struct Null;495impl Typed for Null {496	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);497498	fn into_untyped(_: Self) -> Result<Val> {499		Ok(Val::Null)500	}501502	fn from_untyped(value: Val) -> Result<Self> {503		<Self as Typed>::TYPE.check(&value)?;504		Ok(Self)505	}506}507508pub struct NativeFn<D: NativeDesc>(D::Value);509impl<D: NativeDesc> Deref for NativeFn<D> {510	type Target = D::Value;511512	fn deref(&self) -> &Self::Target {513		&self.0514	}515}516impl<D: NativeDesc> Typed for NativeFn<D> {517	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);518519	fn into_untyped(_typed: Self) -> Result<Val> {520		throw!("can only convert functions from jsonnet to native")521	}522523	fn from_untyped(untyped: Val) -> Result<Self> {524		Ok(Self(525			untyped526				.as_func()527				.expect("shape is checked")528				.into_native::<D>(),529		))530	}531}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,9 +1,10 @@
-use std::{cell::RefCell, fmt::Debug};
+use std::{cell::RefCell, fmt::Debug, mem::replace};
 
 use jrsonnet_gcmodule::{Cc, Trace};
-use jrsonnet_interner::{IBytes, IStr};
+use jrsonnet_interner::IStr;
 use jrsonnet_types::ValType;
 
+pub use crate::arr::ArrValue;
 use crate::{
 	error::{Error, ErrorKind::*},
 	function::FuncVal,
@@ -31,16 +32,22 @@
 #[derive(Clone, Trace)]
 pub struct Thunk<T: Trace>(Cc<RefCell<ThunkInner<T>>>);
 
+impl<T: Trace> Thunk<T> {
+	pub fn evaluated(val: T) -> Self {
+		Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))
+	}
+	pub fn new(f: TraceBox<dyn ThunkValue<Output = T>>) -> Self {
+		Self(Cc::new(RefCell::new(ThunkInner::Waiting(f))))
+	}
+	pub fn errored(e: Error) -> Self {
+		Self(Cc::new(RefCell::new(ThunkInner::Errored(e))))
+	}
+}
+
 impl<T> Thunk<T>
 where
 	T: Clone + Trace,
 {
-	pub fn new(f: TraceBox<dyn ThunkValue<Output = T>>) -> Self {
-		Self(Cc::new(RefCell::new(ThunkInner::Waiting(f))))
-	}
-	pub fn evaluated(val: T) -> Self {
-		Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))
-	}
 	pub fn force(&self) -> Result<()> {
 		self.evaluate()?;
 		Ok(())
@@ -52,7 +59,7 @@
 			ThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),
 			ThunkInner::Waiting(..) => (),
 		};
-		let ThunkInner::Waiting(value) = std::mem::replace(&mut *self.0.borrow_mut(), ThunkInner::Pending) else {
+		let ThunkInner::Waiting(value) = replace(&mut *self.0.borrow_mut(), ThunkInner::Pending) else {
 			unreachable!();
 		};
 		let new_value = match value.0.get() {
@@ -118,337 +125,8 @@
 	fn eq(&self, other: &Self) -> bool {
 		Cc::ptr_eq(&self.0, &other.0)
 	}
-}
-
-#[derive(Debug, Clone, Trace)]
-pub struct Slice {
-	pub(crate) inner: ArrValue,
-	pub(crate) from: u32,
-	pub(crate) to: u32,
-	pub(crate) step: u32,
 }
-impl Slice {
-	const fn from(&self) -> usize {
-		self.from as usize
-	}
-	const fn to(&self) -> usize {
-		self.to as usize
-	}
-	const fn step(&self) -> usize {
-		self.step as usize
-	}
-	const fn len(&self) -> usize {
-		// TODO: use div_ceil
-		let diff = self.to() - self.from();
-		let rem = diff % self.step();
-		let div = diff / self.step();
-
-		if rem == 0 {
-			div
-		} else {
-			div + 1
-		}
-	}
-}
-
-/// Represents a Jsonnet array value.
-#[derive(Debug, Clone, Trace)]
-// may contrain other ArrValue
-#[trace(tracking(force))]
-pub enum ArrValue {
-	/// Layout optimized byte array.
-	Bytes(#[trace(skip)] IBytes),
-	/// Every element is lazy evaluated.
-	Lazy(Cc<Vec<Thunk<Val>>>),
-	/// Every field is already evaluated.
-	Eager(Cc<Vec<Val>>),
-	/// Concatenation of two arrays of any kind.
-	Extended(Box<(Self, Self)>),
-	/// Represents a integer array in form `[start, start + 1, ... end - 1, end]`.
-	/// This kind of arrays is generated by `std.range(start, end)` call, and used for loops.
-	Range(i32, i32),
-	/// Sliced array view.
-	Slice(Box<Slice>),
-	/// Reversed array view.
-	/// Returned by `std.reverse(other)` call
-	Reversed(Box<Self>),
-}
-
-#[cfg(target_pointer_width = "64")]
-static_assertions::assert_eq_size!(ArrValue, [u8; 16]);
-
-impl ArrValue {
-	pub fn new_eager() -> Self {
-		Self::Eager(Cc::new(Vec::new()))
-	}
-	pub fn empty() -> Self {
-		Self::new_range(0, 0)
-	}
-
-	/// # Panics
-	/// If a > b
-	#[inline]
-	pub fn new_range(a: i32, b: i32) -> Self {
-		assert!(a <= b);
-		Self::Range(a, b)
-	}
-
-	/// # Panics
-	/// If passed numbers are incorrect
-	#[must_use]
-	pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {
-		let len = self.len();
-		let from = from.unwrap_or(0);
-		let to = to.unwrap_or(len).min(len);
-		let step = step.unwrap_or(1);
-		assert!(from < to);
-		assert!(step > 0);
-
-		Self::Slice(Box::new(Slice {
-			inner: self,
-			from: from as u32,
-			to: to as u32,
-			step: step as u32,
-		}))
-	}
 
-	/// Array length.
-	pub fn len(&self) -> usize {
-		match self {
-			Self::Bytes(i) => i.len(),
-			Self::Lazy(l) => l.len(),
-			Self::Eager(e) => e.len(),
-			Self::Extended(v) => v.0.len() + v.1.len(),
-			Self::Range(a, b) => a.abs_diff(*b) as usize + 1,
-			Self::Reversed(i) => i.len(),
-			Self::Slice(s) => s.len(),
-		}
-	}
-
-	/// Is array contains no elements?
-	pub fn is_empty(&self) -> bool {
-		self.len() == 0
-	}
-
-	/// Get array element by index, evaluating it, if it is lazy.
-	///
-	/// Returns `None` on out-of-bounds condition.
-	pub fn get(&self, index: usize) -> Result<Option<Val>> {
-		match self {
-			Self::Bytes(i) => i
-				.get(index)
-				.map_or(Ok(None), |v| Ok(Some(Val::Num(f64::from(*v))))),
-			Self::Lazy(vec) => {
-				if let Some(v) = vec.get(index) {
-					Ok(Some(v.evaluate()?))
-				} else {
-					Ok(None)
-				}
-			}
-			Self::Eager(vec) => Ok(vec.get(index).cloned()),
-			Self::Extended(v) => {
-				let a_len = v.0.len();
-				if a_len > index {
-					v.0.get(index)
-				} else {
-					v.1.get(index - a_len)
-				}
-			}
-			Self::Range(a, _) => {
-				if index >= self.len() {
-					return Ok(None);
-				}
-				Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))
-			}
-			Self::Reversed(v) => {
-				let len = v.len();
-				if index >= len {
-					return Ok(None);
-				}
-				v.get(len - index - 1)
-			}
-			Self::Slice(v) => {
-				let index = v.from() + index * v.step();
-				if index >= v.to() {
-					return Ok(None);
-				}
-				v.inner.get(index)
-			}
-		}
-	}
-
-	/// Get array element by index, without evaluation.
-	///
-	/// Returns `None` on out-of-bounds condition.
-	pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
-		match self {
-			Self::Bytes(i) => i
-				.get(index)
-				.map(|b| Thunk::evaluated(Val::Num(f64::from(*b)))),
-			Self::Lazy(vec) => vec.get(index).cloned(),
-			Self::Eager(vec) => vec.get(index).cloned().map(Thunk::evaluated),
-			Self::Extended(v) => {
-				let a_len = v.0.len();
-				if a_len > index {
-					v.0.get_lazy(index)
-				} else {
-					v.1.get_lazy(index - a_len)
-				}
-			}
-			Self::Range(a, _) => {
-				if index >= self.len() {
-					return None;
-				}
-				Some(Thunk::evaluated(Val::Num(
-					((*a as isize) + index as isize) as f64,
-				)))
-			}
-			Self::Reversed(v) => {
-				let len = v.len();
-				if index >= len {
-					return None;
-				}
-				v.get_lazy(len - index - 1)
-			}
-			Self::Slice(s) => {
-				let index = s.from() + index * s.step();
-				if index >= s.to() {
-					return None;
-				}
-				s.inner.get_lazy(index)
-			}
-		}
-	}
-
-	/// Evaluate all array elements, returning new array.
-	pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {
-		Ok(match self {
-			Self::Bytes(i) => {
-				let mut out = Vec::with_capacity(i.len());
-				for v in i.iter() {
-					out.push(Val::Num(f64::from(*v)));
-				}
-				Cc::new(out)
-			}
-			Self::Lazy(vec) => {
-				let mut out = Vec::with_capacity(vec.len());
-				for item in vec.iter() {
-					out.push(item.evaluate()?);
-				}
-				Cc::new(out)
-			}
-			Self::Eager(vec) => vec.clone(),
-			Self::Extended(_v) => {
-				let mut out = Vec::with_capacity(self.len());
-				for item in self.iter() {
-					out.push(item?);
-				}
-				Cc::new(out)
-			}
-			Self::Range(a, b) => {
-				let mut out = Vec::with_capacity(self.len());
-				for i in *a..=*b {
-					out.push(Val::Num(f64::from(i)));
-				}
-				Cc::new(out)
-			}
-			Self::Reversed(r) => {
-				let mut r = r.evaluated()?;
-				Cc::update_with(&mut r, |v| v.reverse());
-				r
-			}
-			Self::Slice(v) => {
-				let mut out = Vec::with_capacity(v.inner.len());
-				for v in v
-					.inner
-					.iter_lazy()
-					.skip(v.from())
-					.take(v.to() - v.from())
-					.step_by(v.step())
-				{
-					out.push(v.evaluate()?);
-				}
-				Cc::new(out)
-			}
-		})
-	}
-
-	/// Iterate over elements, evaluating them.
-	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
-		(0..self.len()).map(move |idx| match self {
-			Self::Bytes(b) => Ok(Val::Num(f64::from(b[idx]))),
-			Self::Lazy(l) => l[idx].evaluate(),
-			Self::Eager(e) => Ok(e[idx].clone()),
-			Self::Extended(..) | Self::Range(..) | Self::Reversed(..) | Self::Slice(..) => {
-				self.get(idx).map(|e| e.expect("idx < len"))
-			}
-		})
-	}
-
-	/// Iterate over elements, returning lazy values.
-	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = Thunk<Val>> + '_ {
-		(0..self.len()).map(move |idx| match self {
-			Self::Bytes(b) => Thunk::evaluated(Val::Num(f64::from(b[idx]))),
-			Self::Lazy(l) => l[idx].clone(),
-			Self::Eager(e) => Thunk::evaluated(e[idx].clone()),
-			Self::Slice(..) | Self::Extended(..) | Self::Range(..) | Self::Reversed(..) => {
-				self.get_lazy(idx).expect("idx < len")
-			}
-		})
-	}
-
-	/// Return a reversed view on current array.
-	#[must_use]
-	pub fn reversed(self) -> Self {
-		Self::Reversed(Box::new(self))
-	}
-
-	/// Return a new array, produced by passing every element of current array to specified callback function.
-	pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {
-		let mut out = Vec::with_capacity(self.len());
-
-		for value in self.iter() {
-			out.push(mapper(value?)?);
-		}
-
-		Ok(Self::Eager(Cc::new(out)))
-	}
-
-	/// Return a new array, produced from current array by removing every value, for which specified callback function returns false.
-	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
-		let mut out = Vec::with_capacity(self.len());
-
-		for value in self.iter() {
-			let value = value?;
-			if filter(&value)? {
-				out.push(value);
-			}
-		}
-
-		Ok(Self::Eager(Cc::new(out)))
-	}
-
-	pub fn ptr_eq(a: &Self, b: &Self) -> bool {
-		match (a, b) {
-			(Self::Lazy(a), Self::Lazy(b)) => Cc::ptr_eq(a, b),
-			(Self::Eager(a), Self::Eager(b)) => Cc::ptr_eq(a, b),
-			_ => false,
-		}
-	}
-}
-
-impl From<Vec<Thunk<Val>>> for ArrValue {
-	fn from(v: Vec<Thunk<Val>>) -> Self {
-		Self::Lazy(Cc::new(v))
-	}
-}
-
-impl From<Vec<Val>> for ArrValue {
-	fn from(v: Vec<Val>) -> Self {
-		Self::Eager(Cc::new(v))
-	}
-}
-
 /// Represents a Jsonnet value, which can be spliced or indexed (string or array).
 #[allow(clippy::module_name_repetitions)]
 pub enum IndexableVal {
@@ -496,15 +174,14 @@
 				let step = step.as_deref().copied().unwrap_or(1);
 
 				if index >= end {
-					return Ok(Self::Arr(ArrValue::new_eager()));
+					return Ok(Self::Arr(ArrValue::empty()));
 				}
 
-				Ok(Self::Arr(ArrValue::Slice(Box::new(Slice {
-					inner: arr.clone(),
-					from: index as u32,
-					to: end as u32,
-					step: step as u32,
-				}))))
+				Ok(Self::Arr(
+					arr.clone()
+						.slice(Some(index), Some(end), Some(step))
+						.expect("arguments checked"),
+				))
 			}
 		}
 	}
@@ -539,10 +216,6 @@
 		}
 	}
 }
-
-// Broken between stable and nightly, as there is new layout size optimization
-// #[cfg(target_pointer_width = "64")]
-// static_assertions::assert_eq_size!(Val, [u8; 24]);
 
 impl Val {
 	pub const fn as_bool(&self) -> Option<bool> {
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -28,8 +28,8 @@
 }
 
 #[builtin]
-pub fn builtin_map(func: NativeFn<((Any,), Any)>, arr: ArrValue) -> Result<ArrValue> {
-	arr.map(|val| Ok(func(Any(val))?.0))
+pub fn builtin_map(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
+	Ok(arr.map(func))
 }
 
 #[builtin]
@@ -94,9 +94,9 @@
 #[builtin]
 pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {
 	if to < from {
-		return Ok(ArrValue::new_eager());
+		return Ok(ArrValue::empty());
 	}
-	Ok(ArrValue::new_range(from, to))
+	Ok(ArrValue::range_inclusive(from, to))
 }
 
 #[builtin]
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -77,7 +77,7 @@
 			} else if b.len() == a.len() {
 				return equals(&Val::Arr(a), &Val::Arr(b));
 			} else {
-				for (a, b) in a.slice(None, Some(b.len()), None).iter().zip(b.iter()) {
+				for (a, b) in a.iter().take(b.len()).zip(b.iter()) {
 					let a = a?;
 					let b = b?;
 					if !equals(&a, &b)? {
@@ -103,11 +103,7 @@
 				return equals(&Val::Arr(a), &Val::Arr(b));
 			} else {
 				let a_len = a.len();
-				for (a, b) in a
-					.slice(Some(a_len - b.len()), None, None)
-					.iter()
-					.zip(b.iter())
-				{
+				for (a, b) in a.iter().skip(a_len - b.len()).zip(b.iter()) {
 					let a = a?;
 					let b = b?;
 					if !equals(&a, &b)? {
modifiedcrates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/sort.rs
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -106,9 +106,9 @@
 	if arr.len() <= 1 {
 		return Ok(arr);
 	}
-	Ok(ArrValue::Eager(super::sort::sort(
+	Ok(ArrValue::eager(super::sort::sort(
 		ctx,
-		arr.evaluated()?,
+		arr.evaluatedcc()?,
 		keyF.unwrap_or_else(FuncVal::identity),
 	)?))
 }