1use crate::{
2 cached::{Cached, TemplateId},
3 cached_set::{CacheId, CachedSet},
4 Node, Render,
5};
6use bumpalo::Bump;
7use fxhash::FxHashMap;
8use std::fmt;
9
10pub struct RenderContext<'a> {
15 pub bump: &'a Bump,
28
29 pub(crate) cached_set: &'a crate::RefCell<CachedSet>,
30
31 pub(crate) templates: &'a mut FxHashMap<TemplateId, Option<CacheId>>,
32
33 _non_exhaustive: (),
36}
37
38impl fmt::Debug for RenderContext<'_> {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 f.debug_struct("RenderContext")
41 .field("bump", &self.bump)
42 .finish()
43 }
44}
45
46impl<'a> RenderContext<'a> {
47 pub_unstable_internal! {
48 pub(crate) fn new(
49 bump: &'a Bump,
50 cached_set: &'a crate::RefCell<CachedSet>,
51 templates: &'a mut FxHashMap<TemplateId, Option<CacheId>>
52 ) -> Self {
53 RenderContext {
54 bump,
55 cached_set,
56 templates,
57 _non_exhaustive: (),
58 }
59 }
60 }
61
62 pub(crate) fn cache<F>(&mut self, pinned: bool, template: Option<CacheId>, f: F) -> CacheId
63 where
64 F: for<'b> FnOnce(&mut RenderContext<'b>) -> Node<'b>,
65 {
66 CachedSet::insert(self, pinned, template, f)
67 }
68
69 pub(crate) fn template<R>(&mut self) -> Option<CacheId>
71 where
72 R: 'static + Default + for<'b> Render<'b>,
73 {
74 let template_id = Cached::<R>::template_id();
75 if let Some(cache_id) = self.templates.get(&template_id).cloned() {
76 return cache_id;
77 }
78
79 self.templates.insert(template_id, None);
83
84 let cache_id = self.cache(true, None, |nested_cx| R::default().render(nested_cx));
87 self.templates.insert(template_id, Some(cache_id));
88 Some(cache_id)
89 }
90}
91
92impl<'a, 'b> From<&'b RenderContext<'a>> for &'a Bump {
93 #[inline]
94 fn from(cx: &'b RenderContext<'a>) -> &'a Bump {
95 cx.bump
96 }
97}
98
99impl<'a, 'b, 'c> From<&'c &'b mut RenderContext<'a>> for &'a Bump {
100 #[inline]
101 fn from(cx: &'c &'b mut RenderContext<'a>) -> &'a Bump {
102 cx.bump
103 }
104}