on 2be0c47 help sign in
dodrio/src/render_context.rs
63 lines 1.7 kB history raw
1use crate::cached_set::CachedSet;
2use bumpalo::Bump;
3use std::fmt;
5/// Common context available to all `Render` implementations.
6///
7/// Notably, the `RenderContext` gives access to the bump arena that the virtual
8/// DOM should be allocated within. This is available via the `bump` field.
9pub struct RenderContext<'a> {
10 /// The underlying bump arena that virtual DOMs are rendered into.
11 ///
12 /// ## Example
13 ///
14 /// ```
15 /// use dodrio::RenderContext;
16 ///
17 /// // Given a rendering context, allocate an i32 inside its bump arena.
18 /// fn foo<'a>(cx: &mut RenderContext<'a>) -> &'a mut i32 {
19 /// cx.bump.alloc(42)
20 /// }
21 /// ```
22 pub bump: &'a Bump,
24 pub(crate) cached_set: &'a crate::RefCell<CachedSet>,
26 // Prevent exhaustive matching on the rendering context, so we can always
27 // add more members in a semver-compatible way.
28 _non_exhaustive: (),
31impl fmt::Debug for RenderContext<'_> {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 f.debug_struct("RenderContext")
34 .field("bump", &self.bump)
35 .finish()
36 }
39impl<'a> RenderContext<'a> {
40 pub_unstable_internal! {
41 pub(crate) fn new(bump: &'a Bump, cached_set: &'a crate::RefCell<CachedSet>) -> Self {
42 RenderContext {
43 bump,
44 cached_set,
45 _non_exhaustive: (),
46 }
47 }
48 }
51impl<'a, 'b> From<&'b RenderContext<'a>> for &'a Bump {
52 #[inline]
53 fn from(cx: &'b RenderContext<'a>) -> &'a Bump {
54 cx.bump
55 }
58impl<'a, 'b, 'c> From<&'c &'b mut RenderContext<'a>> for &'a Bump {
59 #[inline]
60 fn from(cx: &'c &'b mut RenderContext<'a>) -> &'a Bump {
61 cx.bump
62 }