git.delta.rocks / jrsonnet / refs/commits / 6359a4da99ca

difftreelog

feat(evaluator) weak context

Лач2020-06-28parent: #f75ed98.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
6 cell::RefCell,
7 collections::HashMap,
8 fmt::Debug,
9 rc::{Rc, Weak},
10};
611
7rc_fn_helper!(12rc_fn_helper!(
18 super_obj: Option<ObjValue>,23 super_obj: Option<ObjValue>,
19 bindings: LayeredHashMap<Rc<str>, LazyVal>,24 bindings: LayeredHashMap<Rc<str>, LazyVal>,
20}25}
21pub struct Context(Rc<ContextInternals>);
22impl Debug for Context {26impl Debug for ContextInternals {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 f.debug_struct("Context")28 f.debug_struct("Context")
25 .field("this", &self.0.this.as_ref().map(|e| Rc::as_ptr(&e.0)))29 .field("this", &self.this.as_ref().map(|e| Rc::as_ptr(&e.0)))
26 .field("bindings", &self.0.bindings)30 .field("bindings", &self.bindings)
27 .finish()31 .finish()
28 }32 }
29}33}
34
35#[derive(Debug, Clone)]
36pub struct Context(Rc<ContextInternals>);
30impl Context {37impl Context {
31 pub fn new_future() -> FutureContext {38 pub fn new_future() -> FutureContext {
32 FutureContext(Rc::new(RefCell::new(None)))39 FutureContext(Rc::new(RefCell::new(None)))
110 }117 }
111 self.extend(new, new_dollar, this, super_obj)118 self.extend(new, new_dollar, this, super_obj)
112 }119 }
120 pub fn into_weak(self) -> WeakContext {
121 WeakContext(Rc::downgrade(&self.0))
122 }
113}123}
114124
115impl Default for Context {125impl Default for Context {
124 }134 }
125}135}
126136
137#[derive(Debug, Clone)]
138pub struct WeakContext(Weak<ContextInternals>);
127impl Clone for Context {139impl WeakContext {
128 fn clone(&self) -> Self {140 pub fn upgrade(&self) -> Context {
129 Context(self.0.clone())141 Context(self.0.upgrade().expect("context is removed"))
130 }142 }
131}143}
144impl PartialEq for WeakContext {
145 fn eq(&self, other: &Self) -> bool {
146 self.0.ptr_eq(&other.0)
147 }
148}
132149