From 1f53e1f057f6b25021d4605f87be14bd07882b60 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sun, 21 Feb 2021 14:29:44 +0000 Subject: [PATCH] refactor: remove future_wrapper --- --- 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, Option) -> Result ); - -future_wrapper!(Context, FutureContext); struct ContextInternals { dollar: Option, @@ -33,8 +31,8 @@ #[derive(Debug, Clone)] pub struct Context(Rc); impl Context { - pub fn new_future() -> FutureContext { - FutureContext(Rc::new(RefCell::new(None))) + pub fn new_future() -> FutureWrapper { + FutureWrapper::new() } pub fn dollar(&self) -> &Option { @@ -66,7 +64,7 @@ .cloned() .ok_or(VariableIsNotDefined(name))?) } - pub fn into_future(self, ctx: FutureContext) -> Self { + pub fn into_future(self, ctx: FutureWrapper) -> Self { { ctx.0.borrow_mut().replace(self); } --- 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>>); - 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(pub Rc>>); +impl FutureWrapper { + 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 FutureWrapper { + pub fn unwrap(self) -> T { + self.0.borrow().as_ref().cloned().unwrap() + } +} + +impl Default for FutureWrapper { + fn default() -> Self { + Self::new() + } } #[macro_export] --- 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, FutureNewBindings); -future_wrapper!(ObjValue, FutureObjValue); pub fn evaluate_comp( context: Context, @@ -218,8 +215,8 @@ } pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result { - 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, super_obj: Option| { 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 { 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, super_obj: Option| { 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 } }) } -- gitstuff