difftreelog
style fix warnings with nightly clippy
in: master
9 files changed
crates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -9,7 +9,7 @@
use crate::ConfigureState;
-#[derive(PartialEq)]
+#[derive(PartialEq, Eq)]
pub enum TraceFormatName {
Compact,
Explaining,
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -13,20 +13,20 @@
fn into_untyped(value: Self, s: State) -> Result<Val> {
Ok(match value {
- Value::Null => Val::Null,
- Value::Bool(v) => Val::Bool(v),
- Value::Number(n) => Val::Num(n.as_f64().ok_or_else(|| {
+ Self::Null => Val::Null,
+ Self::Bool(v) => Val::Bool(v),
+ Self::Number(n) => Val::Num(n.as_f64().ok_or_else(|| {
RuntimeError(format!("json number can't be represented as jsonnet: {}", n).into())
})?),
- Value::String(s) => Val::Str((&s as &str).into()),
- Value::Array(a) => {
+ Self::String(s) => Val::Str((&s as &str).into()),
+ Self::Array(a) => {
let mut out: Vec<Val> = Vec::with_capacity(a.len());
for v in a {
out.push(Self::into_untyped(v, s.clone())?);
}
Val::Arr(out.into())
}
- Value::Object(o) => {
+ Self::Object(o) => {
let mut builder = ObjValueBuilder::with_capacity(o.len());
for (k, v) in o {
builder
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -17,6 +17,11 @@
clippy::cast_possible_wrap,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
+ // False positives
+ // https://github.com/rust-lang/rust-clippy/issues/6902
+ clippy::use_self,
+ // https://github.com/rust-lang/rust-clippy/issues/8539
+ clippy::iter_with_drain,
)]
// For jrsonnet-macros
crates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -120,7 +120,7 @@
Ok((out, &str[i..]))
}
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Eq)]
pub enum Width {
Star,
Fixed(usize),
@@ -174,7 +174,7 @@
Ok(((), &str[idx..]))
}
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Eq)]
pub enum ConvTypeV {
Decimal,
Octal,
crates/jrsonnet-evaluator/src/stdlib/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
@@ -3,7 +3,7 @@
throw, State, Val,
};
-#[derive(PartialEq, Clone, Copy)]
+#[derive(PartialEq, Eq, Clone, Copy)]
pub enum ManifestType {
// Applied in manifestification
Manifest,
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ b/crates/jrsonnet-evaluator/src/trace/location.rs
@@ -1,5 +1,5 @@
#[allow(clippy::module_name_repetitions)]
-#[derive(Clone, PartialEq, Debug)]
+#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CodeLocation {
pub offset: usize,
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth1use std::{cell::RefCell, fmt::Debug, rc::Rc};23use gcmodule::{Cc, Trace};4use jrsonnet_interner::{IBytes, IStr};5use jrsonnet_types::ValType;67use crate::{8 cc_ptr_eq,9 error::{Error::*, LocError},10 function::FuncVal,11 gc::{GcHashMap, TraceBox},12 stdlib::manifest::{13 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,14 },15 throw, ObjValue, Result, State, Unbound, WeakObjValue,16};1718pub trait ThunkValue: Trace {19 type Output;20 fn get(self: Box<Self>, s: State) -> Result<Self::Output>;21}2223#[derive(Trace)]24enum ThunkInner<T> {25 Computed(T),26 Errored(LocError),27 Waiting(TraceBox<dyn ThunkValue<Output = T>>),28 Pending,29}3031#[allow(clippy::module_name_repetitions)]32#[derive(Clone, Trace)]33pub struct Thunk<T>(Cc<RefCell<ThunkInner<T>>>);3435impl<T> Thunk<T>36where37 T: Clone + Trace,38{39 pub fn new(f: TraceBox<dyn ThunkValue<Output = T>>) -> Self {40 Self(Cc::new(RefCell::new(ThunkInner::Waiting(f))))41 }42 pub fn evaluated(val: T) -> Self {43 Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))44 }45 pub fn force(&self, s: State) -> Result<()> {46 self.evaluate(s)?;47 Ok(())48 }49 pub fn evaluate(&self, s: State) -> Result<T> {50 match &*self.0.borrow() {51 ThunkInner::Computed(v) => return Ok(v.clone()),52 ThunkInner::Errored(e) => return Err(e.clone()),53 ThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),54 ThunkInner::Waiting(..) => (),55 };56 let value = if let ThunkInner::Waiting(value) =57 std::mem::replace(&mut *self.0.borrow_mut(), ThunkInner::Pending)58 {59 value60 } else {61 unreachable!()62 };63 let new_value = match value.0.get(s) {64 Ok(v) => v,65 Err(e) => {66 *self.0.borrow_mut() = ThunkInner::Errored(e.clone());67 return Err(e);68 }69 };70 *self.0.borrow_mut() = ThunkInner::Computed(new_value.clone());71 Ok(new_value)72 }73}7475type CacheKey = (Option<WeakObjValue>, Option<WeakObjValue>);7677#[derive(Trace, Clone)]78pub struct CachedUnbound<I, T>79where80 I: Unbound<Bound = T>,81{82 cache: Cc<RefCell<GcHashMap<CacheKey, T>>>,83 value: I,84}85impl<I: Unbound<Bound = T>, T: Trace> CachedUnbound<I, T> {86 pub fn new(value: I) -> Self {87 Self {88 cache: Cc::new(RefCell::new(GcHashMap::new())),89 value,90 }91 }92}93impl<I: Unbound<Bound = T>, T: Clone + Trace> Unbound for CachedUnbound<I, T> {94 type Bound = T;95 fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<T> {96 let cache_key = (97 sup.as_ref().map(|s| s.clone().downgrade()),98 this.as_ref().map(|t| t.clone().downgrade()),99 );100 {101 if let Some(t) = self.cache.borrow().get(&cache_key) {102 return Ok(t.clone());103 }104 }105 let bound = self.value.bind(s, sup, this)?;106107 {108 let mut cache = self.cache.borrow_mut();109 cache.insert(cache_key, bound.clone());110 }111112 Ok(bound)113 }114}115116impl<T: Debug> Debug for Thunk<T> {117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {118 write!(f, "Lazy")119 }120}121impl<T> PartialEq for Thunk<T> {122 fn eq(&self, other: &Self) -> bool {123 cc_ptr_eq(&self.0, &other.0)124 }125}126127#[derive(Clone)]128pub enum ManifestFormat {129 YamlStream(Box<ManifestFormat>),130 Yaml {131 padding: usize,132 #[cfg(feature = "exp-preserve-order")]133 preserve_order: bool,134 },135 Json {136 padding: usize,137 #[cfg(feature = "exp-preserve-order")]138 preserve_order: bool,139 },140 ToString,141 String,142}143impl ManifestFormat {144 #[cfg(feature = "exp-preserve-order")]145 fn preserve_order(&self) -> bool {146 match self {147 ManifestFormat::YamlStream(s) => s.preserve_order(),148 ManifestFormat::Yaml { preserve_order, .. } => *preserve_order,149 ManifestFormat::Json { preserve_order, .. } => *preserve_order,150 ManifestFormat::ToString => false,151 ManifestFormat::String => false,152 }153 }154}155156#[derive(Debug, Clone, Trace)]157pub struct Slice {158 pub(crate) inner: ArrValue,159 pub(crate) from: u32,160 pub(crate) to: u32,161 pub(crate) step: u32,162}163impl Slice {164 const fn from(&self) -> usize {165 self.from as usize166 }167 const fn to(&self) -> usize {168 self.to as usize169 }170 const fn step(&self) -> usize {171 self.step as usize172 }173 const fn len(&self) -> usize {174 // TODO: use div_ceil175 let diff = self.to() - self.from();176 let rem = diff % self.step();177 let div = diff / self.step();178179 if rem == 0 {180 div181 } else {182 div + 1183 }184 }185}186187#[derive(Debug, Clone, Trace)]188#[force_tracking]189pub enum ArrValue {190 Bytes(#[skip_trace] IBytes),191 Lazy(Cc<Vec<Thunk<Val>>>),192 Eager(Cc<Vec<Val>>),193 Extended(Box<(Self, Self)>),194 Range(i32, i32),195 Slice(Box<Slice>),196 Reversed(Box<Self>),197}198199#[cfg(target_pointer_width = "64")]200static_assertions::assert_eq_size!(ArrValue, [u8; 16]);201202impl ArrValue {203 pub fn new_eager() -> Self {204 Self::Eager(Cc::new(Vec::new()))205 }206207 /// # Panics208 /// If a > b209 pub fn new_range(a: i32, b: i32) -> Self {210 assert!(a <= b);211 Self::Range(a, b)212 }213214 /// # Panics215 /// If passed numbers are incorrect216 #[must_use]217 pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {218 let len = self.len();219 let from = from.unwrap_or(0);220 let to = to.unwrap_or(len).min(len);221 let step = step.unwrap_or(1);222 assert!(from < to);223 assert!(step > 0);224225 Self::Slice(Box::new(Slice {226 inner: self,227 from: from as u32,228 to: to as u32,229 step: step as u32,230 }))231 }232233 pub fn len(&self) -> usize {234 match self {235 Self::Bytes(i) => i.len(),236 Self::Lazy(l) => l.len(),237 Self::Eager(e) => e.len(),238 Self::Extended(v) => v.0.len() + v.1.len(),239 Self::Range(a, b) => a.abs_diff(*b) as usize + 1,240 Self::Reversed(i) => i.len(),241 Self::Slice(s) => s.len(),242 }243 }244245 pub fn is_empty(&self) -> bool {246 self.len() == 0247 }248249 pub fn get(&self, s: State, index: usize) -> Result<Option<Val>> {250 match self {251 Self::Bytes(i) => i252 .get(index)253 .map_or(Ok(None), |v| Ok(Some(Val::Num(f64::from(*v))))),254 Self::Lazy(vec) => {255 if let Some(v) = vec.get(index) {256 Ok(Some(v.evaluate(s)?))257 } else {258 Ok(None)259 }260 }261 Self::Eager(vec) => Ok(vec.get(index).cloned()),262 Self::Extended(v) => {263 let a_len = v.0.len();264 if a_len > index {265 v.0.get(s, index)266 } else {267 v.1.get(s, index - a_len)268 }269 }270 Self::Range(a, _) => {271 if index >= self.len() {272 return Ok(None);273 }274 Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))275 }276 Self::Reversed(v) => {277 let len = v.len();278 if index >= len {279 return Ok(None);280 }281 v.get(s, len - index - 1)282 }283 Self::Slice(v) => {284 let index = v.from() + index * v.step();285 if index >= v.to() {286 return Ok(None);287 }288 v.inner.get(s, index as usize)289 }290 }291 }292293 pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {294 match self {295 Self::Bytes(i) => i296 .get(index)297 .map(|b| Thunk::evaluated(Val::Num(f64::from(*b)))),298 Self::Lazy(vec) => vec.get(index).cloned(),299 Self::Eager(vec) => vec.get(index).cloned().map(Thunk::evaluated),300 Self::Extended(v) => {301 let a_len = v.0.len();302 if a_len > index {303 v.0.get_lazy(index)304 } else {305 v.1.get_lazy(index - a_len)306 }307 }308 Self::Range(a, _) => {309 if index >= self.len() {310 return None;311 }312 Some(Thunk::evaluated(Val::Num(313 ((*a as isize) + index as isize) as f64,314 )))315 }316 Self::Reversed(v) => {317 let len = v.len();318 if index >= len {319 return None;320 }321 v.get_lazy(len - index - 1)322 }323 Self::Slice(s) => {324 let index = s.from() + index * s.step();325 if index >= s.to() {326 return None;327 }328 s.inner.get_lazy(index as usize)329 }330 }331 }332333 pub fn evaluated(&self, s: State) -> Result<Cc<Vec<Val>>> {334 Ok(match self {335 Self::Bytes(i) => {336 let mut out = Vec::with_capacity(i.len());337 for v in i.iter() {338 out.push(Val::Num(f64::from(*v)));339 }340 Cc::new(out)341 }342 Self::Lazy(vec) => {343 let mut out = Vec::with_capacity(vec.len());344 for item in vec.iter() {345 out.push(item.evaluate(s.clone())?);346 }347 Cc::new(out)348 }349 Self::Eager(vec) => vec.clone(),350 Self::Extended(_v) => {351 let mut out = Vec::with_capacity(self.len());352 for item in self.iter(s) {353 out.push(item?);354 }355 Cc::new(out)356 }357 Self::Range(a, b) => {358 let mut out = Vec::with_capacity(self.len());359 for i in *a..*b {360 out.push(Val::Num(f64::from(i)));361 }362 Cc::new(out)363 }364 Self::Reversed(r) => {365 let mut r = r.evaluated(s)?;366 Cc::update_with(&mut r, |v| v.reverse());367 r368 }369 Self::Slice(v) => {370 let mut out = Vec::with_capacity(v.inner.len());371 for v in v372 .inner373 .iter_lazy()374 .skip(v.from())375 .take(v.to() - v.from())376 .step_by(v.step())377 {378 out.push(v.evaluate(s.clone())?);379 }380 Cc::new(out)381 }382 })383 }384385 pub fn iter(&self, s: State) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {386 (0..self.len()).map(move |idx| match self {387 Self::Bytes(b) => Ok(Val::Num(f64::from(b[idx]))),388 Self::Lazy(l) => l[idx].evaluate(s.clone()),389 Self::Eager(e) => Ok(e[idx].clone()),390 Self::Extended(..) | Self::Range(..) | Self::Reversed(..) | Self::Slice(..) => {391 self.get(s.clone(), idx).map(|e| e.expect("idx < len"))392 }393 })394 }395396 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = Thunk<Val>> + '_ {397 (0..self.len()).map(move |idx| match self {398 Self::Bytes(b) => Thunk::evaluated(Val::Num(f64::from(b[idx]))),399 Self::Lazy(l) => l[idx].clone(),400 Self::Eager(e) => Thunk::evaluated(e[idx].clone()),401 Self::Slice(..) | Self::Extended(..) | Self::Range(..) | Self::Reversed(..) => {402 self.get_lazy(idx).expect("idx < len")403 }404 })405 }406407 #[must_use]408 pub fn reversed(self) -> Self {409 Self::Reversed(Box::new(self))410 }411412 pub fn map(self, s: State, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {413 let mut out = Vec::with_capacity(self.len());414415 for value in self.iter(s) {416 out.push(mapper(value?)?);417 }418419 Ok(Self::Eager(Cc::new(out)))420 }421422 pub fn filter(self, s: State, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {423 let mut out = Vec::with_capacity(self.len());424425 for value in self.iter(s) {426 let value = value?;427 if filter(&value)? {428 out.push(value);429 }430 }431432 Ok(Self::Eager(Cc::new(out)))433 }434435 pub fn ptr_eq(a: &Self, b: &Self) -> bool {436 match (a, b) {437 (Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),438 (Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),439 _ => false,440 }441 }442}443444impl From<Vec<Thunk<Val>>> for ArrValue {445 fn from(v: Vec<Thunk<Val>>) -> Self {446 Self::Lazy(Cc::new(v))447 }448}449450impl From<Vec<Val>> for ArrValue {451 fn from(v: Vec<Val>) -> Self {452 Self::Eager(Cc::new(v))453 }454}455456#[allow(clippy::module_name_repetitions)]457pub enum IndexableVal {458 Str(IStr),459 Arr(ArrValue),460}461462#[derive(Debug, Clone, Trace)]463pub enum Val {464 Bool(bool),465 Null,466 Str(IStr),467 Num(f64),468 Arr(ArrValue),469 Obj(ObjValue),470 Func(FuncVal),471}472473#[cfg(target_pointer_width = "64")]474static_assertions::assert_eq_size!(Val, [u8; 32]);475476impl Val {477 pub const fn as_bool(&self) -> Option<bool> {478 match self {479 Val::Bool(v) => Some(*v),480 _ => None,481 }482 }483 pub const fn as_null(&self) -> Option<()> {484 match self {485 Val::Null => Some(()),486 _ => None,487 }488 }489 pub fn as_str(&self) -> Option<IStr> {490 match self {491 Val::Str(s) => Some(s.clone()),492 _ => None,493 }494 }495 pub const fn as_num(&self) -> Option<f64> {496 match self {497 Val::Num(n) => Some(*n),498 _ => None,499 }500 }501 pub fn as_arr(&self) -> Option<ArrValue> {502 match self {503 Val::Arr(a) => Some(a.clone()),504 _ => None,505 }506 }507 pub fn as_obj(&self) -> Option<ObjValue> {508 match self {509 Val::Obj(o) => Some(o.clone()),510 _ => None,511 }512 }513 pub fn as_func(&self) -> Option<FuncVal> {514 match self {515 Val::Func(f) => Some(f.clone()),516 _ => None,517 }518 }519520 /// Creates `Val::Num` after checking for numeric overflow.521 /// As numbers are `f64`, we can just check for their finity.522 pub fn new_checked_num(num: f64) -> Result<Self> {523 if num.is_finite() {524 Ok(Self::Num(num))525 } else {526 throw!(RuntimeError("overflow".into()))527 }528 }529530 pub const fn value_type(&self) -> ValType {531 match self {532 Self::Str(..) => ValType::Str,533 Self::Num(..) => ValType::Num,534 Self::Arr(..) => ValType::Arr,535 Self::Obj(..) => ValType::Obj,536 Self::Bool(_) => ValType::Bool,537 Self::Null => ValType::Null,538 Self::Func(..) => ValType::Func,539 }540 }541542 pub fn to_string(&self, s: State) -> Result<IStr> {543 Ok(match self {544 Self::Bool(true) => "true".into(),545 Self::Bool(false) => "false".into(),546 Self::Null => "null".into(),547 Self::Str(s) => s.clone(),548 v => manifest_json_ex(549 s,550 v,551 &ManifestJsonOptions {552 padding: "",553 mtype: ManifestType::ToString,554 newline: "\n",555 key_val_sep: ": ",556 #[cfg(feature = "exp-preserve-order")]557 preserve_order: false,558 },559 )?560 .into(),561 })562 }563564 /// Expects value to be object, outputs (key, manifested value) pairs565 pub fn manifest_multi(&self, s: State, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {566 let obj = match self {567 Self::Obj(obj) => obj,568 _ => throw!(MultiManifestOutputIsNotAObject),569 };570 let keys = obj.fields(571 #[cfg(feature = "exp-preserve-order")]572 ty.preserve_order(),573 );574 let mut out = Vec::with_capacity(keys.len());575 for key in keys {576 let value = obj577 .get(s.clone(), key.clone())?578 .expect("item in object")579 .manifest(s.clone(), ty)?;580 out.push((key, value));581 }582 Ok(out)583 }584585 /// Expects value to be array, outputs manifested values586 pub fn manifest_stream(&self, s: State, ty: &ManifestFormat) -> Result<Vec<IStr>> {587 let arr = match self {588 Self::Arr(a) => a,589 _ => throw!(StreamManifestOutputIsNotAArray),590 };591 let mut out = Vec::with_capacity(arr.len());592 for i in arr.iter(s.clone()) {593 out.push(i?.manifest(s.clone(), ty)?);594 }595 Ok(out)596 }597598 pub fn manifest(&self, s: State, ty: &ManifestFormat) -> Result<IStr> {599 Ok(match ty {600 ManifestFormat::YamlStream(format) => {601 let arr = match self {602 Self::Arr(a) => a,603 _ => throw!(StreamManifestOutputIsNotAArray),604 };605 let mut out = String::new();606607 match format as &ManifestFormat {608 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),609 ManifestFormat::String => throw!(StreamManifestCannotNestString),610 _ => {}611 };612613 if !arr.is_empty() {614 for v in arr.iter(s.clone()) {615 out.push_str("---\n");616 out.push_str(&v?.manifest(s.clone(), format)?);617 out.push('\n');618 }619 out.push_str("...");620 }621622 out.into()623 }624 ManifestFormat::Yaml {625 padding,626 #[cfg(feature = "exp-preserve-order")]627 preserve_order,628 } => self.to_yaml(629 s,630 *padding,631 #[cfg(feature = "exp-preserve-order")]632 *preserve_order,633 )?,634 ManifestFormat::Json {635 padding,636 #[cfg(feature = "exp-preserve-order")]637 preserve_order,638 } => self.to_json(639 s,640 *padding,641 #[cfg(feature = "exp-preserve-order")]642 *preserve_order,643 )?,644 ManifestFormat::ToString => self.to_string(s)?,645 ManifestFormat::String => match self {646 Self::Str(s) => s.clone(),647 _ => throw!(StringManifestOutputIsNotAString),648 },649 })650 }651652 /// For manifestification653 pub fn to_json(654 &self,655 s: State,656 padding: usize,657 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,658 ) -> Result<IStr> {659 manifest_json_ex(660 s,661 self,662 &ManifestJsonOptions {663 padding: &" ".repeat(padding),664 mtype: if padding == 0 {665 ManifestType::Minify666 } else {667 ManifestType::Manifest668 },669 newline: "\n",670 key_val_sep: ": ",671 #[cfg(feature = "exp-preserve-order")]672 preserve_order,673 },674 )675 .map(Into::into)676 }677678 /// Calls `std.manifestJson`679 pub fn to_std_json(680 &self,681 s: State,682 padding: usize,683 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,684 ) -> Result<Rc<str>> {685 manifest_json_ex(686 s,687 self,688 &ManifestJsonOptions {689 padding: &" ".repeat(padding),690 mtype: ManifestType::Std,691 newline: "\n",692 key_val_sep: ": ",693 #[cfg(feature = "exp-preserve-order")]694 preserve_order,695 },696 )697 .map(Into::into)698 }699700 pub fn to_yaml(701 &self,702 s: State,703 padding: usize,704 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,705 ) -> Result<IStr> {706 let padding = &" ".repeat(padding);707 manifest_yaml_ex(708 s,709 self,710 &ManifestYamlOptions {711 padding,712 arr_element_padding: padding,713 quote_keys: false,714 #[cfg(feature = "exp-preserve-order")]715 preserve_order,716 },717 )718 .map(Into::into)719 }720 pub fn into_indexable(self) -> Result<IndexableVal> {721 Ok(match self {722 Val::Str(s) => IndexableVal::Str(s),723 Val::Arr(arr) => IndexableVal::Arr(arr),724 _ => throw!(ValueIsNotIndexable(self.value_type())),725 })726 }727}728729const fn is_function_like(val: &Val) -> bool {730 matches!(val, Val::Func(_))731}732733/// Native implementation of `std.primitiveEquals`734pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {735 Ok(match (val_a, val_b) {736 (Val::Bool(a), Val::Bool(b)) => a == b,737 (Val::Null, Val::Null) => true,738 (Val::Str(a), Val::Str(b)) => a == b,739 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,740 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(741 "primitiveEquals operates on primitive types, got array".into(),742 )),743 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(744 "primitiveEquals operates on primitive types, got object".into(),745 )),746 (a, b) if is_function_like(a) && is_function_like(b) => {747 throw!(RuntimeError("cannot test equality of functions".into()))748 }749 (_, _) => false,750 })751}752753/// Native implementation of `std.equals`754pub fn equals(s: State, val_a: &Val, val_b: &Val) -> Result<bool> {755 if val_a.value_type() != val_b.value_type() {756 return Ok(false);757 }758 match (val_a, val_b) {759 (Val::Arr(a), Val::Arr(b)) => {760 if ArrValue::ptr_eq(a, b) {761 return Ok(true);762 }763 if a.len() != b.len() {764 return Ok(false);765 }766 for (a, b) in a.iter(s.clone()).zip(b.iter(s.clone())) {767 if !equals(s.clone(), &a?, &b?)? {768 return Ok(false);769 }770 }771 Ok(true)772 }773 (Val::Obj(a), Val::Obj(b)) => {774 if ObjValue::ptr_eq(a, b) {775 return Ok(true);776 }777 let fields = a.fields(778 #[cfg(feature = "exp-preserve-order")]779 false,780 );781 if fields782 != b.fields(783 #[cfg(feature = "exp-preserve-order")]784 false,785 ) {786 return Ok(false);787 }788 for field in fields {789 if !equals(790 s.clone(),791 &a.get(s.clone(), field.clone())?.expect("field exists"),792 &b.get(s.clone(), field)?.expect("field exists"),793 )? {794 return Ok(false);795 }796 }797 Ok(true)798 }799 (a, b) => Ok(primitive_equals(a, b)?),800 }801}1use std::{cell::RefCell, fmt::Debug, rc::Rc};23use gcmodule::{Cc, Trace};4use jrsonnet_interner::{IBytes, IStr};5use jrsonnet_types::ValType;67use crate::{8 cc_ptr_eq,9 error::{Error::*, LocError},10 function::FuncVal,11 gc::{GcHashMap, TraceBox},12 stdlib::manifest::{13 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,14 },15 throw, ObjValue, Result, State, Unbound, WeakObjValue,16};1718pub trait ThunkValue: Trace {19 type Output;20 fn get(self: Box<Self>, s: State) -> Result<Self::Output>;21}2223#[derive(Trace)]24enum ThunkInner<T> {25 Computed(T),26 Errored(LocError),27 Waiting(TraceBox<dyn ThunkValue<Output = T>>),28 Pending,29}3031#[allow(clippy::module_name_repetitions)]32#[derive(Clone, Trace)]33pub struct Thunk<T>(Cc<RefCell<ThunkInner<T>>>);3435impl<T> Thunk<T>36where37 T: Clone + Trace,38{39 pub fn new(f: TraceBox<dyn ThunkValue<Output = T>>) -> Self {40 Self(Cc::new(RefCell::new(ThunkInner::Waiting(f))))41 }42 pub fn evaluated(val: T) -> Self {43 Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))44 }45 pub fn force(&self, s: State) -> Result<()> {46 self.evaluate(s)?;47 Ok(())48 }49 pub fn evaluate(&self, s: State) -> Result<T> {50 match &*self.0.borrow() {51 ThunkInner::Computed(v) => return Ok(v.clone()),52 ThunkInner::Errored(e) => return Err(e.clone()),53 ThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),54 ThunkInner::Waiting(..) => (),55 };56 let value = if let ThunkInner::Waiting(value) =57 std::mem::replace(&mut *self.0.borrow_mut(), ThunkInner::Pending)58 {59 value60 } else {61 unreachable!()62 };63 let new_value = match value.0.get(s) {64 Ok(v) => v,65 Err(e) => {66 *self.0.borrow_mut() = ThunkInner::Errored(e.clone());67 return Err(e);68 }69 };70 *self.0.borrow_mut() = ThunkInner::Computed(new_value.clone());71 Ok(new_value)72 }73}7475type CacheKey = (Option<WeakObjValue>, Option<WeakObjValue>);7677#[derive(Trace, Clone)]78pub struct CachedUnbound<I, T>79where80 I: Unbound<Bound = T>,81{82 cache: Cc<RefCell<GcHashMap<CacheKey, T>>>,83 value: I,84}85impl<I: Unbound<Bound = T>, T: Trace> CachedUnbound<I, T> {86 pub fn new(value: I) -> Self {87 Self {88 cache: Cc::new(RefCell::new(GcHashMap::new())),89 value,90 }91 }92}93impl<I: Unbound<Bound = T>, T: Clone + Trace> Unbound for CachedUnbound<I, T> {94 type Bound = T;95 fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<T> {96 let cache_key = (97 sup.as_ref().map(|s| s.clone().downgrade()),98 this.as_ref().map(|t| t.clone().downgrade()),99 );100 {101 if let Some(t) = self.cache.borrow().get(&cache_key) {102 return Ok(t.clone());103 }104 }105 let bound = self.value.bind(s, sup, this)?;106107 {108 let mut cache = self.cache.borrow_mut();109 cache.insert(cache_key, bound.clone());110 }111112 Ok(bound)113 }114}115116impl<T: Debug> Debug for Thunk<T> {117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {118 write!(f, "Lazy")119 }120}121impl<T> PartialEq for Thunk<T> {122 fn eq(&self, other: &Self) -> bool {123 cc_ptr_eq(&self.0, &other.0)124 }125}126127#[derive(Clone)]128pub enum ManifestFormat {129 YamlStream(Box<ManifestFormat>),130 Yaml {131 padding: usize,132 #[cfg(feature = "exp-preserve-order")]133 preserve_order: bool,134 },135 Json {136 padding: usize,137 #[cfg(feature = "exp-preserve-order")]138 preserve_order: bool,139 },140 ToString,141 String,142}143impl ManifestFormat {144 #[cfg(feature = "exp-preserve-order")]145 fn preserve_order(&self) -> bool {146 match self {147 ManifestFormat::YamlStream(s) => s.preserve_order(),148 ManifestFormat::Yaml { preserve_order, .. } => *preserve_order,149 ManifestFormat::Json { preserve_order, .. } => *preserve_order,150 ManifestFormat::ToString => false,151 ManifestFormat::String => false,152 }153 }154}155156#[derive(Debug, Clone, Trace)]157pub struct Slice {158 pub(crate) inner: ArrValue,159 pub(crate) from: u32,160 pub(crate) to: u32,161 pub(crate) step: u32,162}163impl Slice {164 const fn from(&self) -> usize {165 self.from as usize166 }167 const fn to(&self) -> usize {168 self.to as usize169 }170 const fn step(&self) -> usize {171 self.step as usize172 }173 const fn len(&self) -> usize {174 // TODO: use div_ceil175 let diff = self.to() - self.from();176 let rem = diff % self.step();177 let div = diff / self.step();178179 if rem == 0 {180 div181 } else {182 div + 1183 }184 }185}186187#[derive(Debug, Clone, Trace)]188#[force_tracking]189pub enum ArrValue {190 Bytes(#[skip_trace] IBytes),191 Lazy(Cc<Vec<Thunk<Val>>>),192 Eager(Cc<Vec<Val>>),193 Extended(Box<(Self, Self)>),194 Range(i32, i32),195 Slice(Box<Slice>),196 Reversed(Box<Self>),197}198199#[cfg(target_pointer_width = "64")]200static_assertions::assert_eq_size!(ArrValue, [u8; 16]);201202impl ArrValue {203 pub fn new_eager() -> Self {204 Self::Eager(Cc::new(Vec::new()))205 }206207 /// # Panics208 /// If a > b209 pub fn new_range(a: i32, b: i32) -> Self {210 assert!(a <= b);211 Self::Range(a, b)212 }213214 /// # Panics215 /// If passed numbers are incorrect216 #[must_use]217 pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {218 let len = self.len();219 let from = from.unwrap_or(0);220 let to = to.unwrap_or(len).min(len);221 let step = step.unwrap_or(1);222 assert!(from < to);223 assert!(step > 0);224225 Self::Slice(Box::new(Slice {226 inner: self,227 from: from as u32,228 to: to as u32,229 step: step as u32,230 }))231 }232233 pub fn len(&self) -> usize {234 match self {235 Self::Bytes(i) => i.len(),236 Self::Lazy(l) => l.len(),237 Self::Eager(e) => e.len(),238 Self::Extended(v) => v.0.len() + v.1.len(),239 Self::Range(a, b) => a.abs_diff(*b) as usize + 1,240 Self::Reversed(i) => i.len(),241 Self::Slice(s) => s.len(),242 }243 }244245 pub fn is_empty(&self) -> bool {246 self.len() == 0247 }248249 pub fn get(&self, s: State, index: usize) -> Result<Option<Val>> {250 match self {251 Self::Bytes(i) => i252 .get(index)253 .map_or(Ok(None), |v| Ok(Some(Val::Num(f64::from(*v))))),254 Self::Lazy(vec) => {255 if let Some(v) = vec.get(index) {256 Ok(Some(v.evaluate(s)?))257 } else {258 Ok(None)259 }260 }261 Self::Eager(vec) => Ok(vec.get(index).cloned()),262 Self::Extended(v) => {263 let a_len = v.0.len();264 if a_len > index {265 v.0.get(s, index)266 } else {267 v.1.get(s, index - a_len)268 }269 }270 Self::Range(a, _) => {271 if index >= self.len() {272 return Ok(None);273 }274 Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))275 }276 Self::Reversed(v) => {277 let len = v.len();278 if index >= len {279 return Ok(None);280 }281 v.get(s, len - index - 1)282 }283 Self::Slice(v) => {284 let index = v.from() + index * v.step();285 if index >= v.to() {286 return Ok(None);287 }288 v.inner.get(s, index as usize)289 }290 }291 }292293 pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {294 match self {295 Self::Bytes(i) => i296 .get(index)297 .map(|b| Thunk::evaluated(Val::Num(f64::from(*b)))),298 Self::Lazy(vec) => vec.get(index).cloned(),299 Self::Eager(vec) => vec.get(index).cloned().map(Thunk::evaluated),300 Self::Extended(v) => {301 let a_len = v.0.len();302 if a_len > index {303 v.0.get_lazy(index)304 } else {305 v.1.get_lazy(index - a_len)306 }307 }308 Self::Range(a, _) => {309 if index >= self.len() {310 return None;311 }312 Some(Thunk::evaluated(Val::Num(313 ((*a as isize) + index as isize) as f64,314 )))315 }316 Self::Reversed(v) => {317 let len = v.len();318 if index >= len {319 return None;320 }321 v.get_lazy(len - index - 1)322 }323 Self::Slice(s) => {324 let index = s.from() + index * s.step();325 if index >= s.to() {326 return None;327 }328 s.inner.get_lazy(index as usize)329 }330 }331 }332333 pub fn evaluated(&self, s: State) -> Result<Cc<Vec<Val>>> {334 Ok(match self {335 Self::Bytes(i) => {336 let mut out = Vec::with_capacity(i.len());337 for v in i.iter() {338 out.push(Val::Num(f64::from(*v)));339 }340 Cc::new(out)341 }342 Self::Lazy(vec) => {343 let mut out = Vec::with_capacity(vec.len());344 for item in vec.iter() {345 out.push(item.evaluate(s.clone())?);346 }347 Cc::new(out)348 }349 Self::Eager(vec) => vec.clone(),350 Self::Extended(_v) => {351 let mut out = Vec::with_capacity(self.len());352 for item in self.iter(s) {353 out.push(item?);354 }355 Cc::new(out)356 }357 Self::Range(a, b) => {358 let mut out = Vec::with_capacity(self.len());359 for i in *a..*b {360 out.push(Val::Num(f64::from(i)));361 }362 Cc::new(out)363 }364 Self::Reversed(r) => {365 let mut r = r.evaluated(s)?;366 Cc::update_with(&mut r, |v| v.reverse());367 r368 }369 Self::Slice(v) => {370 let mut out = Vec::with_capacity(v.inner.len());371 for v in v372 .inner373 .iter_lazy()374 .skip(v.from())375 .take(v.to() - v.from())376 .step_by(v.step())377 {378 out.push(v.evaluate(s.clone())?);379 }380 Cc::new(out)381 }382 })383 }384385 pub fn iter(&self, s: State) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {386 (0..self.len()).map(move |idx| match self {387 Self::Bytes(b) => Ok(Val::Num(f64::from(b[idx]))),388 Self::Lazy(l) => l[idx].evaluate(s.clone()),389 Self::Eager(e) => Ok(e[idx].clone()),390 Self::Extended(..) | Self::Range(..) | Self::Reversed(..) | Self::Slice(..) => {391 self.get(s.clone(), idx).map(|e| e.expect("idx < len"))392 }393 })394 }395396 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = Thunk<Val>> + '_ {397 (0..self.len()).map(move |idx| match self {398 Self::Bytes(b) => Thunk::evaluated(Val::Num(f64::from(b[idx]))),399 Self::Lazy(l) => l[idx].clone(),400 Self::Eager(e) => Thunk::evaluated(e[idx].clone()),401 Self::Slice(..) | Self::Extended(..) | Self::Range(..) | Self::Reversed(..) => {402 self.get_lazy(idx).expect("idx < len")403 }404 })405 }406407 #[must_use]408 pub fn reversed(self) -> Self {409 Self::Reversed(Box::new(self))410 }411412 pub fn map(self, s: State, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {413 let mut out = Vec::with_capacity(self.len());414415 for value in self.iter(s) {416 out.push(mapper(value?)?);417 }418419 Ok(Self::Eager(Cc::new(out)))420 }421422 pub fn filter(self, s: State, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {423 let mut out = Vec::with_capacity(self.len());424425 for value in self.iter(s) {426 let value = value?;427 if filter(&value)? {428 out.push(value);429 }430 }431432 Ok(Self::Eager(Cc::new(out)))433 }434435 pub fn ptr_eq(a: &Self, b: &Self) -> bool {436 match (a, b) {437 (Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),438 (Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),439 _ => false,440 }441 }442}443444impl From<Vec<Thunk<Val>>> for ArrValue {445 fn from(v: Vec<Thunk<Val>>) -> Self {446 Self::Lazy(Cc::new(v))447 }448}449450impl From<Vec<Val>> for ArrValue {451 fn from(v: Vec<Val>) -> Self {452 Self::Eager(Cc::new(v))453 }454}455456#[allow(clippy::module_name_repetitions)]457pub enum IndexableVal {458 Str(IStr),459 Arr(ArrValue),460}461462#[derive(Debug, Clone, Trace)]463pub enum Val {464 Bool(bool),465 Null,466 Str(IStr),467 Num(f64),468 Arr(ArrValue),469 Obj(ObjValue),470 Func(FuncVal),471}472473#[cfg(target_pointer_width = "64")]474static_assertions::assert_eq_size!(Val, [u8; 32]);475476impl Val {477 pub const fn as_bool(&self) -> Option<bool> {478 match self {479 Self::Bool(v) => Some(*v),480 _ => None,481 }482 }483 pub const fn as_null(&self) -> Option<()> {484 match self {485 Self::Null => Some(()),486 _ => None,487 }488 }489 pub fn as_str(&self) -> Option<IStr> {490 match self {491 Self::Str(s) => Some(s.clone()),492 _ => None,493 }494 }495 pub const fn as_num(&self) -> Option<f64> {496 match self {497 Self::Num(n) => Some(*n),498 _ => None,499 }500 }501 pub fn as_arr(&self) -> Option<ArrValue> {502 match self {503 Self::Arr(a) => Some(a.clone()),504 _ => None,505 }506 }507 pub fn as_obj(&self) -> Option<ObjValue> {508 match self {509 Self::Obj(o) => Some(o.clone()),510 _ => None,511 }512 }513 pub fn as_func(&self) -> Option<FuncVal> {514 match self {515 Self::Func(f) => Some(f.clone()),516 _ => None,517 }518 }519520 /// Creates `Val::Num` after checking for numeric overflow.521 /// As numbers are `f64`, we can just check for their finity.522 pub fn new_checked_num(num: f64) -> Result<Self> {523 if num.is_finite() {524 Ok(Self::Num(num))525 } else {526 throw!(RuntimeError("overflow".into()))527 }528 }529530 pub const fn value_type(&self) -> ValType {531 match self {532 Self::Str(..) => ValType::Str,533 Self::Num(..) => ValType::Num,534 Self::Arr(..) => ValType::Arr,535 Self::Obj(..) => ValType::Obj,536 Self::Bool(_) => ValType::Bool,537 Self::Null => ValType::Null,538 Self::Func(..) => ValType::Func,539 }540 }541542 pub fn to_string(&self, s: State) -> Result<IStr> {543 Ok(match self {544 Self::Bool(true) => "true".into(),545 Self::Bool(false) => "false".into(),546 Self::Null => "null".into(),547 Self::Str(s) => s.clone(),548 v => manifest_json_ex(549 s,550 v,551 &ManifestJsonOptions {552 padding: "",553 mtype: ManifestType::ToString,554 newline: "\n",555 key_val_sep: ": ",556 #[cfg(feature = "exp-preserve-order")]557 preserve_order: false,558 },559 )?560 .into(),561 })562 }563564 /// Expects value to be object, outputs (key, manifested value) pairs565 pub fn manifest_multi(&self, s: State, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {566 let obj = match self {567 Self::Obj(obj) => obj,568 _ => throw!(MultiManifestOutputIsNotAObject),569 };570 let keys = obj.fields(571 #[cfg(feature = "exp-preserve-order")]572 ty.preserve_order(),573 );574 let mut out = Vec::with_capacity(keys.len());575 for key in keys {576 let value = obj577 .get(s.clone(), key.clone())?578 .expect("item in object")579 .manifest(s.clone(), ty)?;580 out.push((key, value));581 }582 Ok(out)583 }584585 /// Expects value to be array, outputs manifested values586 pub fn manifest_stream(&self, s: State, ty: &ManifestFormat) -> Result<Vec<IStr>> {587 let arr = match self {588 Self::Arr(a) => a,589 _ => throw!(StreamManifestOutputIsNotAArray),590 };591 let mut out = Vec::with_capacity(arr.len());592 for i in arr.iter(s.clone()) {593 out.push(i?.manifest(s.clone(), ty)?);594 }595 Ok(out)596 }597598 pub fn manifest(&self, s: State, ty: &ManifestFormat) -> Result<IStr> {599 Ok(match ty {600 ManifestFormat::YamlStream(format) => {601 let arr = match self {602 Self::Arr(a) => a,603 _ => throw!(StreamManifestOutputIsNotAArray),604 };605 let mut out = String::new();606607 match format as &ManifestFormat {608 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),609 ManifestFormat::String => throw!(StreamManifestCannotNestString),610 _ => {}611 };612613 if !arr.is_empty() {614 for v in arr.iter(s.clone()) {615 out.push_str("---\n");616 out.push_str(&v?.manifest(s.clone(), format)?);617 out.push('\n');618 }619 out.push_str("...");620 }621622 out.into()623 }624 ManifestFormat::Yaml {625 padding,626 #[cfg(feature = "exp-preserve-order")]627 preserve_order,628 } => self.to_yaml(629 s,630 *padding,631 #[cfg(feature = "exp-preserve-order")]632 *preserve_order,633 )?,634 ManifestFormat::Json {635 padding,636 #[cfg(feature = "exp-preserve-order")]637 preserve_order,638 } => self.to_json(639 s,640 *padding,641 #[cfg(feature = "exp-preserve-order")]642 *preserve_order,643 )?,644 ManifestFormat::ToString => self.to_string(s)?,645 ManifestFormat::String => match self {646 Self::Str(s) => s.clone(),647 _ => throw!(StringManifestOutputIsNotAString),648 },649 })650 }651652 /// For manifestification653 pub fn to_json(654 &self,655 s: State,656 padding: usize,657 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,658 ) -> Result<IStr> {659 manifest_json_ex(660 s,661 self,662 &ManifestJsonOptions {663 padding: &" ".repeat(padding),664 mtype: if padding == 0 {665 ManifestType::Minify666 } else {667 ManifestType::Manifest668 },669 newline: "\n",670 key_val_sep: ": ",671 #[cfg(feature = "exp-preserve-order")]672 preserve_order,673 },674 )675 .map(Into::into)676 }677678 /// Calls `std.manifestJson`679 pub fn to_std_json(680 &self,681 s: State,682 padding: usize,683 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,684 ) -> Result<Rc<str>> {685 manifest_json_ex(686 s,687 self,688 &ManifestJsonOptions {689 padding: &" ".repeat(padding),690 mtype: ManifestType::Std,691 newline: "\n",692 key_val_sep: ": ",693 #[cfg(feature = "exp-preserve-order")]694 preserve_order,695 },696 )697 .map(Into::into)698 }699700 pub fn to_yaml(701 &self,702 s: State,703 padding: usize,704 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,705 ) -> Result<IStr> {706 let padding = &" ".repeat(padding);707 manifest_yaml_ex(708 s,709 self,710 &ManifestYamlOptions {711 padding,712 arr_element_padding: padding,713 quote_keys: false,714 #[cfg(feature = "exp-preserve-order")]715 preserve_order,716 },717 )718 .map(Into::into)719 }720 pub fn into_indexable(self) -> Result<IndexableVal> {721 Ok(match self {722 Val::Str(s) => IndexableVal::Str(s),723 Val::Arr(arr) => IndexableVal::Arr(arr),724 _ => throw!(ValueIsNotIndexable(self.value_type())),725 })726 }727}728729const fn is_function_like(val: &Val) -> bool {730 matches!(val, Val::Func(_))731}732733/// Native implementation of `std.primitiveEquals`734pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {735 Ok(match (val_a, val_b) {736 (Val::Bool(a), Val::Bool(b)) => a == b,737 (Val::Null, Val::Null) => true,738 (Val::Str(a), Val::Str(b)) => a == b,739 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,740 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(741 "primitiveEquals operates on primitive types, got array".into(),742 )),743 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(744 "primitiveEquals operates on primitive types, got object".into(),745 )),746 (a, b) if is_function_like(a) && is_function_like(b) => {747 throw!(RuntimeError("cannot test equality of functions".into()))748 }749 (_, _) => false,750 })751}752753/// Native implementation of `std.equals`754pub fn equals(s: State, val_a: &Val, val_b: &Val) -> Result<bool> {755 if val_a.value_type() != val_b.value_type() {756 return Ok(false);757 }758 match (val_a, val_b) {759 (Val::Arr(a), Val::Arr(b)) => {760 if ArrValue::ptr_eq(a, b) {761 return Ok(true);762 }763 if a.len() != b.len() {764 return Ok(false);765 }766 for (a, b) in a.iter(s.clone()).zip(b.iter(s.clone())) {767 if !equals(s.clone(), &a?, &b?)? {768 return Ok(false);769 }770 }771 Ok(true)772 }773 (Val::Obj(a), Val::Obj(b)) => {774 if ObjValue::ptr_eq(a, b) {775 return Ok(true);776 }777 let fields = a.fields(778 #[cfg(feature = "exp-preserve-order")]779 false,780 );781 if fields782 != b.fields(783 #[cfg(feature = "exp-preserve-order")]784 false,785 ) {786 return Ok(false);787 }788 for field in fields {789 if !equals(790 s.clone(),791 &a.get(s.clone(), field.clone())?.expect("field exists"),792 &b.get(s.clone(), field)?.expect("field exists"),793 )? {794 return Ok(false);795 }796 }797 Ok(true)798 }799 (a, b) => Ok(primitive_equals(a, b)?),800 }801}crates/jrsonnet-interner/src/inner.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/inner.rs
+++ b/crates/jrsonnet-interner/src/inner.rs
@@ -157,7 +157,7 @@
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
a.0 == b.0
}
- pub fn as_ptr(this: &Self) -> *const u8 {
+ pub const fn as_ptr(this: &Self) -> *const u8 {
// SAFETY: data is initialized
unsafe { this.0.as_ptr().add(mem::size_of::<InnerHeader>()) }
}
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -21,7 +21,7 @@
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(Debug, Clone, Copy, PartialEq, Trace)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]
pub enum Visibility {
/// :
Normal,
@@ -60,7 +60,7 @@
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(Debug, Clone, Copy, PartialEq, Trace)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]
pub enum UnaryOpType {
Plus,
Minus,
@@ -85,7 +85,7 @@
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(Debug, Clone, Copy, PartialEq, Trace)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]
pub enum BinaryOpType {
Mul,
Div,
@@ -179,7 +179,7 @@
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(Debug, Clone, PartialEq, Trace)]
+#[derive(Debug, Clone, PartialEq, Eq, Trace)]
pub enum DestructRest {
/// ...rest
Keep(IStr),
@@ -188,7 +188,7 @@
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(Debug, Clone, PartialEq, Trace)]
+#[derive(Debug, Clone, PartialEq, Eq, Trace)]
pub enum Destruct {
Full(IStr),
#[cfg(feature = "exp-destruct")]
@@ -263,7 +263,7 @@
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(Debug, PartialEq, Clone, Copy, Trace)]
+#[derive(Debug, PartialEq, Eq, Clone, Copy, Trace)]
pub enum LiteralType {
This,
Super,
@@ -357,7 +357,7 @@
/// file, begin offset, end offset
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(Clone, PartialEq, Trace)]
+#[derive(Clone, PartialEq, Eq, Trace)]
#[skip_trace]
#[repr(C)]
pub struct ExprLocation(pub Source, pub u32, pub u32);