forked from fitzgen/dodrio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.rs
168 lines (151 loc) · 4.41 KB
/
render.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::{Node, RenderContext};
use std::any::Any;
use std::rc::Rc;
use wasm_bindgen::UnwrapThrowExt;
/// A trait for any component that can be rendered to HTML.
///
/// Takes a shared reference to `self` and generates the virtual DOM that
/// represents its rendered HTML.
///
/// ## `Bump` Allocation
///
/// `Render` implementations can use the `Bump` inside the provided
/// `RenderContext` for very fast allocation for anything that needs to be
/// temporarily allocated during rendering.
///
/// ## Example
///
/// ```no_run
/// use dodrio::{Node, Render, RenderContext};
///
/// pub struct MyComponent;
///
/// impl<'a> Render<'a> for MyComponent {
/// fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
/// use dodrio::builder::*;
///
/// p(&cx)
/// .children([
/// text("This is "),
/// strong(&cx).children([text("my component")]).finish(),
/// text(" rendered!"),
/// ])
/// .finish()
/// }
/// }
/// ```
pub trait Render<'a> {
/// Render `self` as a virtual DOM. Use the given context's `Bump` for
/// temporary allocations.
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a>;
}
impl<'a, 'r, R> Render<'a> for &'r R
where
R: Render<'a>,
{
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
(**self).render(cx)
}
}
impl<'a, R> Render<'a> for Rc<R>
where
R: Render<'a>,
{
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
(**self).render(cx)
}
}
/// A `RootRender` is a render component that can be the root rendering component
/// mounted to a virtual DOM.
///
/// In addition to rendering, it must also be `'static` so that it can be owned
/// by the virtual DOM and `Any` so that it can be downcast to its concrete type
/// by event listener callbacks.
///
/// You do not need to implement this trait by hand: there is a blanket
/// implementation for all `Render` types that fulfill the `RootRender`
/// requirements.
pub trait RootRender: Any + for<'a> Render<'a> {
/// Get this `&RootRender` trait object as an `&Any` trait object reference.
fn as_any(&self) -> &dyn Any;
/// Get this `&mut RootRender` trait object as an `&mut Any` trait object
/// reference.
fn as_any_mut(&mut self) -> &mut dyn Any;
}
impl<T> RootRender for T
where
T: Any + for<'a> Render<'a>,
{
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl dyn RootRender {
/// Downcast this shared `&dyn RootRender` trait object reference to its
/// underlying concrete type.
///
/// # Panics
///
/// Panics if this virtual DOM's root rendering component is not an `R`
/// instance.
pub fn unwrap_ref<R>(&self) -> &R
where
R: RootRender,
{
self.as_any()
.downcast_ref::<R>()
.expect_throw("bad `RootRender::unwrap_ref` call")
}
/// Downcast this exclusive `&mut dyn RootRender` trait object reference to
/// its underlying concrete type.
///
/// # Panics
///
/// Panics if this virtual DOM's root rendering component is not an `R`
/// instance.
pub fn unwrap_mut<R>(&mut self) -> &mut R
where
R: RootRender,
{
self.as_any_mut()
.downcast_mut::<R>()
.expect_throw("bad `RootRender::unwrap_ref` call")
}
}
#[cfg(test)]
mod tests {
#[test]
fn render_is_object_safe() {
#[allow(dead_code)]
fn takes_dyn_render(_: &dyn super::Render) {}
}
#[test]
fn root_render_is_object_safe() {
#[allow(dead_code)]
fn takes_dyn_render(_: &dyn super::RootRender) {}
}
#[test]
fn render_bump_scoped_child() {
use crate::{builder::*, bumpalo::collections::String, Node, Render, RenderContext};
struct Child<'a> {
name: &'a str,
}
impl<'a> Render<'a> for Child<'a> {
fn render(&self, _cx: &mut RenderContext<'a>) -> Node<'a> {
text(self.name)
}
}
struct Parent;
impl<'a> Render<'a> for Parent {
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
let child_name = String::from_str_in("child", cx.bump).into_bump_str();
div(&cx)
.children([Child { name: child_name }.render(cx)])
.finish()
}
}
}
}