on master help sign in
Move `Render` to a private submodule
commitdaa23fe460b1306d6c4d2c31d25916af3b3924a7 authorNick Fitzgerald <fitzgen@gmail.com> dateSun 10 Feb 2019, 15:55 (8 years ago) parent307c8d1
2 files changed+109−107
src/lib.rs+2−107 src/render.rs+107
src/lib.rs+2−107
@@ -58,11 +58,6 @@
5858// Re-export the `bumpalo` crate.
5959pub use bumpalo;
6060
61−use bumpalo::Bump;
62−use std::any::Any;
63−use std::rc::Rc;
64−use wasm_bindgen::UnwrapThrowExt;
65−
6661// Only `pub` so that the wasm-bindgen bindings work.
6762#[doc(hidden)]
6863pub mod change_list;
@@ -70,111 +65,11 @@ pub mod change_list;
7065mod cached;
7166mod events;
7267mod node;
68+mod render;
7369mod vdom;
7470
7571// Re-export items at the top level.
7672pub use self::cached::Cached;
7773pub use self::node::{on, Attribute, ElementNode, Listener, ListenerCallback, Node, TextNode};
74+pub use self::render::{Render, RootRender};
7875pub use self::vdom::{Vdom, VdomWeak};
79−
80−pub trait Render {
81− fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
82− where
83− 'a: 'bump;
84−}
85−
86−impl<'r, R> Render for &'r R
87−where
88− R: Render,
89−{
90− fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
91− where
92− 'a: 'bump,
93− {
94− (**self).render(bump)
95− }
96−}
97−
98−impl<R> Render for Rc<R>
99−where
100− R: Render,
101−{
102− fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
103− where
104− 'a: 'bump,
105− {
106− (**self).render(bump)
107− }
108−}
109−
110−/// A `RootRender` is a render component that can be the root rendering component
111−/// mounted to a virtual DOM.
112−///
113−/// In addition to rendering, it must also be `'static` so that it can be owned
114−/// by the virtual DOM and `Any` so that it can be downcast to its concrete type
115−/// by event listener callbacks.
116−///
117−/// You do not need to implement this trait by hand: there is a blanket
118−/// implementation for all `Render` types that fulfill the `RootRender`
119−/// requirements.
120−pub trait RootRender: Any + Render {
121− /// Get this `&RootRender` trait object as an `&Any` trait object reference.
122− fn as_any(&self) -> &Any;
123−
124− /// Get this `&mut RootRender` trait object as an `&mut Any` trait object
125− /// reference.
126− fn as_any_mut(&mut self) -> &mut Any;
127−}
128−
129−impl<T: Any + Render> RootRender for T {
130− fn as_any(&self) -> &Any {
131− self
132− }
133−
134− fn as_any_mut(&mut self) -> &mut Any {
135− self
136− }
137−}
138−
139−impl dyn RootRender {
140− /// Downcast this shared `&dyn RootRender` trait object reference to its
141− /// underlying concrete type.
142− ///
143− /// # Panics
144− ///
145− /// Panics if this virtual DOM's root rendering component is not an `R`
146− /// instance.
147− pub fn unwrap_ref<R: RootRender>(&self) -> &R {
148− self.as_any()
149− .downcast_ref::<R>()
150− .expect_throw("bad `RootRender::unwrap_ref` call")
151− }
152−
153− /// Downcast this exclusive `&mut dyn RootRender` trait object reference to
154− /// its underlying concrete type.
155− ///
156− /// # Panics
157− ///
158− /// Panics if this virtual DOM's root rendering component is not an `R`
159− /// instance.
160− pub fn unwrap_mut<R: RootRender>(&mut self) -> &mut R {
161− self.as_any_mut()
162− .downcast_mut::<R>()
163− .expect_throw("bad `RootRender::unwrap_ref` call")
164− }
165−}
166−
167−#[cfg(test)]
168−mod tests {
169− #[test]
170− fn render_is_object_safe() {
171− #[allow(dead_code)]
172− fn takes_dyn_render(_: &dyn super::Render) {}
173− }
174−
175− #[test]
176− fn root_render_is_object_safe() {
177− #[allow(dead_code)]
178− fn takes_dyn_render(_: &dyn super::RootRender) {}
179− }
180−}
src/render.rsnew file+107
@@ -0,0 +1,107 @@
1+use crate::Node;
2+use bumpalo::Bump;
3+use std::any::Any;
4+use std::rc::Rc;
5+use wasm_bindgen::UnwrapThrowExt;
6+
7+pub trait Render {
8+ fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
9+ where
10+ 'a: 'bump;
11+}
12+
13+impl<'r, R> Render for &'r R
14+where
15+ R: Render,
16+{
17+ fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
18+ where
19+ 'a: 'bump,
20+ {
21+ (**self).render(bump)
22+ }
23+}
24+
25+impl<R> Render for Rc<R>
26+where
27+ R: Render,
28+{
29+ fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
30+ where
31+ 'a: 'bump,
32+ {
33+ (**self).render(bump)
34+ }
35+}
36+
37+/// A `RootRender` is a render component that can be the root rendering component
38+/// mounted to a virtual DOM.
39+///
40+/// In addition to rendering, it must also be `'static` so that it can be owned
41+/// by the virtual DOM and `Any` so that it can be downcast to its concrete type
42+/// by event listener callbacks.
43+///
44+/// You do not need to implement this trait by hand: there is a blanket
45+/// implementation for all `Render` types that fulfill the `RootRender`
46+/// requirements.
47+pub trait RootRender: Any + Render {
48+ /// Get this `&RootRender` trait object as an `&Any` trait object reference.
49+ fn as_any(&self) -> &Any;
50+
51+ /// Get this `&mut RootRender` trait object as an `&mut Any` trait object
52+ /// reference.
53+ fn as_any_mut(&mut self) -> &mut Any;
54+}
55+
56+impl<T: Any + Render> RootRender for T {
57+ fn as_any(&self) -> &Any {
58+ self
59+ }
60+
61+ fn as_any_mut(&mut self) -> &mut Any {
62+ self
63+ }
64+}
65+
66+impl dyn RootRender {
67+ /// Downcast this shared `&dyn RootRender` trait object reference to its
68+ /// underlying concrete type.
69+ ///
70+ /// # Panics
71+ ///
72+ /// Panics if this virtual DOM's root rendering component is not an `R`
73+ /// instance.
74+ pub fn unwrap_ref<R: RootRender>(&self) -> &R {
75+ self.as_any()
76+ .downcast_ref::<R>()
77+ .expect_throw("bad `RootRender::unwrap_ref` call")
78+ }
79+
80+ /// Downcast this exclusive `&mut dyn RootRender` trait object reference to
81+ /// its underlying concrete type.
82+ ///
83+ /// # Panics
84+ ///
85+ /// Panics if this virtual DOM's root rendering component is not an `R`
86+ /// instance.
87+ pub fn unwrap_mut<R: RootRender>(&mut self) -> &mut R {
88+ self.as_any_mut()
89+ .downcast_mut::<R>()
90+ .expect_throw("bad `RootRender::unwrap_ref` call")
91+ }
92+}
93+
94+#[cfg(test)]
95+mod tests {
96+ #[test]
97+ fn render_is_object_safe() {
98+ #[allow(dead_code)]
99+ fn takes_dyn_render(_: &dyn super::Render) {}
100+ }
101+
102+ #[test]
103+ fn root_render_is_object_safe() {
104+ #[allow(dead_code)]
105+ fn takes_dyn_render(_: &dyn super::RootRender) {}
106+ }
107+}