difftreelog
feat array unification
in: master
11 files changed
bindings/jsonnet/src/val_make.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -50,7 +50,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(Vec::new()))))
+ Box::into_raw(Box::new(Val::arr(())))
}
/// Make a `JsonnetJsonValue` representing an object.
bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -24,7 +24,7 @@
}
new.push(Thunk::evaluated(val.clone()));
- *arr = Val::Arr(ArrValue::lazy(new));
+ *arr = Val::arr(new);
}
_ => panic!("should receive array"),
}
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -6,7 +6,6 @@
};
use jrsonnet_gcmodule::{Cc, cc_dyn};
-use jrsonnet_interner::IBytes;
use jrsonnet_ir::Expr;
use crate::{Context, Result, Thunk, Val, function::NativeFn, typed::IntoUntyped};
@@ -35,28 +34,17 @@
impl ArrValue {
pub fn empty() -> Self {
- Self::new(RangeArray::empty())
+ Self::new(())
}
pub fn expr(ctx: Context, exprs: Rc<Vec<Expr>>) -> Self {
Self::new(ExprArray::new(ctx, exprs))
- }
-
- pub fn lazy(thunks: Vec<Thunk<Val>>) -> Self {
- Self::new(LazyArray(thunks))
}
- pub fn eager(values: Vec<Val>) -> Self {
- Self::new(EagerArray(values))
- }
-
pub fn repeated(data: Self, repeats: usize) -> Option<Self> {
Some(Self::new(RepeatedArray::new(data, repeats)?))
}
- pub fn bytes(bytes: IBytes) -> Self {
- Self::new(BytesArray(bytes))
- }
pub fn chars(chars: impl Iterator<Item = char>) -> Self {
Self::new(CharArray(chars.collect()))
}
@@ -83,7 +71,7 @@
out.push(i);
}
}
- return Ok(Self::eager(out));
+ return Ok(Self::new(out));
};
let mut out = Vec::new();
@@ -92,29 +80,16 @@
out.push(i);
}
}
- Ok(Self::lazy(out))
+ Ok(Self::new(out))
}
pub fn extended(a: Self, b: Self) -> Self {
- // TODO: benchmark for an optimal value, currently just a arbitrary choice
- const ARR_EXTEND_THRESHOLD: usize = 1000;
-
if a.is_empty() {
b
} else if b.is_empty() {
a
- } else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {
+ } else {
Self::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(out)
- } else {
- let mut out = Vec::with_capacity(a.len() + b.len());
- out.extend(a.iter_lazy());
- out.extend(b.iter_lazy());
- Self::lazy(out)
}
}
@@ -165,19 +140,15 @@
self.0.is_empty()
}
+ pub fn is_cheap(&self) -> bool {
+ self.0.is_cheap()
+ }
+
/// 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>> {
self.0.get(index)
- }
-
- /// Returns None if get is either non cheap, or out of bounds
- /// Note that non-cheap access includes errorable values
- ///
- /// Prefer it to `get_lazy`, but use `get` when you can.
- fn get_cheap(&self, index: usize) -> Option<Val> {
- self.0.get_cheap(index)
}
/// Get array element by index, without evaluation.
@@ -196,15 +167,6 @@
(0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))
}
- /// Prefer it over `iter_lazy`, but do not use it where `iter` will do.
- 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")))
- } else {
- None
- }
- }
-
/// Return a reversed view on current array.
#[must_use]
pub fn reversed(self) -> Self {
@@ -213,50 +175,25 @@
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
Cc::ptr_eq(&a.0, &b.0)
- }
-
- /// Is this vec supports `.get_cheap()?`
- pub fn is_cheap(&self) -> bool {
- self.0.is_cheap()
}
pub fn as_any(&self) -> &dyn Any {
&self.0
}
}
-impl From<Vec<Val>> for ArrValue {
- fn from(value: Vec<Val>) -> Self {
- Self::eager(value)
- }
-}
-impl From<Vec<Thunk<Val>>> for ArrValue {
- fn from(value: Vec<Thunk<Val>>) -> Self {
- Self::lazy(value)
- }
-}
-impl FromIterator<Val> for ArrValue {
- fn from_iter<T: IntoIterator<Item = Val>>(iter: T) -> Self {
- Self::eager(iter.into_iter().collect())
+impl<T> From<T> for ArrValue
+where
+ T: ArrayLike,
+{
+ fn from(value: T) -> Self {
+ Self::new(value)
}
}
-impl ArrayLike for ArrValue {
- fn len(&self) -> usize {
- self.0.len()
- }
-
- fn get(&self, index: usize) -> Result<Option<Val>> {
- self.0.get(index)
- }
-
- fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
- self.0.get_lazy(index)
- }
-
- fn get_cheap(&self, index: usize) -> Option<Val> {
- self.0.get_cheap(index)
- }
-
- fn is_cheap(&self) -> bool {
- self.0.is_cheap()
+impl<I> FromIterator<I> for ArrValue
+where
+ Vec<I>: ArrayLike,
+{
+ fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
+ Self::new(iter.into_iter().collect::<Vec<_>>())
}
}
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -1,4 +1,10 @@
-use std::{any::Any, cell::RefCell, fmt::Debug, mem::replace, rc::Rc};
+use std::{
+ any::Any,
+ cell::RefCell,
+ fmt::{self, Debug},
+ mem::replace,
+ rc::Rc,
+};
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::{IBytes, IStr};
@@ -21,11 +27,45 @@
}
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 is_cheap(&self) -> bool;
+ fn is_cheap(&self) -> bool {
+ false
+ }
+}
+trait ArrayCheap {
+ fn get(&self, index: usize) -> Option<Val>;
+ fn len(&self) -> usize;
}
+impl<T> ArrayLike for T
+where
+ T: Any + Trace + Debug + ArrayCheap,
+{
+ fn len(&self) -> usize {
+ <T as ArrayCheap>::len(self)
+ }
+
+ fn get(&self, index: usize) -> Result<Option<Val>> {
+ Ok(<T as ArrayCheap>::get(self, index))
+ }
+ fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+ <T as ArrayCheap>::get(self, index).map(Thunk::evaluated)
+ }
+
+ fn is_cheap(&self) -> bool {
+ true
+ }
+}
+
+impl ArrayCheap for () {
+ fn len(&self) -> usize {
+ 0
+ }
+ fn get(&self, _index: usize) -> Option<Val> {
+ None
+ }
+}
+
#[derive(Debug, Trace)]
pub struct SliceArray {
pub(crate) inner: ArrValue,
@@ -52,9 +92,6 @@
self.inner.get_lazy(self.map_idx(index))
}
- fn get_cheap(&self, index: usize) -> Option<Val> {
- self.inner.get_cheap(self.map_idx(index))
- }
fn is_cheap(&self) -> bool {
self.inner.is_cheap()
}
@@ -62,47 +99,21 @@
#[derive(Trace, Debug)]
pub struct CharArray(pub Vec<char>);
-impl ArrayLike for CharArray {
+impl ArrayCheap for CharArray {
fn len(&self) -> usize {
- self.0.len()
+ self.0.as_slice().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::string(*v))
- }
- fn is_cheap(&self) -> bool {
- true
+ fn get(&self, index: usize) -> Option<Val> {
+ self.0.as_slice().get(index).map(|v| Val::string(*v))
}
}
-#[derive(Trace, Debug)]
-pub struct BytesArray(pub IBytes);
-impl ArrayLike for BytesArray {
+impl ArrayCheap for IBytes {
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((*v).into()))
+ self.as_slice().len()
}
- fn is_cheap(&self) -> bool {
- true
+ fn get(&self, index: usize) -> Option<Val> {
+ self.as_slice().get(index).map(|v| Val::Num((*v).into()))
}
}
@@ -190,9 +201,6 @@
expr: self.clone(),
index,
}))
- }
- fn get_cheap(&self, _index: usize) -> Option<Val> {
- None
}
fn is_cheap(&self) -> bool {
false
@@ -275,61 +283,34 @@
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 is_cheap(&self) -> bool {
self.a.is_cheap() && self.b.is_cheap()
}
}
-#[derive(Trace, Debug)]
-pub struct LazyArray(pub Vec<Thunk<Val>>);
-impl ArrayLike for LazyArray {
+impl<T> ArrayLike for Vec<T>
+where
+ T: IntoUntyped + Trace + fmt::Debug,
+ for<'a> &'a T: IntoUntyped,
+{
fn len(&self) -> usize {
- self.0.len()
+ self.as_slice().len()
}
+
fn get(&self, index: usize) -> Result<Option<Val>> {
- let Some(v) = self.0.get(index) else {
+ let Some(elem) = self.as_slice().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 is_cheap(&self) -> bool {
- false
- }
-}
-
-#[derive(Trace, Debug)]
-pub struct EagerArray(pub Vec<Val>);
-impl ArrayLike for EagerArray {
- fn len(&self) -> usize {
- self.0.len()
+ IntoUntyped::into_untyped(elem).map(Some)
}
- 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)
+ let elem = self.as_slice().get(index)?;
+ Some(IntoUntyped::into_lazy_untyped(elem))
}
- fn get_cheap(&self, index: usize) -> Option<Val> {
- self.0.get(index).cloned()
- }
fn is_cheap(&self) -> bool {
- true
+ !T::provides_lazy()
}
}
@@ -363,28 +344,12 @@
WithExactSize(self.start..=self.end, self.size())
}
}
-
-impl ArrayLike for RangeArray {
- fn len(&self) -> usize {
- self.size()
- }
- fn is_empty(&self) -> bool {
- self.size() == 0
- }
-
- 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> {
+impl ArrayCheap for RangeArray {
+ fn get(&self, index: usize) -> Option<Val> {
self.range().nth(index).map(|i| Val::Num(i.into()))
}
- fn is_cheap(&self) -> bool {
- true
+ fn len(&self) -> usize {
+ self.size()
}
}
@@ -403,9 +368,6 @@
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 is_cheap(&self) -> bool {
self.0.is_cheap()
}
@@ -509,13 +471,6 @@
arr: self.clone(),
index,
}))
- }
-
- fn get_cheap(&self, _index: usize) -> Option<Val> {
- None
- }
- fn is_cheap(&self) -> bool {
- false
}
}
@@ -534,6 +489,12 @@
total_len,
})
}
+ fn map_idx(&self, index: usize) -> Option<usize> {
+ if index > self.total_len {
+ return None;
+ }
+ Some(index % self.data.len())
+ }
}
impl ArrayLike for RepeatedArray {
@@ -542,25 +503,17 @@
}
fn get(&self, index: usize) -> Result<Option<Val>> {
- if index > self.total_len {
+ let Some(idx) = self.map_idx(index) else {
return Ok(None);
- }
- self.data.get(index % self.data.len())
+ };
+ self.data.get(idx)
}
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
- if index > self.total_len {
- return None;
- }
- self.data.get_lazy(index % self.data.len())
+ let idx = self.map_idx(index)?;
+ self.data.get_lazy(idx)
}
- fn get_cheap(&self, index: usize) -> Option<Val> {
- if index > self.total_len {
- return None;
- }
- self.data.get_cheap(index % self.data.len())
- }
fn is_cheap(&self) -> bool {
self.data.is_cheap()
}
@@ -584,21 +537,17 @@
}
fn get(&self, index: usize) -> Result<Option<Val>> {
- let Some(key) = self.keys.get(index) else {
+ let Some(key) = self.keys.as_slice().get(index) else {
return Ok(None);
};
Ok(Some(self.obj.get_or_bail(key.clone())?))
}
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
- let key = self.keys.get(index)?;
+ let key = self.keys.as_slice().get(index)?;
Some(self.obj.get_lazy_or_bail(key.clone()))
}
- fn get_cheap(&self, _index: usize) -> Option<Val> {
- None
- }
-
fn is_cheap(&self) -> bool {
false
}
@@ -628,7 +577,7 @@
}
fn get(&self, index: usize) -> Result<Option<Val>> {
- let Some(key) = self.keys.get(index) else {
+ let Some(key) = self.keys.as_slice().get(index) else {
return Ok(None);
};
Ok(Some(
@@ -641,7 +590,7 @@
}
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
- let key = self.keys.get(index)?;
+ let key = self.keys.as_slice().get(index)?;
// Nothing can fail in the key part, yet value is still
// lazy-evaluated
Some(Thunk::evaluated(
@@ -651,10 +600,6 @@
})
.expect("convertible"),
))
- }
-
- fn get_cheap(&self, _index: usize) -> Option<Val> {
- None
}
fn is_cheap(&self) -> bool {
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -70,12 +70,12 @@
if n.iter().any(|e| !is_trivial(e)) {
return None;
}
- Val::Arr(ArrValue::eager(
+ Val::Arr(
n.iter()
.map(evaluate_trivial)
.map(|e| e.expect("checked trivial"))
.collect(),
- ))
+ )
}
_ => return None,
})
@@ -145,12 +145,12 @@
let fctx = Pending::new();
let mut new_bindings = FxHashMap::with_capacity(into.binds_len());
let obj = obj.clone();
- let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![
+ let value = Thunk::evaluated(Val::arr(vec![
Thunk::evaluated(Val::string(field.clone())),
- Thunk!(move || obj.get(field).transpose().expect(
+ obj.get_lazy(field).transpose().expect(
"field exists, as field name was obtained from object.fields()",
- )),
- ])));
+ ),
+ ]));
destruct(into, value, fctx.clone(), &mut new_bindings)?;
let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
@@ -528,7 +528,7 @@
#[cfg(feature = "exp-null-coaelse")]
None if part.null_coaelse => return Ok(Val::Null),
None => {
- let suggestions = suggest_object_fields(&v, key.clone().into_flat());
+ let suggestions = suggest_object_fields(&v, key.into_flat());
return Err(Error::from(NoSuchField(
key.clone().into_flat(),
@@ -628,7 +628,7 @@
}
Arr(items) => {
if items.is_empty() {
- Val::Arr(ArrValue::empty())
+ Val::arr(())
} else {
Val::Arr(ArrValue::expr(ctx, items.clone()))
}
@@ -640,7 +640,7 @@
out.push(Thunk!(move || evaluate(ctx, &expr)));
Ok(())
})?;
- Val::Arr(ArrValue::lazy(out))
+ Val::arr(out)
}
Obj(body) => Val::Obj(evaluate_object(None, ctx, body)?),
ObjExtend(a, b) => {
@@ -718,9 +718,7 @@
|| s.import_resolved(resolved_path),
)?,
ImportKind::Str => Val::string(s.import_resolved_str(resolved_path)?),
- ImportKind::Bin => {
- Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?))
- }
+ ImportKind::Bin => Val::arr(s.import_resolved_bin(resolved_path)?),
}) as Result<Val>
})?
}
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -1,6 +1,6 @@
use std::borrow::Cow;
-use jrsonnet_interner::IStr;
+use jrsonnet_interner::{IBytes, IStr};
use serde::{
Deserialize, Serialize, Serializer,
de::{self, Visitor},
@@ -11,8 +11,8 @@
};
use crate::{
- Error as JrError, ObjValue, ObjValueBuilder, Result, Val, arr::ArrValue, in_description_frame,
- runtime_error, val::NumValue,
+ Error as JrError, ObjValue, ObjValueBuilder, Result, Val, in_description_frame, runtime_error,
+ val::NumValue,
};
impl<'de> Deserialize<'de> for Val {
@@ -90,7 +90,7 @@
where
E: de::Error,
{
- Ok(Val::Arr(ArrValue::bytes(v.into())))
+ Ok(Val::arr(IBytes::from(v)))
}
fn visit_none<E>(self) -> Result<Self::Value, E>
@@ -130,7 +130,7 @@
out.push(val);
}
- Ok(Val::Arr(ArrValue::eager(out)))
+ Ok(Val::arr(out))
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
@@ -270,7 +270,7 @@
}
fn end(self) -> Result<Val> {
- let inner = Val::Arr(ArrValue::eager(self.data));
+ let inner = Val::arr(self.data);
if let Some(variant) = self.variant {
let mut out = ObjValue::builder_with_capacity(1);
out.field(variant).value(inner);
@@ -509,7 +509,7 @@
}
fn serialize_bytes(self, v: &[u8]) -> Result<Val> {
- Ok(Val::Arr(ArrValue::bytes(v.into())))
+ Ok(Val::arr(IBytes::from(v)))
}
fn serialize_none(self) -> Result<Val> {
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -6,7 +6,7 @@
use crate::{
ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
- arr::{ArrValue, BytesArray},
+ arr::ArrValue,
bail,
function::FuncVal,
typed::CheckType,
@@ -83,6 +83,12 @@
pub trait Typed: Sized {
const TYPE: &'static ComplexValType;
}
+impl<T> Typed for &T
+where
+ T: Typed,
+{
+ const TYPE: &'static ComplexValType = <&T as Typed>::TYPE;
+}
pub trait IntoUntyped: Typed {
// Whatever caller should use `into_lazy_untyped` instead of `into_untyped`
fn provides_lazy() -> bool {
@@ -93,6 +99,7 @@
Thunk::from(Self::into_untyped(typed))
}
}
+
pub trait IntoUntypedResult: Typed {
/// Hack to make builtins be able to return non-result values, and make macros able to convert those values to result
/// This method returns identity in impl Typed for Result, and should not be overriden
@@ -157,6 +164,26 @@
inner.map(<ThunkIntoUntyped<T>>::default())
}
}
+impl<T> IntoUntyped for &Thunk<T>
+where
+ T: IntoUntyped + Trace + Clone,
+{
+ fn into_untyped(typed: Self) -> Result<Val> {
+ T::into_untyped(typed.evaluate()?)
+ }
+ fn provides_lazy() -> bool {
+ true
+ }
+
+ fn into_lazy_untyped(inner: Self) -> Thunk<Val> {
+ // Avoid lazy mapping
+ let inner = match try_cast_thunk_val(inner.clone()) {
+ Ok(v) => return v,
+ Err(e) => e,
+ };
+ inner.map(<ThunkIntoUntyped<T>>::default())
+ }
+}
fn try_cast_thunk_t<T: 'static>(typed: Thunk<Val>) -> Result<Thunk<T>, Thunk<Val>> {
if TypeId::of::<T>() == TypeId::of::<Val>() {
@@ -221,6 +248,11 @@
}
}
}
+ impl IntoUntyped for &$ty {
+ fn into_untyped(value: Self) -> Result<Val> {
+ Ok(Val::Num((*value).into()))
+ }
+ }
impl IntoUntyped for $ty {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Num(value.into()))
@@ -305,6 +337,11 @@
impl Typed for f64 {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);
}
+impl IntoUntyped for &f64 {
+ fn into_untyped(value: Self) -> Result<Val> {
+ Ok(Val::try_num(*value)?)
+ }
+}
impl IntoUntyped for f64 {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::try_num(value)?)
@@ -324,7 +361,7 @@
impl Typed for PositiveF64 {
const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);
}
-impl IntoUntyped for PositiveF64 {
+impl IntoUntyped for &PositiveF64 {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::try_num(value.0)?)
}
@@ -538,6 +575,11 @@
impl Typed for Val {
const TYPE: &'static ComplexValType = &ComplexValType::Any;
}
+impl IntoUntyped for &Val {
+ fn into_untyped(typed: Self) -> Result<Val> {
+ Ok(typed.clone())
+ }
+}
impl IntoUntyped for Val {
fn into_untyped(typed: Self) -> Result<Val> {
Ok(typed)
@@ -567,9 +609,14 @@
const TYPE: &'static ComplexValType =
&ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));
}
+impl IntoUntyped for &IBytes {
+ fn into_untyped(value: Self) -> Result<Val> {
+ Ok(Val::arr(value.clone()))
+ }
+}
impl IntoUntyped for IBytes {
fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Arr(ArrValue::bytes(value)))
+ Ok(Val::arr(value))
}
}
impl FromUntyped for IBytes {
@@ -578,8 +625,8 @@
<Self as Typed>::TYPE.check(&value)?;
unreachable!()
};
- if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {
- return Ok(bytes.0.as_slice().into());
+ if let Some(bytes) = a.as_any().downcast_ref::<IBytes>() {
+ return Ok(bytes.clone());
}
<Self as Typed>::TYPE.check(&value)?;
// Any::downcast_ref::<ByteArray>(&a);
@@ -596,7 +643,7 @@
impl Typed for M1 {
const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));
}
-impl IntoUntyped for M1 {
+impl IntoUntyped for &M1 {
fn into_untyped(_: Self) -> Result<Val> {
Ok(Val::Num(NumValue::new(-1.0).expect("finite")))
}
@@ -728,6 +775,11 @@
impl Typed for bool {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);
}
+impl IntoUntyped for &bool {
+ fn into_untyped(value: Self) -> Result<Val> {
+ Ok(Val::Bool(*value))
+ }
+}
impl IntoUntyped for bool {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Bool(value))
@@ -764,19 +816,23 @@
}
}
-pub struct Null;
-impl Typed for Null {
+impl Typed for () {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);
}
-impl IntoUntyped for Null {
- fn into_untyped(_: Self) -> Result<Val> {
+impl IntoUntyped for &() {
+ fn into_untyped((): Self) -> Result<Val> {
Ok(Val::Null)
}
}
-impl FromUntyped for Null {
+impl IntoUntyped for () {
+ fn into_untyped((): Self) -> Result<Val> {
+ Ok(Val::Null)
+ }
+}
+impl FromUntyped for () {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
- Ok(Self)
+ Ok(())
}
}
@@ -811,9 +867,9 @@
impl Typed for NumValue {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);
}
-impl IntoUntyped for NumValue {
+impl IntoUntyped for &NumValue {
fn into_untyped(typed: Self) -> Result<Val> {
- Ok(Val::Num(typed))
+ Ok(Val::Num(*typed))
}
}
impl FromUntyped for NumValue {
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -137,7 +137,7 @@
impl<T> Thunk<T>
where
- T: Clone + Trace,
+ T: Trace,
{
pub fn force(&self) -> Result<()> {
self.evaluate()?;
@@ -161,7 +161,7 @@
}
impl<Input> Thunk<Input>
where
- Input: Trace + Clone,
+ Input: Trace,
{
pub fn map<M>(self, mapper: M) -> Thunk<M::Output>
where
@@ -355,7 +355,7 @@
Self::Tree(Rc::new((a, b, len)))
}
}
- pub fn into_flat(self) -> IStr {
+ pub fn into_flat(&self) -> IStr {
#[cold]
fn write_buf(s: &StrValue, out: &mut String) {
match s {
@@ -367,10 +367,10 @@
}
}
match self {
- Self::Flat(f) => f,
+ Self::Flat(f) => f.clone(),
Self::Tree(_) => {
let mut buf = String::with_capacity(self.len());
- write_buf(&self, &mut buf);
+ write_buf(self, &mut buf);
buf.into()
}
}
@@ -701,6 +701,9 @@
{
Ok(Self::Num(num.try_into()?))
}
+ pub fn arr(a: impl ArrayLike) -> Self {
+ Self::Arr(ArrValue::new(a))
+ }
}
impl From<IStr> for Val {
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -34,7 +34,7 @@
for _ in 0..*sz {
out.push(trivial.clone());
}
- Ok(ArrValue::eager(out))
+ Ok(ArrValue::new(out))
},
)
}
@@ -256,7 +256,7 @@
pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {
builtin_join(
IndexableVal::Str("\n".into()),
- ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),
+ ArrValue::extended(arr, ArrValue::new(vec![Val::string("")])),
)
}
@@ -468,7 +468,7 @@
out.push(ele);
}
}
- Val::Arr(ArrValue::eager(out))
+ Val::arr(out)
}
Val::Obj(o) => {
let mut out = ObjValueBuilder::new();
crates/jrsonnet-stdlib/src/sets.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/sets.rs
+++ b/crates/jrsonnet-stdlib/src/sets.rs
@@ -29,7 +29,11 @@
#[builtin]
#[allow(non_snake_case, clippy::redundant_closure)]
-pub fn builtin_set_inter(a: ArrValue, b: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {
+pub fn builtin_set_inter(
+ a: ArrValue,
+ b: ArrValue,
+ #[default] keyF: KeyF,
+) -> Result<Vec<Thunk<Val>>> {
let mut a = a.iter_lazy();
let mut b = b.iter_lazy();
@@ -60,12 +64,16 @@
}
}
}
- Ok(ArrValue::lazy(out))
+ Ok(out)
}
#[builtin]
#[allow(non_snake_case, clippy::redundant_closure)]
-pub fn builtin_set_diff(a: ArrValue, b: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {
+pub fn builtin_set_diff(
+ a: ArrValue,
+ b: ArrValue,
+ #[default] keyF: KeyF,
+) -> Result<Vec<Thunk<Val>>> {
let mut a = a.iter_lazy();
let mut b = b.iter_lazy();
@@ -103,12 +111,16 @@
av = a.next();
ak = av.clone().map(keyF).transpose()?;
}
- Ok(ArrValue::lazy(out))
+ Ok(out)
}
#[builtin]
#[allow(non_snake_case, clippy::redundant_closure)]
-pub fn builtin_set_union(a: ArrValue, b: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {
+pub fn builtin_set_union(
+ a: ArrValue,
+ b: ArrValue,
+ #[default] keyF: KeyF,
+) -> Result<Vec<Thunk<Val>>> {
let mut a = a.iter_lazy();
let mut b = b.iter_lazy();
@@ -154,5 +166,5 @@
bv = b.next();
bk = bv.clone().map(keyF).transpose()?;
}
- Ok(ArrValue::lazy(out))
+ Ok(out)
}
crates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth1#![allow(non_snake_case)]23use std::cmp::Ordering;45use jrsonnet_evaluator::{6 Result, Thunk, Val, bail,7 function::builtin,8 operator::evaluate_compare_op,9 val::{ArrValue, equals},10};11use jrsonnet_ir::BinaryOpType;1213use crate::{eval_on_empty, keyf::KeyF};1415#[derive(Copy, Clone)]16enum SortKeyType {17 Number,18 String,19 Unspecialized,20 Unknown,21}2223fn get_sort_type<T>(values: &[T], key_getter: impl Fn(&T) -> &Val) -> Result<SortKeyType> {24 let mut sort_type = SortKeyType::Unknown;25 for i in values {26 let i = key_getter(i);27 match (i, sort_type) {28 (Val::Str(_), SortKeyType::Unknown) => sort_type = SortKeyType::String,29 (Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,30 (Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}31 (Val::Str(_) | Val::Num(_), _) => {32 bail!("sort elements should have the same types")33 }34 (_, _) => return Ok(SortKeyType::Unspecialized),35 }36 }37 Ok(sort_type)38}3940fn sort_identity(mut values: Vec<Val>) -> Result<Vec<Val>> {41 // Fast path, identity key getter42 let sort_type = get_sort_type(&values, |k| k)?;43 match sort_type {44 SortKeyType::Number => values.sort_unstable_by_key(|v| match v {45 Val::Num(n) => *n,46 _ => unreachable!(),47 }),48 SortKeyType::String => values.sort_unstable_by_key(|v| match v {49 Val::Str(s) => s.clone(),50 _ => unreachable!(),51 }),52 SortKeyType::Unknown | SortKeyType::Unspecialized => {53 let mut err = None;54 // evaluate_compare_op will never return equal on types, which are different from55 // jsonnet perspective56 values.sort_unstable_by(|a, b| match evaluate_compare_op(a, b, BinaryOpType::Lt) {57 Ok(ord) => ord,58 Err(e) if err.is_none() => {59 let _ = err.insert(e);60 Ordering::Equal61 }62 Err(_) => Ordering::Equal,63 });64 if let Some(err) = err {65 return Err(err);66 }67 }68 }69 Ok(values)70}7172fn sort_keyf(values: ArrValue, keyf: KeyF) -> Result<Vec<Thunk<Val>>> {73 // Slow path, user provided key getter74 let mut vk = Vec::with_capacity(values.len());75 for value in values.iter_lazy() {76 vk.push((value.clone(), keyf.eval(value)?));77 }78 let sort_type = get_sort_type(&vk, |v| &v.1)?;79 match sort_type {80 SortKeyType::Number => vk.sort_by_key(|v| match v.1 {81 Val::Num(n) => n,82 _ => unreachable!(),83 }),84 SortKeyType::String => vk.sort_by_key(|v| match &v.1 {85 Val::Str(s) => s.clone(),86 _ => unreachable!(),87 }),88 SortKeyType::Unknown | SortKeyType::Unspecialized => {89 let mut err = None;90 // evaluate_compare_op will never return equal on types, which are different from91 // jsonnet perspective92 vk.sort_by(93 |(_a, ak), (_b, bk)| match evaluate_compare_op(ak, bk, BinaryOpType::Lt) {94 Ok(ord) => ord,95 Err(e) if err.is_none() => {96 let _ = err.insert(e);97 Ordering::Equal98 }99 Err(_) => Ordering::Equal,100 },101 );102 if let Some(err) = err {103 return Err(err);104 }105 }106 }107 Ok(vk.into_iter().map(|v| v.0).collect())108}109110/// * `key_getter` - None, if identity sort required111pub fn sort(values: ArrValue, key_getter: KeyF) -> Result<ArrValue> {112 if values.len() <= 1 {113 return Ok(values);114 }115 if key_getter.is_identity() {116 Ok(ArrValue::eager(sort_identity(117 values.iter().collect::<Result<Vec<Val>>>()?,118 )?))119 } else {120 Ok(ArrValue::lazy(sort_keyf(values, key_getter)?))121 }122}123124#[builtin]125pub fn builtin_sort(arr: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {126 super::sort::sort(arr, keyF)127}128129fn uniq_identity(arr: Vec<Val>) -> Result<Vec<Val>> {130 let mut out = Vec::new();131 let mut last = arr[0].clone();132 out.push(last.clone());133 for next in arr.into_iter().skip(1) {134 if !equals(&last, &next)? {135 out.push(next.clone());136 }137 last = next;138 }139 Ok(out)140}141142fn uniq_keyf(arr: ArrValue, keyf: KeyF) -> Result<Vec<Thunk<Val>>> {143 let mut out = Vec::new();144 let last_value = arr.get_lazy(0).unwrap();145 let mut last_key = keyf.eval(last_value.clone())?;146 out.push(last_value);147148 for next in arr.iter_lazy().skip(1) {149 let next_key = keyf.eval(next.clone())?;150 if !equals(&last_key, &next_key)? {151 out.push(next.clone());152 }153 last_key = next_key;154 }155 Ok(out)156}157158#[builtin]159#[allow(non_snake_case)]160pub fn builtin_uniq(arr: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {161 if arr.len() <= 1 {162 return Ok(arr);163 }164 if keyF.is_identity() {165 Ok(ArrValue::eager(uniq_identity(166 arr.iter().collect::<Result<Vec<Val>>>()?,167 )?))168 } else {169 Ok(ArrValue::lazy(uniq_keyf(arr, keyF)?))170 }171}172173#[builtin]174#[allow(non_snake_case)]175pub fn builtin_set(arr: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {176 if arr.len() <= 1 {177 return Ok(arr);178 }179 if keyF.is_identity() {180 let arr = arr.iter().collect::<Result<Vec<Val>>>()?;181 let arr = sort_identity(arr)?;182 let arr = uniq_identity(arr)?;183 Ok(ArrValue::eager(arr))184 } else {185 let arr = sort_keyf(arr, keyF.clone())?;186 let arr = uniq_keyf(ArrValue::lazy(arr), keyF)?;187 Ok(ArrValue::lazy(arr))188 }189}190191fn array_top1(arr: ArrValue, keyf: KeyF, ordering: Ordering) -> Result<Val> {192 let mut iter = arr.iter();193 let mut min = iter.next().expect("not empty")?;194 let mut min_key = keyf.eval(Thunk::evaluated(min.clone()))?;195 for item in iter {196 let cur = item?;197 let cur_key = keyf.eval(Thunk::evaluated(cur.clone()))?;198 if evaluate_compare_op(&cur_key, &min_key, BinaryOpType::Lt)? == ordering {199 min = cur;200 min_key = cur_key;201 }202 }203 Ok(min)204}205206#[builtin]207pub fn builtin_min_array(208 arr: ArrValue,209 #[default] keyF: KeyF,210 onEmpty: Option<Thunk<Val>>,211) -> Result<Val> {212 if arr.is_empty() {213 return eval_on_empty(onEmpty);214 }215 array_top1(arr, keyF, Ordering::Less)216}217#[builtin]218pub fn builtin_max_array(219 arr: ArrValue,220 #[default] keyF: KeyF,221 onEmpty: Option<Thunk<Val>>,222) -> Result<Val> {223 if arr.is_empty() {224 return eval_on_empty(onEmpty);225 }226 array_top1(arr, keyF, Ordering::Greater)227}1#![allow(non_snake_case)]23use std::cmp::Ordering;45use jrsonnet_evaluator::{6 Result, Thunk, Val, bail,7 function::builtin,8 operator::evaluate_compare_op,9 val::{ArrValue, equals},10};11use jrsonnet_ir::BinaryOpType;1213use crate::{eval_on_empty, keyf::KeyF};1415#[derive(Copy, Clone)]16enum SortKeyType {17 Number,18 String,19 Unspecialized,20 Unknown,21}2223fn get_sort_type<T>(values: &[T], key_getter: impl Fn(&T) -> &Val) -> Result<SortKeyType> {24 let mut sort_type = SortKeyType::Unknown;25 for i in values {26 let i = key_getter(i);27 match (i, sort_type) {28 (Val::Str(_), SortKeyType::Unknown) => sort_type = SortKeyType::String,29 (Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,30 (Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}31 (Val::Str(_) | Val::Num(_), _) => {32 bail!("sort elements should have the same types")33 }34 (_, _) => return Ok(SortKeyType::Unspecialized),35 }36 }37 Ok(sort_type)38}3940fn sort_identity(mut values: Vec<Val>) -> Result<Vec<Val>> {41 // Fast path, identity key getter42 let sort_type = get_sort_type(&values, |k| k)?;43 match sort_type {44 SortKeyType::Number => values.sort_unstable_by_key(|v| match v {45 Val::Num(n) => *n,46 _ => unreachable!(),47 }),48 SortKeyType::String => values.sort_unstable_by_key(|v| match v {49 Val::Str(s) => s.clone(),50 _ => unreachable!(),51 }),52 SortKeyType::Unknown | SortKeyType::Unspecialized => {53 let mut err = None;54 // evaluate_compare_op will never return equal on types, which are different from55 // jsonnet perspective56 values.sort_unstable_by(|a, b| match evaluate_compare_op(a, b, BinaryOpType::Lt) {57 Ok(ord) => ord,58 Err(e) if err.is_none() => {59 let _ = err.insert(e);60 Ordering::Equal61 }62 Err(_) => Ordering::Equal,63 });64 if let Some(err) = err {65 return Err(err);66 }67 }68 }69 Ok(values)70}7172fn sort_keyf(values: ArrValue, keyf: KeyF) -> Result<Vec<Thunk<Val>>> {73 // Slow path, user provided key getter74 let mut vk = Vec::with_capacity(values.len());75 for value in values.iter_lazy() {76 vk.push((value.clone(), keyf.eval(value)?));77 }78 let sort_type = get_sort_type(&vk, |v| &v.1)?;79 match sort_type {80 SortKeyType::Number => vk.sort_by_key(|v| match v.1 {81 Val::Num(n) => n,82 _ => unreachable!(),83 }),84 SortKeyType::String => vk.sort_by_key(|v| match &v.1 {85 Val::Str(s) => s.clone(),86 _ => unreachable!(),87 }),88 SortKeyType::Unknown | SortKeyType::Unspecialized => {89 let mut err = None;90 // evaluate_compare_op will never return equal on types, which are different from91 // jsonnet perspective92 vk.sort_by(93 |(_a, ak), (_b, bk)| match evaluate_compare_op(ak, bk, BinaryOpType::Lt) {94 Ok(ord) => ord,95 Err(e) if err.is_none() => {96 let _ = err.insert(e);97 Ordering::Equal98 }99 Err(_) => Ordering::Equal,100 },101 );102 if let Some(err) = err {103 return Err(err);104 }105 }106 }107 Ok(vk.into_iter().map(|v| v.0).collect())108}109110/// * `key_getter` - None, if identity sort required111pub fn sort(values: ArrValue, key_getter: KeyF) -> Result<ArrValue> {112 if values.len() <= 1 {113 return Ok(values);114 }115 if key_getter.is_identity() {116 Ok(ArrValue::new(sort_identity(117 values.iter().collect::<Result<Vec<Val>>>()?,118 )?))119 } else {120 Ok(ArrValue::new(sort_keyf(values, key_getter)?))121 }122}123124#[builtin]125pub fn builtin_sort(arr: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {126 super::sort::sort(arr, keyF)127}128129fn uniq_identity(arr: Vec<Val>) -> Result<Vec<Val>> {130 let mut out = Vec::new();131 let mut last = arr[0].clone();132 out.push(last.clone());133 for next in arr.into_iter().skip(1) {134 if !equals(&last, &next)? {135 out.push(next.clone());136 }137 last = next;138 }139 Ok(out)140}141142fn uniq_keyf(arr: ArrValue, keyf: KeyF) -> Result<Vec<Thunk<Val>>> {143 let mut out = Vec::new();144 let last_value = arr.get_lazy(0).unwrap();145 let mut last_key = keyf.eval(last_value.clone())?;146 out.push(last_value);147148 for next in arr.iter_lazy().skip(1) {149 let next_key = keyf.eval(next.clone())?;150 if !equals(&last_key, &next_key)? {151 out.push(next.clone());152 }153 last_key = next_key;154 }155 Ok(out)156}157158#[builtin]159#[allow(non_snake_case)]160pub fn builtin_uniq(arr: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {161 if arr.len() <= 1 {162 return Ok(arr);163 }164 if keyF.is_identity() {165 Ok(ArrValue::new(uniq_identity(166 arr.iter().collect::<Result<Vec<Val>>>()?,167 )?))168 } else {169 Ok(ArrValue::new(uniq_keyf(arr, keyF)?))170 }171}172173#[builtin]174#[allow(non_snake_case)]175pub fn builtin_set(arr: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {176 if arr.len() <= 1 {177 return Ok(arr);178 }179 if keyF.is_identity() {180 let arr = arr.iter().collect::<Result<Vec<Val>>>()?;181 let arr = sort_identity(arr)?;182 let arr = uniq_identity(arr)?;183 Ok(ArrValue::new(arr))184 } else {185 let arr = sort_keyf(arr, keyF.clone())?;186 let arr = uniq_keyf(ArrValue::new(arr), keyF)?;187 Ok(ArrValue::new(arr))188 }189}190191fn array_top1(arr: ArrValue, keyf: KeyF, ordering: Ordering) -> Result<Val> {192 let mut iter = arr.iter();193 let mut min = iter.next().expect("not empty")?;194 let mut min_key = keyf.eval(Thunk::evaluated(min.clone()))?;195 for item in iter {196 let cur = item?;197 let cur_key = keyf.eval(Thunk::evaluated(cur.clone()))?;198 if evaluate_compare_op(&cur_key, &min_key, BinaryOpType::Lt)? == ordering {199 min = cur;200 min_key = cur_key;201 }202 }203 Ok(min)204}205206#[builtin]207pub fn builtin_min_array(208 arr: ArrValue,209 #[default] keyF: KeyF,210 onEmpty: Option<Thunk<Val>>,211) -> Result<Val> {212 if arr.is_empty() {213 return eval_on_empty(onEmpty);214 }215 array_top1(arr, keyF, Ordering::Less)216}217#[builtin]218pub fn builtin_max_array(219 arr: ArrValue,220 #[default] keyF: KeyF,221 onEmpty: Option<Thunk<Val>>,222) -> Result<Val> {223 if arr.is_empty() {224 return eval_on_empty(onEmpty);225 }226 array_top1(arr, keyF, Ordering::Greater)227}