Skip to content

Commit

Permalink
Fix build errors for examples
Browse files Browse the repository at this point in the history
- The type signature for the method 'render' of Render trait seems to
  have changed, causing examples to fail.
  • Loading branch information
dgellow authored and bodil committed Jan 27, 2022
1 parent 4c13ecc commit d1525a3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 53 deletions.
12 changes: 5 additions & 7 deletions examples/dodrio/counter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![recursion_limit = "128"]

use dodrio::builder::text;
use dodrio::bumpalo::{self, Bump};
use dodrio::bumpalo::{self};
use dodrio::Render;
use log::*;
use typed_html::dodrio;
Expand Down Expand Up @@ -31,13 +31,11 @@ impl Counter {

// The `Render` implementation for `Counter`s displays the current count and has
// buttons to increment and decrement the count.
impl Render for Counter {
fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> dodrio::Node<'bump>
where
'a: 'bump,
{
impl<'a> Render<'a> for Counter {
fn render(&self, cx: &mut dodrio::RenderContext<'a>) -> dodrio::Node<'a> {
// Stringify the count as a bump-allocated string.
let count = bumpalo::format!(in bump, "{}", self.count);
let count = bumpalo::format!(in cx.bump, "{}", self.count);
let bump = cx.bump;

dodrio!(bump,
<div id="counter">
Expand Down
4 changes: 2 additions & 2 deletions examples/dodrio/todomvc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ version = "0.1.0"
[dependencies]
cfg-if = "0.1.7"
dodrio = "0.2.0"
futures = "0.1.25"
futures = "0.3"
js-sys = "0.3.15"
serde = { features = ["derive"], version = "1.0.89" }
serde_json = "1.0.39"
wasm-bindgen = "0.2.38"
wasm-bindgen-futures = "0.3.15"
wasm-bindgen-futures = "0.4"
typed-html = { path = "../../../typed-html", features = ["dodrio_macro"] }

# Optional dependencies for logging.
Expand Down
9 changes: 5 additions & 4 deletions examples/dodrio/todomvc/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::todos::Todos;
use crate::utils;
use crate::visibility::Visibility;
use dodrio::VdomWeak;
use futures::prelude::*;
use std::str::FromStr;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
Expand All @@ -30,7 +29,8 @@ pub fn start(vdom: VdomWeak) {
v
});

wasm_bindgen_futures::spawn_local(
let vdom = vdom.clone();
wasm_bindgen_futures::spawn_local(async move {
vdom.with_component({
let vdom = vdom.clone();
move |root| {
Expand All @@ -45,8 +45,9 @@ pub fn start(vdom: VdomWeak) {
}
}
})
.map_err(|_| ()),
);
.await
.ok();
});
};

// Call it once to handle the initial `#` fragment.
Expand Down
13 changes: 6 additions & 7 deletions examples/dodrio/todomvc/src/todo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Type definition and `dodrio::Render` implementation for a single todo item.
use crate::keys;
use dodrio::{bumpalo::Bump, Node, Render, RootRender, VdomWeak};
use dodrio::{bumpalo::Bump, Node, Render, RenderContext, RootRender, VdomWeak};
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use typed_html::dodrio;
Expand Down Expand Up @@ -93,17 +93,16 @@ impl<C> Todo<C> {
}
}

impl<C: TodoActions> Render for Todo<C> {
fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
where
'a: 'bump,
{
impl<'a, C: TodoActions> Render<'a> for Todo<C> {
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
use dodrio::{builder::text, bumpalo};
use typed_html::types::ClassList;

let id = self.id;
let title = self.edits.as_ref().unwrap_or(&self.title);
let title = bumpalo::collections::String::from_str_in(title, cx.bump).into_bump_str();

let bump = cx.bump;
dodrio!(bump,
<li class={
let mut class = ClassList::new();
Expand All @@ -130,7 +129,7 @@ impl<C: TodoActions> Render for Todo<C> {
C::delete(root, vdom, id)
}}/>
</div>
<input class="edit" value={title.as_str()} name="title" id={
<input class="edit" value={title} name="title" id={
bumpalo::format!(in bump, "todo-{}", id).into_bump_str()
} oninput={move |root, vdom, event| {
let input = event
Expand Down
57 changes: 24 additions & 33 deletions examples/dodrio/todomvc/src/todos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use crate::controller::Controller;
use crate::todo::{Todo, TodoActions};
use crate::visibility::Visibility;
use crate::{keys, utils};
use dodrio::RenderContext;
use dodrio::{
builder::text,
bumpalo::{self, Bump},
bumpalo::{self},
Node, Render, RootRender, VdomWeak,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -138,10 +139,8 @@ impl<C> Todos<C> {

/// Rendering helpers.
impl<C: TodosActions> Todos<C> {
fn header<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
where
'a: 'bump,
{
fn header<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
let bump = cx.bump;
dodrio!(bump,
<header class="header">
<h1>"todos"</h1>
Expand All @@ -161,13 +160,10 @@ impl<C: TodosActions> Todos<C> {
)
}

fn todos_list<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
where
'a: 'bump,
{
fn todos_list<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
use dodrio::bumpalo::collections::Vec;

let mut todos = Vec::with_capacity_in(self.todos.len(), bump);
let mut todos = Vec::with_capacity_in(self.todos.len(), cx.bump);
todos.extend(
self.todos
.iter()
Expand All @@ -176,9 +172,10 @@ impl<C: TodosActions> Todos<C> {
Visibility::Active => !t.is_complete(),
Visibility::Completed => t.is_complete(),
})
.map(|t| t.render(bump)),
.map(|t| t.render(cx)),
);

let bump = cx.bump;
dodrio!(bump,
<section class="main" style={
if self.todos.is_empty() {
Expand All @@ -200,25 +197,23 @@ impl<C: TodosActions> Todos<C> {
)
}

fn footer<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
where
'a: 'bump,
{
fn footer<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
let completed_count = self.todos.iter().filter(|t| t.is_complete()).count();
let incomplete_count = self.todos.len() - completed_count;
let items_left = if incomplete_count == 1 {
" item left"
} else {
" items left"
};
let incomplete_count = bumpalo::format!(in bump, "{}", incomplete_count);
let incomplete_count = bumpalo::format!(in cx.bump, "{}", incomplete_count);

let clear_completed_text = bumpalo::format!(
in bump,
in cx.bump,
"Clear completed ({})",
self.todos.iter().filter(|t| t.is_complete()).count()
);

let bump = cx.bump;
dodrio!(bump,
<footer class="footer" hidden={self.todos.is_empty()}>
<span class="todo-count">
Expand All @@ -229,9 +224,9 @@ impl<C: TodosActions> Todos<C> {
</span>
<ul class="filters">
{ bumpalo::vec![in &bump;
self.visibility_swap(bump, "#/", Visibility::All),
self.visibility_swap(bump, "#/active", Visibility::Active),
self.visibility_swap(bump, "#/completed", Visibility::Completed)
self.visibility_swap(cx, "#/", Visibility::All),
self.visibility_swap(cx, "#/active", Visibility::Active),
self.visibility_swap(cx, "#/completed", Visibility::Completed)
] }
</ul>
<button class="clear-completed" hidden={completed_count == 0} onclick={|root, vdom, _event| {
Expand All @@ -241,15 +236,13 @@ impl<C: TodosActions> Todos<C> {
)
}

fn visibility_swap<'a, 'bump>(
&'a self,
bump: &'bump Bump,
fn visibility_swap<'a>(
&self,
cx: &mut RenderContext<'a>,
url: &'static str,
target_vis: Visibility,
) -> Node<'bump>
where
'a: 'bump,
{
) -> Node<'a> {
let bump = cx.bump;
dodrio!(bump,
<li onclick={move |root, vdom, _event| {
C::change_visibility(root, vdom, target_vis);
Expand All @@ -266,14 +259,12 @@ impl<C: TodosActions> Todos<C> {
}
}

impl<C: TodosActions> Render for Todos<C> {
fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
where
'a: 'bump,
{
impl<'a, C: TodosActions> Render<'a> for Todos<C> {
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
let bump = cx.bump;
dodrio!(bump,
<div>{ bumpalo::vec![in &bump;
self.header(bump), self.todos_list(bump), self.footer(bump)
self.header(cx), self.todos_list(cx), self.footer(cx)
] }</div>
)
}
Expand Down

0 comments on commit d1525a3

Please sign in to comment.