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
1use crate::{1use crate::{
2 error::Error::*, future_wrapper, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val,2 error::Error::*, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val, FutureWrapper,
3 LazyBinding, LazyVal, ObjValue, Result, Val,3 LazyBinding, LazyVal, ObjValue, Result, Val,
4};4};
5use jrsonnet_interner::IStr;5use jrsonnet_interner::IStr;
6use rustc_hash::FxHashMap;6use rustc_hash::FxHashMap;
7use std::hash::BuildHasherDefault;7use std::hash::BuildHasherDefault;
8use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};8use std::{collections::HashMap, fmt::Debug, rc::Rc};
99
10rc_fn_helper!(10rc_fn_helper!(
11 ContextCreator,11 ContextCreator,
12 context_creator,12 context_creator,
13 dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Context>13 dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Context>
14);14);
15
16future_wrapper!(Context, FutureContext);
1715
18struct ContextInternals {16struct ContextInternals {
19 dollar: Option<ObjValue>,17 dollar: Option<ObjValue>,
33#[derive(Debug, Clone)]31#[derive(Debug, Clone)]
34pub struct Context(Rc<ContextInternals>);32pub struct Context(Rc<ContextInternals>);
35impl Context {33impl Context {
36 pub fn new_future() -> FutureContext {34 pub fn new_future() -> FutureWrapper<Context> {
37 FutureContext(Rc::new(RefCell::new(None)))35 FutureWrapper::new()
38 }36 }
3937
40 pub fn dollar(&self) -> &Option<ObjValue> {38 pub fn dollar(&self) -> &Option<ObjValue> {
66 .cloned()64 .cloned()
67 .ok_or(VariableIsNotDefined(name))?)65 .ok_or(VariableIsNotDefined(name))?)
68 }66 }
69 pub fn into_future(self, ctx: FutureContext) -> Self {67 pub fn into_future(self, ctx: FutureWrapper<Context>) -> Self {
70 {68 {
71 ctx.0.borrow_mut().replace(self);69 ctx.0.borrow_mut().replace(self);
72 }70 }
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
--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -1,6 +1,6 @@
 use crate::{
-	context_creator, error::Error::*, future_wrapper, lazy_val, push, throw, with_state, Context,
-	ContextCreator, FuncDesc, FuncVal, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
+	context_creator, error::Error::*, lazy_val, push, throw, with_state, Context, ContextCreator,
+	FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
 };
 use closure::closure;
 use jrsonnet_interner::IStr;
@@ -182,9 +182,6 @@
 		)),
 	})
 }
-
-future_wrapper!(HashMap<IStr, LazyBinding>, FutureNewBindings);
-future_wrapper!(ObjValue, FutureObjValue);
 
 pub fn evaluate_comp<T>(
 	context: Context,
@@ -218,8 +215,8 @@
 }
 
 pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {
-	let new_bindings = FutureNewBindings::new();
-	let future_this = FutureObjValue::new();
+	let new_bindings = FutureWrapper::new();
+	let future_this = FutureWrapper::new();
 	let context_creator = context_creator!(
 		closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
 			context.clone().extend_unbound(
@@ -312,19 +309,21 @@
 			Member::AssertStmt(_) => {}
 		}
 	}
-	Ok(future_this.fill(ObjValue::new(None, Rc::new(new_members))))
+	let this = ObjValue::new(None, Rc::new(new_members));
+	future_this.fill(this.clone());
+	Ok(this)
 }
 
 pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {
 	Ok(match object {
 		ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,
 		ObjBody::ObjComp(obj) => {
-			let future_this = FutureObjValue::new();
+			let future_this = FutureWrapper::new();
 			let mut new_members = FxHashMap::default();
 			for (k, v) in evaluate_comp(
 				context.clone(),
 				&|ctx| {
-					let new_bindings = FutureNewBindings::new();
+					let new_bindings = FutureWrapper::new();
 					let context_creator = context_creator!(
 						closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
 							context.clone().extend_unbound(
@@ -344,7 +343,7 @@
 					{
 						bindings.insert(n, b);
 					}
-					let bindings = new_bindings.fill(bindings);
+					new_bindings.fill(bindings.clone());
 					let ctx = ctx.extend_unbound(bindings, None, None, None)?;
 					let key = evaluate(ctx.clone(), &obj.key)?;
 					let value = LazyBinding::Bindable(Rc::new(
@@ -376,7 +375,9 @@
 				}
 			}
 
-			future_this.fill(ObjValue::new(None, Rc::new(new_members)))
+			let this = ObjValue::new(None, Rc::new(new_members));
+			future_this.fill(this.clone());
+			this
 		}
 	})
 }