difftreelog
refactor remove future_wrapper
in: master
3 files changed
crates/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);
}
crates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth1#[macro_export]1use std::{cell::RefCell, rc::Rc};2macro_rules! future_wrapper {23 ($orig: ty, $wrapper: ident) => {3#[derive(Clone)]4 #[derive(Debug, Clone)]5 pub struct $wrapper(pub std::rc::Rc<std::cell::RefCell<Option<$orig>>>);4pub struct FutureWrapper<V>(pub Rc<RefCell<Option<V>>>);6 impl $wrapper {5impl<T> FutureWrapper<T> {7 pub fn unwrap(self) -> $orig {8 self.0.borrow().as_ref().map(|e| e.clone()).unwrap()9 }10 pub fn new() -> Self {6 pub fn new() -> Self {11 $wrapper(std::rc::Rc::new(std::cell::RefCell::new(None)))7 Self(Rc::new(RefCell::new(None)))12 }8 }13 pub fn fill(self, val: $orig) -> $orig {9 pub fn fill(self, value: T) {14 if self.0.borrow().is_some() {10 assert!(self.0.borrow().is_none(), "wrapper is filled already");15 panic!("wrapper is filled already");16 }17 {18 self.0.borrow_mut().replace(val);11 self.0.borrow_mut().replace(value);19 }20 self.unwrap()21 }12 }22 }13}23 impl Default for $wrapper {14impl<T: Clone> FutureWrapper<T> {15 pub fn unwrap(self) -> T {16 self.0.borrow().as_ref().cloned().unwrap()17 }18}1920impl<T> Default for FutureWrapper<T> {24 fn default() -> Self {21 fn default() -> Self {25 Self::new()22 Self::new()26 }23 }27 }24}28 };29}302531#[macro_export]26#[macro_export]32macro_rules! rc_fn_helper {27macro_rules! rc_fn_helper {crates/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
}
})
}