git.delta.rocks / jrsonnet / refs/commits / 2cd889c5aabc

difftreelog

feat object extend_with_field method

Yaroslav Bolyukin2021-02-20parent: #2051500.patch.diff
in: master

2 files changed

modifiedbindings/jsonnet/src/val_modify.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -2,11 +2,9 @@
 //! Only tested with variables, which haven't altered by code before appearing here
 //! In jrsonnet every value is immutable, and this code is probally broken
 
-use jrsonnet_evaluator::{
-	ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
-};
+use jrsonnet_evaluator::{ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};
 use jrsonnet_parser::Visibility;
-use std::{collections::HashMap, ffi::CStr, os::raw::c_char, rc::Rc};
+use std::{ffi::CStr, os::raw::c_char, rc::Rc};
 
 /// # Safety
 ///
@@ -42,8 +40,7 @@
 ) {
 	match obj {
 		Val::Obj(old) => {
-			let mut new = HashMap::new();
-			new.insert(
+			let new_obj = old.clone().extend_with_field(
 				CStr::from_ptr(name).to_str().unwrap().into(),
 				ObjMember {
 					add: false,
@@ -52,7 +49,7 @@
 					location: None,
 				},
 			);
-			let new_obj = ObjValue::new(Some(old.clone()), Rc::new(new));
+
 			*obj = Val::Obj(new_obj);
 		}
 		_ => panic!("should receive object"),
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
3use jrsonnet_parser::{ExprLocation, Visibility};3use jrsonnet_parser::{ExprLocation, Visibility};
4use rustc_hash::FxHashMap;4use rustc_hash::FxHashMap;
5use std::hash::{Hash, Hasher};5use std::hash::{Hash, Hasher};
6use std::{cell::RefCell, fmt::Debug, rc::Rc};6use std::{cell::RefCell, fmt::Debug, hash::BuildHasherDefault, rc::Rc};
77
8#[derive(Debug)]8#[derive(Debug)]
9pub struct ObjMember {9pub struct ObjMember {
170 self.get_raw(key, self.0.this_obj.as_ref())170 self.get_raw(key, self.0.this_obj.as_ref())
171 }171 }
172
173 pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {
174 let mut new = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
175 new.insert(key, value);
176 ObjValue::new(Some(self), Rc::new(new))
177 }
178
172 pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {179 pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
173 let real_this = real_this.unwrap_or(self);180 let real_this = real_this.unwrap_or(self);