Skip to content

Commit 64b709c

Browse files
committed
eliminate RenderContextInner
1 parent 080b5e0 commit 64b709c

File tree

1 file changed

+38
-70
lines changed

1 file changed

+38
-70
lines changed

src/render.rs

Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,15 @@ const BLOCK_HELPER_MISSING: &str = "blockHelperMissing";
3333
/// This context stores information of a render and a writer where generated
3434
/// content is written to.
3535
///
36-
#[derive(Clone, Debug)]
37-
pub struct RenderContext<'reg, 'rc> {
38-
inner: Rc<RenderContextInner<'reg, 'rc>>,
39-
36+
#[derive(Clone)]
37+
pub struct RenderContext<'reg: 'rc, 'rc> {
4038
dev_mode_templates: Option<&'rc BTreeMap<String, Cow<'rc, Template>>>,
4139

4240
blocks: VecDeque<BlockContext<'rc>>,
41+
4342
// copy-on-write context
4443
modified_context: Option<Rc<Context>>,
45-
}
4644

47-
#[derive(Clone)]
48-
pub struct RenderContextInner<'reg: 'rc, 'rc> {
4945
partials: BTreeMap<String, &'rc Template>,
5046
partial_block_stack: VecDeque<&'rc Template>,
5147
partial_block_depth: isize,
@@ -72,7 +68,11 @@ pub struct RenderContextInner<'reg: 'rc, 'rc> {
7268
impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
7369
/// Create a render context
7470
pub fn new(root_template: Option<&'reg String>) -> RenderContext<'reg, 'rc> {
75-
let inner = Rc::new(RenderContextInner {
71+
let mut blocks = VecDeque::with_capacity(5);
72+
blocks.push_front(BlockContext::new());
73+
74+
let modified_context = None;
75+
RenderContext {
7676
partials: BTreeMap::new(),
7777
partial_block_stack: VecDeque::new(),
7878
partial_block_depth: 0,
@@ -84,36 +84,12 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
8484
content_produced: false,
8585
indent_before_write: false,
8686
indent_string: None,
87-
});
88-
89-
let mut blocks = VecDeque::with_capacity(5);
90-
blocks.push_front(BlockContext::new());
91-
92-
let modified_context = None;
93-
RenderContext {
94-
inner,
9587
blocks,
9688
modified_context,
9789
dev_mode_templates: None,
9890
}
9991
}
10092

101-
pub(crate) fn new_for_block(&self) -> RenderContext<'reg, 'rc> {
102-
let inner = self.inner.clone();
103-
104-
let mut blocks = VecDeque::with_capacity(2);
105-
blocks.push_front(BlockContext::new());
106-
107-
let modified_context = self.modified_context.clone();
108-
109-
RenderContext {
110-
inner,
111-
blocks,
112-
modified_context,
113-
dev_mode_templates: self.dev_mode_templates,
114-
}
115-
}
116-
11793
/// Push a block context into render context stack. This is typically
11894
/// called when you entering a block scope.
11995
pub fn push_block(&mut self, block: BlockContext<'rc>) {
@@ -141,14 +117,6 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
141117
self.blocks.front_mut()
142118
}
143119

144-
fn inner(&self) -> &RenderContextInner<'reg, 'rc> {
145-
self.inner.borrow()
146-
}
147-
148-
fn inner_mut(&mut self) -> &mut RenderContextInner<'reg, 'rc> {
149-
Rc::make_mut(&mut self.inner)
150-
}
151-
152120
/// Get the modified context data if any
153121
pub fn context(&self) -> Option<Rc<Context>> {
154122
self.modified_context.clone()
@@ -192,45 +160,44 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
192160
pub fn get_partial(&self, name: &str) -> Option<&'rc Template> {
193161
if name == partial::PARTIAL_BLOCK {
194162
return self
195-
.inner()
196163
.partial_block_stack
197-
.get(self.inner().partial_block_depth as usize)
164+
.get(self.partial_block_depth as usize)
198165
.copied();
199166
}
200-
self.inner().partials.get(name).copied()
167+
self.partials.get(name).copied()
201168
}
202169

203170
/// Register a partial for this context
204171
pub fn set_partial(&mut self, name: String, partial: &'rc Template) {
205-
self.inner_mut().partials.insert(name, partial);
172+
self.partials.insert(name, partial);
206173
}
207174

208175
pub(crate) fn push_partial_block(&mut self, partial: &'rc Template) {
209-
self.inner_mut().partial_block_stack.push_front(partial);
176+
self.partial_block_stack.push_front(partial);
210177
}
211178

212179
pub(crate) fn pop_partial_block(&mut self) {
213-
self.inner_mut().partial_block_stack.pop_front();
180+
self.partial_block_stack.pop_front();
214181
}
215182

216183
pub(crate) fn inc_partial_block_depth(&mut self) {
217-
self.inner_mut().partial_block_depth += 1;
184+
self.partial_block_depth += 1;
218185
}
219186

220187
pub(crate) fn dec_partial_block_depth(&mut self) {
221-
let depth = &mut self.inner_mut().partial_block_depth;
188+
let depth = &mut self.partial_block_depth;
222189
if *depth > 0 {
223190
*depth -= 1;
224191
}
225192
}
226193

227194
pub(crate) fn set_indent_string(&mut self, indent: Option<Cow<'rc, str>>) {
228-
self.inner_mut().indent_string = indent;
195+
self.indent_string = indent;
229196
}
230197

231198
#[inline]
232199
pub(crate) fn get_indent_string(&self) -> Option<&Cow<'rc, str>> {
233-
self.inner.indent_string.as_ref()
200+
self.indent_string.as_ref()
234201
}
235202

236203
pub(crate) fn get_dev_mode_template(&self, name: &str) -> Option<&'rc Template> {
@@ -247,7 +214,7 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
247214

248215
/// Remove a registered partial
249216
pub fn remove_partial(&mut self, name: &str) {
250-
self.inner_mut().partials.remove(name);
217+
self.partials.remove(name);
251218
}
252219

253220
fn get_local_var(&self, level: usize, name: &str) -> Option<&Json> {
@@ -258,7 +225,7 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
258225

259226
/// Test if given template name is current template.
260227
pub fn is_current_template(&self, p: &str) -> bool {
261-
self.inner().current_template.is_some_and(|s| s == p)
228+
self.current_template.is_some_and(|s| s == p)
262229
}
263230

264231
/// Register a helper in this render context.
@@ -269,89 +236,90 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
269236
name: &str,
270237
def: Box<dyn HelperDef + Send + Sync + 'rc>,
271238
) {
272-
self.inner_mut()
273-
.local_helpers
274-
.insert(name.to_string(), def.into());
239+
self.local_helpers.insert(name.to_string(), def.into());
275240
}
276241

277242
/// Remove a helper from render context
278243
pub fn unregister_local_helper(&mut self, name: &str) {
279-
self.inner_mut().local_helpers.remove(name);
244+
self.local_helpers.remove(name);
280245
}
281246

282247
/// Attempt to get a helper from current render context.
283248
pub fn get_local_helper(&self, name: &str) -> Option<Rc<dyn HelperDef + Send + Sync + 'rc>> {
284-
self.inner().local_helpers.get(name).cloned()
249+
self.local_helpers.get(name).cloned()
285250
}
286251

287252
#[inline]
288253
fn has_local_helper(&self, name: &str) -> bool {
289-
self.inner.local_helpers.contains_key(name)
254+
self.local_helpers.contains_key(name)
290255
}
291256

292257
/// Returns the current template name.
293258
/// Note that the name can be vary from root template when you are rendering
294259
/// from partials.
295260
pub fn get_current_template_name(&self) -> Option<&'rc String> {
296-
self.inner().current_template
261+
self.current_template
297262
}
298263

299264
/// Set the current template name.
300265
pub fn set_current_template_name(&mut self, name: Option<&'rc String>) {
301-
self.inner_mut().current_template = name;
266+
self.current_template = name;
302267
}
303268

304269
/// Get root template name if any.
305270
/// This is the template name that you call `render` from `Handlebars`.
306271
pub fn get_root_template_name(&self) -> Option<&'reg String> {
307-
self.inner().root_template
272+
self.root_template
308273
}
309274

310275
/// Get the escape toggle
311276
pub fn is_disable_escape(&self) -> bool {
312-
self.inner().disable_escape
277+
self.disable_escape
313278
}
314279

315280
/// Set the escape toggle.
316281
/// When toggle is on, `escape_fn` will be called when rendering.
317282
pub fn set_disable_escape(&mut self, disable: bool) {
318-
self.inner_mut().disable_escape = disable;
283+
self.disable_escape = disable;
319284
}
320285

321286
#[inline]
322287
pub fn set_trailing_newline(&mut self, trailing_newline: bool) {
323-
self.inner_mut().trailing_newline = trailing_newline;
288+
self.trailing_newline = trailing_newline;
324289
}
325290

326291
#[inline]
327292
pub fn get_trailine_newline(&self) -> bool {
328-
self.inner().trailing_newline
293+
self.trailing_newline
329294
}
330295

331296
#[inline]
332297
pub fn set_content_produced(&mut self, content_produced: bool) {
333-
self.inner_mut().content_produced = content_produced;
298+
self.content_produced = content_produced;
334299
}
335300

336301
#[inline]
337302
pub fn get_content_produced(&self) -> bool {
338-
self.inner().content_produced
303+
self.content_produced
339304
}
340305

341306
#[inline]
342307
pub fn set_indent_before_write(&mut self, indent_before_write: bool) {
343-
self.inner_mut().indent_before_write = indent_before_write;
308+
self.indent_before_write = indent_before_write;
344309
}
345310

346311
#[inline]
347312
pub fn get_indent_before_write(&self) -> bool {
348-
self.inner().indent_before_write
313+
self.indent_before_write
349314
}
350315
}
351316

352-
impl<'reg, 'rc> fmt::Debug for RenderContextInner<'reg, 'rc> {
317+
impl<'reg, 'rc> fmt::Debug for RenderContext<'reg, 'rc> {
353318
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
354319
f.debug_struct("RenderContextInner")
320+
.field("dev_mode_templates", &self.dev_mode_templates)
321+
.field("blocks", &self.blocks)
322+
.field("modified_context", &self.modified_context)
355323
.field("partials", &self.partials)
356324
.field("partial_block_stack", &self.partial_block_stack)
357325
.field("partial_block_depth", &self.partial_block_depth)

0 commit comments

Comments
 (0)