git.delta.rocks / jrsonnet / refs/commits / 1f53e1f057f6

difftreelog

refactor remove future_wrapper

Yaroslav Bolyukin2021-02-21parent: #be90068.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -1,19 +1,17 @@
 use crate::{
-	error::Error::*, future_wrapper, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val,
+	error::Error::*, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val, FutureWrapper,
 	LazyBinding, LazyVal, ObjValue, Result, Val,
 };
 use jrsonnet_interner::IStr;
 use rustc_hash::FxHashMap;
 use std::hash::BuildHasherDefault;
-use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
+use std::{collections::HashMap, fmt::Debug, rc::Rc};
 
 rc_fn_helper!(
 	ContextCreator,
 	context_creator,
 	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Context>
 );
-
-future_wrapper!(Context, FutureContext);
 
 struct ContextInternals {
 	dollar: Option<ObjValue>,
@@ -33,8 +31,8 @@
 #[derive(Debug, Clone)]
 pub struct Context(Rc<ContextInternals>);
 impl Context {
-	pub fn new_future() -> FutureContext {
-		FutureContext(Rc::new(RefCell::new(None)))
+	pub fn new_future() -> FutureWrapper<Context> {
+		FutureWrapper::new()
 	}
 
 	pub fn dollar(&self) -> &Option<ObjValue> {
@@ -66,7 +64,7 @@
 			.cloned()
 			.ok_or(VariableIsNotDefined(name))?)
 	}
-	pub fn into_future(self, ctx: FutureContext) -> Self {
+	pub fn into_future(self, ctx: FutureWrapper<Context>) -> Self {
 		{
 			ctx.0.borrow_mut().replace(self);
 		}
modifiedcrates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -1,31 +1,26 @@
-#[macro_export]
-macro_rules! future_wrapper {
-	($orig: ty, $wrapper: ident) => {
-		#[derive(Debug, Clone)]
-		pub struct $wrapper(pub std::rc::Rc<std::cell::RefCell<Option<$orig>>>);
-		impl $wrapper {
-			pub fn unwrap(self) -> $orig {
-				self.0.borrow().as_ref().map(|e| e.clone()).unwrap()
-			}
-			pub fn new() -> Self {
-				$wrapper(std::rc::Rc::new(std::cell::RefCell::new(None)))
-			}
-			pub fn fill(self, val: $orig) -> $orig {
-				if self.0.borrow().is_some() {
-					panic!("wrapper is filled already");
-				}
-				{
-					self.0.borrow_mut().replace(val);
-				}
-				self.unwrap()
-			}
-		}
-		impl Default for $wrapper {
-			fn default() -> Self {
-				Self::new()
-			}
-		}
-	};
+use std::{cell::RefCell, rc::Rc};
+
+#[derive(Clone)]
+pub struct FutureWrapper<V>(pub Rc<RefCell<Option<V>>>);
+impl<T> FutureWrapper<T> {
+	pub fn new() -> Self {
+		Self(Rc::new(RefCell::new(None)))
+	}
+	pub fn fill(self, value: T) {
+		assert!(self.0.borrow().is_none(), "wrapper is filled already");
+		self.0.borrow_mut().replace(value);
+	}
+}
+impl<T: Clone> FutureWrapper<T> {
+	pub fn unwrap(self) -> T {
+		self.0.borrow().as_ref().cloned().unwrap()
+	}
+}
+
+impl<T> Default for FutureWrapper<T> {
+	fn default() -> Self {
+		Self::new()
+	}
 }
 
 #[macro_export]
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
1use crate::{1use crate::{
2 context_creator, error::Error::*, future_wrapper, lazy_val, push, throw, with_state, Context,2 context_creator, error::Error::*, lazy_val, push, throw, with_state, Context, ContextCreator,
3 ContextCreator, FuncDesc, FuncVal, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,3 FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
4};4};
5use closure::closure;5use closure::closure;
6use jrsonnet_interner::IStr;6use jrsonnet_interner::IStr;
183 })183 })
184}184}
185
186future_wrapper!(HashMap<IStr, LazyBinding>, FutureNewBindings);
187future_wrapper!(ObjValue, FutureObjValue);
188185
189pub fn evaluate_comp<T>(186pub fn evaluate_comp<T>(
190 context: Context,187 context: Context,
218}215}
219216
220pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {217pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {
221 let new_bindings = FutureNewBindings::new();218 let new_bindings = FutureWrapper::new();
222 let future_this = FutureObjValue::new();219 let future_this = FutureWrapper::new();
223 let context_creator = context_creator!(220 let context_creator = context_creator!(
224 closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {221 closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
225 context.clone().extend_unbound(222 context.clone().extend_unbound(
312 Member::AssertStmt(_) => {}309 Member::AssertStmt(_) => {}
313 }310 }
314 }311 }
315 Ok(future_this.fill(ObjValue::new(None, Rc::new(new_members))))312 let this = ObjValue::new(None, Rc::new(new_members));
313 future_this.fill(this.clone());
314 Ok(this)
316}315}
317316
318pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {317pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {
319 Ok(match object {318 Ok(match object {
320 ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,319 ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,
321 ObjBody::ObjComp(obj) => {320 ObjBody::ObjComp(obj) => {
322 let future_this = FutureObjValue::new();321 let future_this = FutureWrapper::new();
323 let mut new_members = FxHashMap::default();322 let mut new_members = FxHashMap::default();
324 for (k, v) in evaluate_comp(323 for (k, v) in evaluate_comp(
325 context.clone(),324 context.clone(),
326 &|ctx| {325 &|ctx| {
327 let new_bindings = FutureNewBindings::new();326 let new_bindings = FutureWrapper::new();
328 let context_creator = context_creator!(327 let context_creator = context_creator!(
329 closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {328 closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
330 context.clone().extend_unbound(329 context.clone().extend_unbound(
344 {343 {
345 bindings.insert(n, b);344 bindings.insert(n, b);
346 }345 }
347 let bindings = new_bindings.fill(bindings);346 new_bindings.fill(bindings.clone());
348 let ctx = ctx.extend_unbound(bindings, None, None, None)?;347 let ctx = ctx.extend_unbound(bindings, None, None, None)?;
349 let key = evaluate(ctx.clone(), &obj.key)?;348 let key = evaluate(ctx.clone(), &obj.key)?;
350 let value = LazyBinding::Bindable(Rc::new(349 let value = LazyBinding::Bindable(Rc::new(
376 }375 }
377 }376 }
378377
379 future_this.fill(ObjValue::new(None, Rc::new(new_members)))378 let this = ObjValue::new(None, Rc::new(new_members));
379 future_this.fill(this.clone());
380 this
380 }381 }
381 })382 })
382}383}