on master help sign in
Use `'a` lifetimes instead of `'bump`

It isn't as clear why we were using `'bump` now that we pass a `RenderContext` to `render` rather than a raw `Bump`.

commit2ef092385df91efd3668d192887c979ad8c417ab authorNick Fitzgerald <fitzgen@gmail.com> dateFri 29 Mar 2019, 21:35 (8 years ago) parent52d9f3e
18 files changed+54−78
README.md+4−6 benches/benches.rs+2−2 crates/js-api/src/lib.rs+3−3 examples/counter/src/lib.rs+1−1 examples/game-of-life/src/lib.rs+1−1 examples/hello-world/src/lib.rs+1−1 examples/input-form/src/lib.rs+1−1 examples/js-component/src/lib.rs+1−1 examples/moire/src/lib.rs+13−22 examples/sierpinski-triangle/src/lib.rs+7−17 examples/todomvc/src/todo.rs+1−1 examples/todomvc/src/todos.rs+7−7 src/lib.rs+1−1 src/render.rs+4−4 src/vdom.rs+1−4 tests/web/events.rs+2−2 tests/web/js_api.rs+1−1 tests/web/main.rs+3−3
README.md+4−6
@@ -31,12 +31,10 @@ struct Hello {
3131}
3232
3333impl Render for Hello {
34− fn render<'a, 'bump>(&'a self, cx: &mut RenderContext<'bump>) -> Node<'bump>
35− where
36− 'a: 'bump,
37− {
38− span(cx)
39− .children([text("Hello, "), text(&self.who), text("!")])
34+ fn render<'a>(&self, cx: &mut RenderContext<a>) -> Node<'a> {
35+ let who = bumpalo::format!(in cx.bump, "Hello, {}!", self.who);
36+ div(cx)
37+ .children([text(who.into_bump_str())])
4038 .finish()
4139 }
4240}
benches/benches.rs+2−2
@@ -17,7 +17,7 @@ use std::cell::RefCell;
1717/// The simplest thing we can render: `<div/>`.
1818struct Empty;
1919impl Render for Empty {
20− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
20+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
2121 div(&cx).finish()
2222 }
2323}
@@ -25,7 +25,7 @@ impl Render for Empty {
2525/// Render a list that is `self.0` items long, has attributes and listeners.
2626struct SimpleList(usize);
2727impl Render for SimpleList {
28− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
28+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
2929 let mut children = bumpalo::collections::Vec::with_capacity_in(self.0, cx.bump);
3030 children.extend((0..self.0).map(|_| {
3131 li(&cx)
crates/js-api/src/lib.rs+3−3
@@ -100,7 +100,7 @@ impl GreetingViaJs {
100100/// And finally the `Render` implementation! This adds a `<p>` element and some
101101/// text around whatever the inner JS `Greeting` component renders.
102102impl Render for GreetingViaJs {
103− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
103+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
104104 use dodrio::builder::*;
105105 p(&cx)
106106 .children([
@@ -214,7 +214,7 @@ impl JsRender {
214214}
215215
216216impl Render for JsRender {
217− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
217+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
218218 create(cx, self.render())
219219 }
220220}
@@ -223,7 +223,7 @@ fn has_property(obj: &Object, property: &str) -> bool {
223223 Reflect::has(obj, &property.into()).unwrap_or_default()
224224}
225225
226−fn create<'bump>(cx: &mut RenderContext<'bump>, val: JsValue) -> Node<'bump> {
226+fn create<'a>(cx: &mut RenderContext<'a>, val: JsValue) -> Node<'a> {
227227 if let Some(txt) = val.as_string() {
228228 let text = bumpalo::collections::String::from_str_in(&txt, cx.bump);
229229 return builder::text(text.into_bump_str());
examples/counter/src/lib.rs+1−1
@@ -27,7 +27,7 @@ impl Counter {
2727// The `Render` implementation for `Counter`s displays the current count and has
2828// buttons to increment and decrement the count.
2929impl Render for Counter {
30− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> dodrio::Node<'bump> {
30+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> dodrio::Node<'a> {
3131 use dodrio::builder::*;
3232
3333 // Stringify the count as a bump-allocated string.
examples/game-of-life/src/lib.rs+1−1
@@ -128,7 +128,7 @@ impl Universe {
128128
129129/// The rendering implementation for our Game of Life.
130130impl Render for Universe {
131− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
131+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
132132 use dodrio::builder::*;
133133
134134 let mut rows = bumpalo::collections::Vec::with_capacity_in(self.height as usize, cx.bump);
examples/hello-world/src/lib.rs+1−1
@@ -11,7 +11,7 @@ struct Hello {
1111// The `Render` implementation describes how to render a `Hello` component into
1212// HTML.
1313impl Render for Hello {
14− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
14+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
1515 let msg = bumpalo::format!(in cx.bump, "Hello, {}!", self.who);
1616 let msg = msg.into_bump_str();
1717 p(&cx).children([text(msg)]).finish()
examples/input-form/src/lib.rs+1−1
@@ -24,7 +24,7 @@ impl SayHelloTo {
2424// The `Render` implementation has a text `<input>` and a `<div>` that shows a
2525// greeting to the `<input>`'s value.
2626impl Render for SayHelloTo {
27− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
27+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
2828 use dodrio::builder::*;
2929
3030 div(&cx)
examples/js-component/src/lib.rs+1−1
@@ -32,7 +32,7 @@ impl GreetingViaJs {
3232/// Here's the `Render` implementation! This adds a `<p>` element and some text
3333/// around whatever the inner JS `Greeting` component renders.
3434impl Render for GreetingViaJs {
35− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
35+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
3636 use dodrio::builder::*;
3737 p(&cx)
3838 .children([text("JavaScript says: "), self.js.render(cx)])
examples/moire/src/lib.rs+13−22
@@ -106,7 +106,7 @@ impl Moire {
106106}
107107
108108impl Render for Moire {
109− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
109+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
110110 use dodrio::builder::*;
111111
112112 let elapsed = web_sys::window()
@@ -150,7 +150,7 @@ impl Render for Moire {
150150/// Rendering helper methods.
151151impl Moire {
152152 /// Generate the main Moire element's inline CSS styles.
153− fn moire_style<'bump>(&self, cx: &mut RenderContext<'bump>, elapsed: f64) -> &'bump str {
153+ fn moire_style<'a>(&self, cx: &mut RenderContext<'a>, elapsed: f64) -> &'a str {
154154 let elapsed = elapsed / 3.0;
155155 let color = colors::get_interpolated_color(|(_, bg)| bg, elapsed % 1.0);
156156 let style = bumpalo::format!(
@@ -164,7 +164,7 @@ impl Moire {
164164 }
165165
166166 /// Generate the orbiting circle objects.
167− fn orbiting_objects<'bump>(&self, cx: &mut RenderContext<'bump>, elapsed: f64) -> Node<'bump> {
167+ fn orbiting_objects<'a>(&self, cx: &mut RenderContext<'a>, elapsed: f64) -> Node<'a> {
168168 const D: f64 = 200.0;
169169 let x = elapsed.sin() * D;
170170 let y = elapsed.cos() * D;
@@ -173,24 +173,20 @@ impl Moire {
173173
174174 /// Generate the lemniscate circle objects that interact with the orbiting
175175 /// circle objects to form Moire patterns.
176− fn lemniscate_objects<'bump>(
177− &self,
178− cx: &mut RenderContext<'bump>,
179− elapsed: f64,
180− ) -> Node<'bump> {
176+ fn lemniscate_objects<'a>(&self, cx: &mut RenderContext<'a>, elapsed: f64) -> Node<'a> {
181177 const A: f64 = 150.0;
182178 let x = elapsed.sin() * A;
183179 let y = elapsed.sin() * elapsed.cos() * A;
184180 self.moving_object(cx, elapsed, x, y)
185181 }
186182
187− fn moving_object<'a, 'bump>(
188− &'a self,
189− cx: &mut RenderContext<'bump>,
183+ fn moving_object<'a>(
184+ &self,
185+ cx: &mut RenderContext<'a>,
190186 elapsed: f64,
191187 x: f64,
192188 y: f64,
193− ) -> Node<'bump> {
189+ ) -> Node<'a> {
194190 use dodrio::builder::*;
195191 div(&cx)
196192 .attr("class", "object")
@@ -200,17 +196,12 @@ impl Moire {
200196 }
201197
202198 /// Generate inline CSS styles for a moving object.
203− fn moving_object_style<'bump>(
204− &self,
205− cx: &mut RenderContext<'bump>,
206− x: f64,
207− y: f64,
208− ) -> &'bump str {
199+ fn moving_object_style<'a>(&self, cx: &mut RenderContext<'a>, x: f64, y: f64) -> &'a str {
209200 bumpalo::format!(in cx.bump, "left: {}px; top: {}px;", x, y).into_bump_str()
210201 }
211202
212203 /// Recursively generate `self.count` concentric circles.
213− fn circle<'bump>(&self, cx: &mut RenderContext<'bump>, elapsed: f64, n: u32) -> Node<'bump> {
204+ fn circle<'a>(&self, cx: &mut RenderContext<'a>, elapsed: f64, n: u32) -> Node<'a> {
214205 use dodrio::builder::*;
215206
216207 let r = n * 16;
@@ -231,13 +222,13 @@ impl Moire {
231222 }
232223
233224 /// Generate inline CSS styles for a circle.
234− fn circle_style<'bump>(
225+ fn circle_style<'a>(
235226 &self,
236− cx: &mut RenderContext<'bump>,
227+ cx: &mut RenderContext<'a>,
237228 elapsed: f64,
238229 n: u32,
239230 r: u32,
240− ) -> &'bump str {
231+ ) -> &'a str {
241232 let (border, alpha) = self.circle_color(elapsed, n);
242233 let margin = r / 2 + 3;
243234 let style = bumpalo::format!(
examples/sierpinski-triangle/src/lib.rs+7−17
@@ -17,11 +17,7 @@ impl Container {
1717 }
1818
1919 // Generate the container style to fluctuate triangle width.
20− fn container_transform<'bump>(
21− &self,
22− cx: &mut RenderContext<'bump>,
23− elapsed: f64,
24− ) -> &'bump str {
20+ fn container_transform<'a>(&self, cx: &mut RenderContext<'a>, elapsed: f64) -> &'a str {
2521 let t = elapsed % 10.0;
2622 let scale = 0.45 + (if t > 5.0 { 10.0 - t } else { t }) / 40.0;
2723 let transform = bumpalo::format!(
@@ -33,7 +29,7 @@ impl Container {
3329 }
3430
3531 // Generate the dot's position on the grid.
36− fn dot_style<'bump>(&self, cx: &mut RenderContext<'bump>, x: f64, y: f64) -> &'bump str {
32+ fn dot_style<'a>(&self, cx: &mut RenderContext<'a>, x: f64, y: f64) -> &'a str {
3733 let s = self.size * 1.3;
3834 let radius = s / 2.0;
3935 let styles = bumpalo::format!(
@@ -52,13 +48,7 @@ impl Container {
5248 }
5349
5450 // Create a dot node.
55− fn dot<'bump>(
56− &self,
57− cx: &mut RenderContext<'bump>,
58− x: f64,
59− y: f64,
60− content: u32,
61− ) -> Node<'bump> {
51+ fn dot<'a>(&self, cx: &mut RenderContext<'a>, x: f64, y: f64, content: u32) -> Node<'a> {
6252 use dodrio::builder::{div, text};
6353
6454 div(&cx)
@@ -71,14 +61,14 @@ impl Container {
7161 }
7262
7363 // Create a flattened vector of dot nodes (arranged in recursive triangles)
74− fn triangle<'bump>(
64+ fn triangle<'a>(
7565 &self,
76− cx: &mut RenderContext<'bump>,
66+ cx: &mut RenderContext<'a>,
7767 x: f64,
7868 y: f64,
7969 s: f64,
8070 content: u32,
81− children: &mut bumpalo::collections::Vec<Node<'bump>>,
71+ children: &mut bumpalo::collections::Vec<Node<'a>>,
8272 ) {
8373 if s <= self.size {
8474 children.push(self.dot(cx, x, y, content));
@@ -97,7 +87,7 @@ impl Container {
9787}
9888
9989impl Render for Container {
100− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
90+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
10191 use dodrio::builder::div;
10292
10393 let elapsed = web_sys::window()
examples/todomvc/src/todo.rs+1−1
@@ -93,7 +93,7 @@ impl<C> Todo<C> {
9393}
9494
9595impl<C: TodoActions> Render for Todo<C> {
96− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
96+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
9797 use dodrio::{
9898 builder::*,
9999 bumpalo::{self, collections::String},
examples/todomvc/src/todos.rs+7−7
@@ -133,7 +133,7 @@ impl<C> Todos<C> {
133133
134134/// Rendering helpers.
135135impl<C: TodosActions> Todos<C> {
136− fn header<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
136+ fn header<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
137137 use dodrio::builder::*;
138138
139139 header(&cx)
@@ -166,7 +166,7 @@ impl<C: TodosActions> Todos<C> {
166166 .finish()
167167 }
168168
169− fn todos_list<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
169+ fn todos_list<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
170170 use dodrio::{builder::*, bumpalo::collections::Vec};
171171
172172 let mut todos = Vec::with_capacity_in(self.todos.len(), cx.bump);
@@ -211,7 +211,7 @@ impl<C: TodosActions> Todos<C> {
211211 .finish()
212212 }
213213
214− fn footer<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
214+ fn footer<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
215215 use dodrio::builder::*;
216216
217217 let completed_count = self.todos.iter().filter(|t| t.is_complete()).count();
@@ -262,12 +262,12 @@ impl<C: TodosActions> Todos<C> {
262262 .finish()
263263 }
264264
265− fn visibility_swap<'bump>(
265+ fn visibility_swap<'a>(
266266 &self,
267− cx: &mut RenderContext<'bump>,
267+ cx: &mut RenderContext<'a>,
268268 url: &'static str,
269269 target_vis: Visibility,
270− ) -> Node<'bump> {
270+ ) -> Node<'a> {
271271 use dodrio::builder::*;
272272
273273 li(&cx)
@@ -291,7 +291,7 @@ impl<C: TodosActions> Todos<C> {
291291}
292292
293293impl<C: TodosActions> Render for Todos<C> {
294− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
294+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
295295 use dodrio::builder::*;
296296
297297 div(&cx)
src/lib.rs+1−1
@@ -19,7 +19,7 @@
1919//! }
2020//!
2121//! impl<'who> Render for Hello<'who> {
22−//! fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
22+//! fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
2323//! use dodrio::builder::*;
2424//!
2525//! let id = bumpalo::format!(in cx.bump, "hello-{}", self.who);
src/render.rs+4−4
@@ -22,7 +22,7 @@ use wasm_bindgen::UnwrapThrowExt;
2222/// pub struct MyComponent;
2323///
2424/// impl Render for MyComponent {
25−/// fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
25+/// fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
2626/// use dodrio::builder::*;
2727///
2828/// p(&cx)
@@ -38,14 +38,14 @@ use wasm_bindgen::UnwrapThrowExt;
3838pub trait Render {
3939 /// Render `self` as a virtual DOM. Use the given context's `Bump` for
4040 /// temporary allocations.
41− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump>;
41+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a>;
4242}
4343
4444impl<'r, R> Render for &'r R
4545where
4646 R: Render,
4747{
48− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
48+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
4949 (**self).render(cx)
5050 }
5151}
@@ -54,7 +54,7 @@ impl<R> Render for Rc<R>
5454where
5555 R: Render,
5656{
57− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
57+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
5858 (**self).render(cx)
5959 }
6060}
src/vdom.rs+1−4
@@ -158,10 +158,7 @@ impl Vdom {
158158
159159 /// Construct a `Vdom` with the already-boxed-as-a-trait-object root
160160 /// rendering component.
161− pub fn with_boxed_root_render<'a, 'bump>(
162− container: &crate::Element,
163− component: Box<RootRender>,
164− ) -> Vdom {
161+ pub fn with_boxed_root_render(container: &crate::Element, component: Box<RootRender>) -> Vdom {
165162 let dom_buffers = [Bump::new(), Bump::new()];
166163 let change_list = ManuallyDrop::new(ChangeList::new(container));
167164
tests/web/events.rs+2−2
@@ -25,7 +25,7 @@ impl EventContainer {
2525}
2626
2727impl Render for EventContainer {
28− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
28+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
2929 use dodrio::builder::*;
3030 div(&cx)
3131 .attr("id", "target")
@@ -134,7 +134,7 @@ impl ListensOnlyOnFirstRender {
134134}
135135
136136impl Render for ListensOnlyOnFirstRender {
137− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
137+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
138138 use dodrio::builder::*;
139139
140140 let count = self.count.get();
tests/web/js_api.rs+1−1
@@ -37,7 +37,7 @@ impl WrapJs {
3737}
3838
3939impl Render for WrapJs {
40− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
40+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
4141 div(&cx)
4242 .attr("class", "wrap-js")
4343 .children([self.inner.render(cx)])
tests/web/main.rs+3−3
@@ -151,13 +151,13 @@ pub fn assert_rendered<R: Render>(container: &web_sys::Element, r: &R) {
151151/// Use the function `F` to render.
152152pub struct RenderFn<F>(F)
153153where
154− F: for<'bump> Fn(&mut RenderContext<'bump>) -> Node<'bump>;
154+ F: for<'a> Fn(&mut RenderContext<'a>) -> Node<'a>;
155155
156156impl<F> Render for RenderFn<F>
157157where
158− F: for<'bump> Fn(&mut RenderContext<'bump>) -> Node<'bump>,
158+ F: for<'a> Fn(&mut RenderContext<'a>) -> Node<'a>,
159159{
160− fn render<'bump>(&self, cx: &mut RenderContext<'bump>) -> Node<'bump> {
160+ fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
161161 (self.0)(cx)
162162 }
163163}