From 6a2bca3d52a30c6729c3867766976ec1865fdf46 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 19 Mar 2026 03:55:32 +0000 Subject: [PATCH] refactor: no need to store multiple assertions for oopobject --- --- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs +++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs @@ -597,13 +597,7 @@ || "error statement".to_owned(), || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)), )?, - IfElse (if_else) - // { - // cond, - // cond_then, - // cond_else, - // } - => { + IfElse(if_else) => { if in_frame( CallLocation::new(&loc), || "if condition".to_owned(), --- a/crates/jrsonnet-evaluator/src/obj.rs +++ b/crates/jrsonnet-evaluator/src/obj.rs @@ -151,7 +151,7 @@ #[derive(Trace, Default)] #[trace(tracking(force))] pub struct OopObject { - assertions: Vec, + assertion: Option, this_entries: FxHashMap, } impl Debug for OopObject { @@ -163,7 +163,7 @@ } impl OopObject { fn is_empty(&self) -> bool { - self.assertions.is_empty() && self.this_entries.is_empty() + self.assertion.is_none() && self.this_entries.is_empty() } } @@ -979,11 +979,11 @@ impl OopObject { pub fn new( this_entries: FxHashMap, - assertions: Vec, + assertion: Option, ) -> Self { Self { this_entries, - assertions, + assertion, } } } @@ -1042,10 +1042,7 @@ } fn run_assertions_core(&self, sup_this: SupThis) -> Result<()> { - if self.assertions.is_empty() { - return Ok(()); - } - for assertion in self.assertions.iter() { + if let Some(assertion) = &self.assertion { assertion.0.run(sup_this.clone())?; } Ok(()) @@ -1067,7 +1064,7 @@ Self { sup: vec![], new: OopObject { - assertions: vec![], + assertion: None, this_entries: FxHashMap::with_capacity(capacity), }, next_field_index: FieldIndex::default(), @@ -1075,10 +1072,6 @@ } pub fn reserve_cores(&mut self, capacity: usize) -> &mut Self { self.sup.reserve_exact(capacity); - self - } - pub fn reserve_asserts(&mut self, capacity: usize) -> &mut Self { - self.new.assertions.reserve_exact(capacity); self } pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self { @@ -1087,7 +1080,11 @@ } pub fn assert(&mut self, assertion: impl ObjectAssertion + 'static) -> &mut Self { - self.new.assertions.push(CcObjectAssertion::new(assertion)); + assert!( + self.new.assertion.is_none(), + "one OopObject can only have one assertion" + ); + self.new.assertion = Some(CcObjectAssertion::new(assertion)); self } pub fn field(&mut self, name: impl Into) -> ObjMemberBuilder> { -- gitstuff