From a964e89d1a43d17c2a26256c9c97c3fcbd0e9864 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Fri, 20 Jan 2023 10:08:13 -0500 Subject: [PATCH] `cargo fmt` --- benchmarks/src/ssr.rs | 63 ++--- benchmarks/src/todomvc/leptos.rs | 412 +++++++++++++++---------------- benchmarks/src/todomvc/mod.rs | 8 +- leptos_dom/src/ssr.rs | 7 +- 4 files changed, 246 insertions(+), 244 deletions(-) diff --git a/benchmarks/src/ssr.rs b/benchmarks/src/ssr.rs index ac319d5..1c04b35 100644 --- a/benchmarks/src/ssr.rs +++ b/benchmarks/src/ssr.rs @@ -2,7 +2,7 @@ use test::Bencher; #[bench] fn leptos_ssr_bench(b: &mut Bencher) { - b.iter(|| { + b.iter(|| { use leptos::*; HydrationCtx::reset_id(); _ = create_scope(create_runtime(), |cx| { @@ -39,10 +39,10 @@ fn leptos_ssr_bench(b: &mut Bencher) { #[bench] fn tera_ssr_bench(b: &mut Bencher) { - use tera::*; - use serde::{Serialize, Deserialize}; + use serde::{Deserialize, Serialize}; + use tera::*; - static TEMPLATE: &str = r#"
+ static TEMPLATE: &str = r#"

Welcome to our benchmark page.

Here's some introductory text.

{% for counter in counters %} @@ -54,37 +54,40 @@ fn tera_ssr_bench(b: &mut Bencher) { {% endfor %}
"#; - lazy_static::lazy_static! { - static ref TERA: Tera = { - let mut tera = Tera::default(); - tera.add_raw_templates(vec![("template.html", TEMPLATE)]).unwrap(); - tera - }; - } + lazy_static::lazy_static! { + static ref TERA: Tera = { + let mut tera = Tera::default(); + tera.add_raw_templates(vec![("template.html", TEMPLATE)]).unwrap(); + tera + }; + } - #[derive(Serialize, Deserialize)] - struct Counter { - value: i32 - } + #[derive(Serialize, Deserialize)] + struct Counter { + value: i32, + } - b.iter(|| { - let mut ctx = Context::new(); - ctx.insert("counters", &vec![ - Counter { value: 0 }, - Counter { value: 1}, - Counter { value: 2 } - ]); + b.iter(|| { + let mut ctx = Context::new(); + ctx.insert( + "counters", + &vec![ + Counter { value: 0 }, + Counter { value: 1 }, + Counter { value: 2 }, + ], + ); - let _ = TERA.render("template.html", &ctx).unwrap(); - }); + let _ = TERA.render("template.html", &ctx).unwrap(); + }); } #[bench] fn sycamore_ssr_bench(b: &mut Bencher) { - use sycamore::*; - use sycamore::prelude::*; + use sycamore::prelude::*; + use sycamore::*; - b.iter(|| { + b.iter(|| { _ = create_scope(|cx| { #[derive(Prop)] struct CounterProps { @@ -138,10 +141,10 @@ fn sycamore_ssr_bench(b: &mut Bencher) { #[bench] fn yew_ssr_bench(b: &mut Bencher) { - use yew::prelude::*; - use yew::ServerRenderer; + use yew::prelude::*; + use yew::ServerRenderer; - b.iter(|| { + b.iter(|| { #[derive(Properties, PartialEq, Eq, Debug)] struct CounterProps { initial: i32 diff --git a/benchmarks/src/todomvc/leptos.rs b/benchmarks/src/todomvc/leptos.rs index f18a086..d107cbf 100644 --- a/benchmarks/src/todomvc/leptos.rs +++ b/benchmarks/src/todomvc/leptos.rs @@ -8,171 +8,165 @@ pub struct Todos(pub Vec); const STORAGE_KEY: &str = "todos-leptos"; impl Todos { - pub fn new(cx: Scope) -> Self { - Self(vec![]) - } + pub fn new(cx: Scope) -> Self { + Self(vec![]) + } - pub fn new_with_1000(cx: Scope) -> Self { - let todos = (0..1000) - .map(|id| Todo::new(cx, id, format!("Todo #{id}"))) - .collect(); - Self(todos) - } + pub fn new_with_1000(cx: Scope) -> Self { + let todos = (0..1000) + .map(|id| Todo::new(cx, id, format!("Todo #{id}"))) + .collect(); + Self(todos) + } - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } - pub fn add(&mut self, todo: Todo) { - self.0.push(todo); - } + pub fn add(&mut self, todo: Todo) { + self.0.push(todo); + } - pub fn remove(&mut self, id: usize) { - self.0.retain(|todo| todo.id != id); - } + pub fn remove(&mut self, id: usize) { + self.0.retain(|todo| todo.id != id); + } - pub fn remaining(&self) -> usize { - self.0.iter().filter(|todo| !(todo.completed)()).count() - } + pub fn remaining(&self) -> usize { + self.0.iter().filter(|todo| !(todo.completed)()).count() + } - pub fn completed(&self) -> usize { - self.0.iter().filter(|todo| (todo.completed)()).count() - } + pub fn completed(&self) -> usize { + self.0.iter().filter(|todo| (todo.completed)()).count() + } - pub fn toggle_all(&self) { - // if all are complete, mark them all active instead - if self.remaining() == 0 { - for todo in &self.0 { - if todo.completed.get() { - (todo.set_completed)(false); + pub fn toggle_all(&self) { + // if all are complete, mark them all active instead + if self.remaining() == 0 { + for todo in &self.0 { + if todo.completed.get() { + (todo.set_completed)(false); + } + } + } + // otherwise, mark them all complete + else { + for todo in &self.0 { + (todo.set_completed)(true); + } } - } } - // otherwise, mark them all complete - else { - for todo in &self.0 { - (todo.set_completed)(true); - } - } - } - fn clear_completed(&mut self) { - self.0.retain(|todo| !todo.completed.get()); - } + fn clear_completed(&mut self) { + self.0.retain(|todo| !todo.completed.get()); + } } #[derive(Debug, PartialEq, Eq, Clone)] pub struct Todo { - pub id: usize, - pub title: ReadSignal, - pub set_title: WriteSignal, - pub completed: ReadSignal, - pub set_completed: WriteSignal, + pub id: usize, + pub title: ReadSignal, + pub set_title: WriteSignal, + pub completed: ReadSignal, + pub set_completed: WriteSignal, } impl Todo { - pub fn new(cx: Scope, id: usize, title: String) -> Self { - Self::new_with_completed(cx, id, title, false) - } - - pub fn new_with_completed( - cx: Scope, - id: usize, - title: String, - completed: bool, - ) -> Self { - let (title, set_title) = create_signal(cx, title); - let (completed, set_completed) = create_signal(cx, completed); - Self { - id, - title, - set_title, - completed, - set_completed, + pub fn new(cx: Scope, id: usize, title: String) -> Self { + Self::new_with_completed(cx, id, title, false) } - } - pub fn toggle(&self) { - self - .set_completed - .update(|completed| *completed = !*completed); - } + pub fn new_with_completed(cx: Scope, id: usize, title: String, completed: bool) -> Self { + let (title, set_title) = create_signal(cx, title); + let (completed, set_completed) = create_signal(cx, completed); + Self { + id, + title, + set_title, + completed, + set_completed, + } + } + + pub fn toggle(&self) { + self.set_completed + .update(|completed| *completed = !*completed); + } } const ESCAPE_KEY: u32 = 27; const ENTER_KEY: u32 = 13; #[component] -pub fn TodoMVC(cx: Scope,todos: Todos) -> impl IntoView { - let mut next_id = todos - .0 - .iter() - .map(|todo| todo.id) - .max() - .map(|last| last + 1) - .unwrap_or(0); - - let (todos, set_todos) = create_signal(cx, todos); - provide_context(cx, set_todos); - - let (mode, set_mode) = create_signal(cx, Mode::All); - window_event_listener("hashchange", move |_| { - let new_mode = location_hash().map(|hash| route(&hash)).unwrap_or_default(); - set_mode(new_mode); - }); - - let add_todo = move |ev: web_sys::KeyboardEvent| { - let target = event_target::(&ev); - ev.stop_propagation(); - let key_code = ev.unchecked_ref::().key_code(); - if key_code == ENTER_KEY { - let title = event_target_value(&ev); - let title = title.trim(); - if !title.is_empty() { - let new = Todo::new(cx, next_id, title.to_string()); - set_todos.update(|t| t.add(new)); - next_id += 1; - target.set_value(""); - } - } - }; - - let filtered_todos = create_memo::>(cx, move |_| { - todos.with(|todos| match mode.get() { - Mode::All => todos.0.to_vec(), - Mode::Active => todos +pub fn TodoMVC(cx: Scope, todos: Todos) -> impl IntoView { + let mut next_id = todos .0 .iter() - .filter(|todo| !todo.completed.get()) - .cloned() - .collect(), - Mode::Completed => todos - .0 - .iter() - .filter(|todo| todo.completed.get()) - .cloned() - .collect(), - }) - }); + .map(|todo| todo.id) + .max() + .map(|last| last + 1) + .unwrap_or(0); - // effect to serialize to JSON - // this does reactive reads, so it will automatically serialize on any relevant change - create_effect(cx, move |_| { - if let Ok(Some(storage)) = window().local_storage() { - let objs = todos - .get() - .0 - .iter() - .map(TodoSerialized::from) - .collect::>(); - let json = json::to_string(&objs); - if storage.set_item(STORAGE_KEY, &json).is_err() { - log::error!("error while trying to set item in localStorage"); - } - } - }); + let (todos, set_todos) = create_signal(cx, todos); + provide_context(cx, set_todos); - view! { cx, + let (mode, set_mode) = create_signal(cx, Mode::All); + window_event_listener("hashchange", move |_| { + let new_mode = location_hash().map(|hash| route(&hash)).unwrap_or_default(); + set_mode(new_mode); + }); + + let add_todo = move |ev: web_sys::KeyboardEvent| { + let target = event_target::(&ev); + ev.stop_propagation(); + let key_code = ev.unchecked_ref::().key_code(); + if key_code == ENTER_KEY { + let title = event_target_value(&ev); + let title = title.trim(); + if !title.is_empty() { + let new = Todo::new(cx, next_id, title.to_string()); + set_todos.update(|t| t.add(new)); + next_id += 1; + target.set_value(""); + } + } + }; + + let filtered_todos = create_memo::>(cx, move |_| { + todos.with(|todos| match mode.get() { + Mode::All => todos.0.to_vec(), + Mode::Active => todos + .0 + .iter() + .filter(|todo| !todo.completed.get()) + .cloned() + .collect(), + Mode::Completed => todos + .0 + .iter() + .filter(|todo| todo.completed.get()) + .cloned() + .collect(), + }) + }); + + // effect to serialize to JSON + // this does reactive reads, so it will automatically serialize on any relevant change + create_effect(cx, move |_| { + if let Ok(Some(storage)) = window().local_storage() { + let objs = todos + .get() + .0 + .iter() + .map(TodoSerialized::from) + .collect::>(); + let json = json::to_string(&objs); + if storage.set_item(STORAGE_KEY, &json).is_err() { + log::error!("error while trying to set item in localStorage"); + } + } + }); + + view! { cx,
@@ -228,100 +222,100 @@ pub fn TodoMVC(cx: Scope,todos: Todos) -> impl IntoView { #[component] pub fn Todo(cx: Scope, todo: Todo) -> impl IntoView { - let (editing, set_editing) = create_signal(cx, false); - let set_todos = use_context::>(cx).unwrap(); - //let input = NodeRef::new(cx); + let (editing, set_editing) = create_signal(cx, false); + let set_todos = use_context::>(cx).unwrap(); + //let input = NodeRef::new(cx); - let save = move |value: &str| { - let value = value.trim(); - if value.is_empty() { - set_todos.update(|t| t.remove(todo.id)); - } else { - (todo.set_title)(value.to_string()); + let save = move |value: &str| { + let value = value.trim(); + if value.is_empty() { + set_todos.update(|t| t.remove(todo.id)); + } else { + (todo.set_title)(value.to_string()); + } + set_editing(false); + }; + + view! { cx, +
  • +
    + + +
    + {move || editing().then(|| view! { cx, + ().key_code(); + if key_code == ENTER_KEY { + save(&event_target_value(&ev)); + } else if key_code == ESCAPE_KEY { + set_editing(false); + } + }} + /> + }) + } +
  • } - set_editing(false); - }; - - view! { cx, -
  • -
    - - -
    - {move || editing().then(|| view! { cx, - ().key_code(); - if key_code == ENTER_KEY { - save(&event_target_value(&ev)); - } else if key_code == ESCAPE_KEY { - set_editing(false); - } - }} - /> - }) - } -
  • - } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Mode { - Active, - Completed, - All, + Active, + Completed, + All, } impl Default for Mode { - fn default() -> Self { - Mode::All - } + fn default() -> Self { + Mode::All + } } pub fn route(hash: &str) -> Mode { - match hash { - "/active" => Mode::Active, - "/completed" => Mode::Completed, - _ => Mode::All, - } + match hash { + "/active" => Mode::Active, + "/completed" => Mode::Completed, + _ => Mode::All, + } } #[derive(Serialize, Deserialize)] pub struct TodoSerialized { - pub id: usize, - pub title: String, - pub completed: bool, + pub id: usize, + pub title: String, + pub completed: bool, } impl TodoSerialized { - pub fn into_todo(self, cx: Scope) -> Todo { - Todo::new_with_completed(cx, self.id, self.title, self.completed) - } + pub fn into_todo(self, cx: Scope) -> Todo { + Todo::new_with_completed(cx, self.id, self.title, self.completed) + } } impl From<&Todo> for TodoSerialized { - fn from(todo: &Todo) -> Self { - Self { - id: todo.id, - title: todo.title.get(), - completed: (todo.completed)(), + fn from(todo: &Todo) -> Self { + Self { + id: todo.id, + title: todo.title.get(), + completed: (todo.completed)(), + } } - } } diff --git a/benchmarks/src/todomvc/mod.rs b/benchmarks/src/todomvc/mod.rs index 416a329..1280759 100644 --- a/benchmarks/src/todomvc/mod.rs +++ b/benchmarks/src/todomvc/mod.rs @@ -14,7 +14,9 @@ fn leptos_todomvc_ssr(b: &mut Bencher) { let rendered = view! { cx, - }.into_view(cx).render_to_string(cx); + } + .into_view(cx) + .render_to_string(cx); assert!(rendered.len() > 1); }); @@ -55,7 +57,7 @@ fn yew_todomvc_ssr(b: &mut Bencher) { }); }); } -/* +/* #[bench] fn leptos_todomvc_ssr_with_1000(b: &mut Bencher) { b.iter(|| { @@ -107,4 +109,4 @@ fn yew_todomvc_ssr_with_1000(b: &mut Bencher) { }); }); } - */ \ No newline at end of file + */ diff --git a/leptos_dom/src/ssr.rs b/leptos_dom/src/ssr.rs index d183a6f..af18072 100644 --- a/leptos_dom/src/ssr.rs +++ b/leptos_dom/src/ssr.rs @@ -417,6 +417,9 @@ fn to_kebab_case(name: &str) -> String { } #[doc(hidden)] -pub fn escape_attr(value: &T) -> Cow<'_, str> where T: AsRef{ - html_escape::encode_double_quoted_attribute(value) +pub fn escape_attr(value: &T) -> Cow<'_, str> +where + T: AsRef, +{ + html_escape::encode_double_quoted_attribute(value) }