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
1#[macro_export]1use std::{cell::RefCell, rc::Rc};
2macro_rules! future_wrapper {2
3 ($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}
19
20impl<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}
3025
31#[macro_export]26#[macro_export]
32macro_rules! rc_fn_helper {27macro_rules! rc_fn_helper {
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}