git.delta.rocks / jrsonnet / refs/commits / 0b32cb0e3353

difftreelog

source

crates/jrsonnet-evaluator/src/arr/mod.rs6.1 KiBsourcehistory
1use jrsonnet_gcmodule::{Cc, Trace};2use jrsonnet_interner::IBytes;3use jrsonnet_parser::LocExpr;45use crate::{function::FuncVal, Context, Result, Thunk, Val};67mod spec;8use spec::*;910/// Represents a Jsonnet array value.11#[derive(Debug, Clone, Trace)]12// may contrain other ArrValue13#[trace(tracking(force))]14pub enum ArrValue {15	/// Layout optimized byte array.16	Bytes(BytesArray),17	/// Every element is lazy evaluated.18	Lazy(LazyArray),19	/// Every element is defined somewhere in source code20	Expr(ExprArray),21	/// Every field is already evaluated.22	Eager(EagerArray),23	/// Concatenation of two arrays of any kind.24	Extended(Cc<ExtendedArray>),25	/// Represents a integer array in form `[start, start + 1, ... end - 1, end]`.26	/// This kind of arrays is generated by `std.range(start, end)` call, and used for loops.27	Range(RangeArray),28	/// Sliced array view.29	Slice(Box<SliceArray>),30	/// Reversed array view.31	/// Returned by `std.reverse(other)` call32	Reverse(Box<ReverseArray>),33	/// Returned by `std.map` call34	Mapped(MappedArray),35	/// Returned by `std.repeat` call36	Repeated(RepeatedArray),37}3839impl ArrValue {40	pub fn empty() -> Self {41		Self::Range(RangeArray::empty())42	}4344	pub fn expr(ctx: Context, exprs: impl IntoIterator<Item = LocExpr>) -> Self {45		Self::Expr(ExprArray::new(ctx, exprs))46	}4748	pub fn lazy(thunks: Cc<Vec<Thunk<Val>>>) -> Self {49		Self::Lazy(LazyArray(thunks))50	}5152	pub fn eager(values: Cc<Vec<Val>>) -> Self {53		Self::Eager(EagerArray(values))54	}5556	pub fn repeated(data: ArrValue, repeats: usize) -> Option<Self> {57		Some(Self::Repeated(RepeatedArray::new(data, repeats)?))58	}5960	pub fn bytes(bytes: IBytes) -> Self {61		Self::Bytes(BytesArray(bytes))62	}6364	#[must_use]65	pub fn map(self, mapper: FuncVal) -> Self {66		Self::Mapped(MappedArray::new(self, mapper))67	}6869	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {70		// TODO: ArrValue::Picked(inner, indexes) for large arrays71		let mut out = Vec::new();72		for i in self.iter() {73			let i = i?;74			if filter(&i)? {75				out.push(i);76			};77		}78		Ok(Self::eager(Cc::new(out)))79	}8081	pub fn extended(a: ArrValue, b: ArrValue) -> Self {82		// TODO: benchmark for an optimal value, currently just a arbitrary choice83		const ARR_EXTEND_THRESHOLD: usize = 100;8485		if a.is_empty() {86			b87		} else if b.is_empty() {88			a89		} else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {90			Self::Extended(Cc::new(ExtendedArray::new(a, b)))91		} else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {92			let mut out = Vec::with_capacity(a.len() + b.len());93			out.extend(a);94			out.extend(b);95			Self::eager(Cc::new(out))96		} else {97			let mut out = Vec::with_capacity(a.len() + b.len());98			out.extend(a.iter_lazy());99			out.extend(b.iter_lazy());100			Self::lazy(Cc::new(out))101		}102	}103104	pub fn range_exclusive(a: i32, b: i32) -> Self {105		Self::Range(RangeArray::new_exclusive(a, b))106	}107	pub fn range_inclusive(a: i32, b: i32) -> Self {108		Self::Range(RangeArray::new_inclusive(a, b))109	}110111	#[must_use]112	pub fn slice(113		self,114		from: Option<usize>,115		to: Option<usize>,116		step: Option<usize>,117	) -> Option<Self> {118		let len = self.len();119		let from = from.unwrap_or(0);120		let to = to.unwrap_or(len).min(len);121		let step = step.unwrap_or(1);122		if from >= to || step == 0 {123			return None;124		}125126		Some(Self::Slice(Box::new(SliceArray {127			inner: self,128			from: from as u32,129			to: to as u32,130			step: step as u32,131		})))132	}133134	/// Array length.135	pub fn len(&self) -> usize {136		pass!(self.len())137	}138139	/// Is array contains no elements?140	pub fn is_empty(&self) -> bool {141		pass!(self.is_empty())142	}143144	/// Get array element by index, evaluating it, if it is lazy.145	///146	/// Returns `None` on out-of-bounds condition.147	pub fn get(&self, index: usize) -> Result<Option<Val>> {148		pass!(self.get(index))149	}150151	/// Returns None if get is either non cheap, or out of bounds152	fn get_cheap(&self, index: usize) -> Option<Val> {153		pass!(self.get_cheap(index))154	}155156	/// Get array element by index, without evaluation.157	///158	/// Returns `None` on out-of-bounds condition.159	pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {160		pass!(self.get_lazy(index))161	}162163	/// Evaluate all array elements, returning new array.164	pub fn evaluatedcc(&self) -> Result<Cc<Vec<Val>>> {165		self.evaluated().map(Cc::new)166	}167	pub fn evaluated(&self) -> Result<Vec<Val>> {168		pass!(self.evaluated())169	}170171	/// Iterate over elements, evaluating them.172	pub fn iter(&self) -> UnknownArrayIter<'_> {173		pass_iter_call!(self.iter => UnknownArrayIter)174	}175176	/// Iterate over elements, returning lazy values.177	pub fn iter_lazy(&self) -> UnknownArrayIterLazy<'_> {178		pass_iter_call!(self.iter_lazy => UnknownArrayIterLazy)179	}180181	pub fn iter_cheap(&self) -> Option<UnknownArrayIterCheap<'_>> {182		macro_rules! question {183			($v:expr) => {184				$v?185			};186		}187		Some(pass_iter_call!(self.iter_cheap in question => UnknownArrayIterCheap))188	}189190	/// Return a reversed view on current array.191	#[must_use]192	pub fn reversed(self) -> Self {193		Self::Reverse(Box::new(ReverseArray(self)))194	}195196	pub fn ptr_eq(a: &Self, b: &Self) -> bool {197		match (a, b) {198			(ArrValue::Bytes(a), ArrValue::Bytes(b)) => a.0 == b.0,199			(ArrValue::Lazy(a), ArrValue::Lazy(b)) => Cc::ptr_eq(&a.0, &b.0),200			(ArrValue::Expr(a), ArrValue::Expr(b)) => Cc::ptr_eq(&a.0, &b.0),201			(ArrValue::Eager(a), ArrValue::Eager(b)) => Cc::ptr_eq(&a.0, &b.0),202			(ArrValue::Extended(a), ArrValue::Extended(b)) => Cc::ptr_eq(a, b),203			(ArrValue::Range(a), ArrValue::Range(b)) => a == b,204			_ => false,205		}206	}207208	pub fn is_cheap(&self) -> bool {209		match self {210			ArrValue::Eager(_) | ArrValue::Range(..) | ArrValue::Bytes(_) => true,211			ArrValue::Extended(v) => v.a.is_cheap() && v.b.is_cheap(),212			ArrValue::Slice(r) => r.inner.is_cheap(),213			ArrValue::Reverse(i) => i.0.is_cheap(),214			ArrValue::Repeated(v) => v.is_cheap(),215			ArrValue::Expr(_) | ArrValue::Lazy(_) | ArrValue::Mapped(_) => false,216		}217	}218}219impl From<Vec<Val>> for ArrValue {220	fn from(value: Vec<Val>) -> Self {221		Self::eager(Cc::new(value))222	}223}224impl From<Vec<Thunk<Val>>> for ArrValue {225	fn from(value: Vec<Thunk<Val>>) -> Self {226		Self::lazy(Cc::new(value))227	}228}229230#[cfg(target_pointer_width = "64")]231static_assertions::assert_eq_size!(ArrValue, [u8; 16]);