feat: make `__Props` imports unnecessary (closes #746) (#828)

This commit is contained in:
Greg Johnston 2023-04-07 15:06:10 -04:00 committed by GitHub
parent 5dab35447a
commit 016ad6b7a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 239 additions and 186 deletions

View File

@ -106,6 +106,9 @@ in as if it were an HTML element attribute. Simple.
> sure you include this `ComponentProps` type:
>
> `use progress_bar::{ProgressBar, ProgressBarProps};`
>
> **Note**: This is still true as of `0.2.5`, but the requirement has been removed on `main`
> and will not apply to later versions.
### Reactive and Static Props

View File

@ -1,4 +1,4 @@
use counter::*;
use counter::SimpleCounter;
use leptos::*;
pub fn main() {

View File

@ -1,4 +1,4 @@
use counters::{Counters, CountersProps};
use counters::Counters;
use leptos::*;
fn main() {

View File

@ -218,3 +218,21 @@ pub type ChildrenFnMut = Box<dyn FnMut(Scope) -> Fragment>;
/// }
/// ```
pub type AttributeValue = Box<dyn IntoAttribute>;
#[doc(hidden)]
pub trait Component<P> {}
#[doc(hidden)]
pub trait Props {
type Builder;
fn builder() -> Self::Builder;
}
impl<P, F, R> Component<P> for F where F: FnOnce(::leptos::Scope, P) -> R {}
#[doc(hidden)]
pub fn component_props_builder<P: Props>(
_f: &impl Component<P>,
) -> <P as Props>::Builder {
<P as Props>::builder()
}

View File

@ -137,6 +137,7 @@ impl ToTokens for Model {
let lifetimes = body.sig.generics.lifetimes();
let props_name = format_ident!("{name}Props");
let props_builder_name = format_ident!("{name}PropsBuilder");
let trace_name = format!("<{name} />");
let prop_builder_fields = prop_builder_fields(vis, props);
@ -200,6 +201,13 @@ impl ToTokens for Model {
#prop_builder_fields
}
impl #generics ::leptos::Props for #props_name #generics #where_clause {
type Builder = #props_builder_name #generics;
fn builder() -> Self::Builder {
#props_name::builder()
}
}
#docs
#component_fn_prop_docs
#[allow(non_snake_case, clippy::too_many_arguments)]

View File

@ -1062,7 +1062,6 @@ pub(crate) fn component_to_tokens(
let name = &node.name;
let component_name = ident_from_tag_name(&node.name);
let span = node.name.span();
let component_props_name = format_ident!("{component_name}Props");
let attrs = node.attributes.iter().filter_map(|node| {
if let Node::Attribute(node) = node {
@ -1154,7 +1153,7 @@ pub(crate) fn component_to_tokens(
let component = quote! {
#name(
#cx,
#component_props_name::builder()
::leptos::component_props_builder(&#name)
#(#props)*
#children
.build()

View File

@ -1,7 +1,9 @@
use criterion::{criterion_group, criterion_main, Criterion};
fn rs_deep_update(c: &mut Criterion) {
use reactive_signals::{Signal, signal, runtimes::ClientRuntime, types::Func};
use reactive_signals::{
runtimes::ClientRuntime, signal, types::Func, Signal,
};
c.bench_function("rs_deep_update", |b| {
b.iter(|| {
@ -25,78 +27,84 @@ fn rs_deep_update(c: &mut Criterion) {
fn l021_deep_update(c: &mut Criterion) {
use l021::*;
c.bench_function("l021_deep_update", |b| {
let runtime = create_runtime();
b.iter(|| {
create_scope(runtime, |cx| {
let signal = create_rw_signal(cx, 0);
let mut memos = Vec::<Memo<usize>>::new();
for i in 0..1000usize {
let prev = memos.get(i.saturating_sub(1)).copied();
if let Some(prev) = prev {
memos.push(create_memo(cx, move |_| prev.get() + 1));
} else {
memos.push(create_memo(cx, move |_| signal.get() + 1));
}
}
signal.set(1);
assert_eq!(memos[999].get(), 1001);
})
.dispose()
});
runtime.dispose();
});
c.bench_function("l021_deep_update", |b| {
let runtime = create_runtime();
b.iter(|| {
create_scope(runtime, |cx| {
let signal = create_rw_signal(cx, 0);
let mut memos = Vec::<Memo<usize>>::new();
for i in 0..1000usize {
let prev = memos.get(i.saturating_sub(1)).copied();
if let Some(prev) = prev {
memos.push(create_memo(cx, move |_| prev.get() + 1));
} else {
memos.push(create_memo(cx, move |_| signal.get() + 1));
}
}
signal.set(1);
assert_eq!(memos[999].get(), 1001);
})
.dispose()
});
runtime.dispose();
});
}
fn sycamore_deep_update(c: &mut Criterion) {
use sycamore::reactive::*;
c.bench_function("sycamore_deep_update", |b| {
b.iter(|| {
let d = create_scope(|cx| {
let signal = create_signal(cx, 0);
let mut memos = Vec::<&ReadSignal<usize>>::new();
for i in 0..1000usize {
let prev = memos.get(i.saturating_sub(1)).copied();
if let Some(prev) = prev {
memos.push(create_memo(cx, move || *prev.get() + 1));
} else {
memos.push(create_memo(cx, move || *signal.get() + 1));
}
}
signal.set(1);
assert_eq!(*memos[999].get(), 1001);
});
unsafe { d.dispose() };
});
});
c.bench_function("sycamore_deep_update", |b| {
b.iter(|| {
let d = create_scope(|cx| {
let signal = create_signal(cx, 0);
let mut memos = Vec::<&ReadSignal<usize>>::new();
for i in 0..1000usize {
let prev = memos.get(i.saturating_sub(1)).copied();
if let Some(prev) = prev {
memos.push(create_memo(cx, move || *prev.get() + 1));
} else {
memos.push(create_memo(cx, move || *signal.get() + 1));
}
}
signal.set(1);
assert_eq!(*memos[999].get(), 1001);
});
unsafe { d.dispose() };
});
});
}
fn leptos_deep_update(c: &mut Criterion) {
use leptos::*;
let runtime = create_runtime();
c.bench_function("leptos_deep_update", |b| {
b.iter(|| {
create_scope(runtime, |cx| {
let signal = create_rw_signal(cx, 0);
let mut memos = Vec::<Memo<usize>>::new();
for i in 0..1000usize {
let prev = memos.get(i.saturating_sub(1)).copied();
if let Some(prev) = prev {
memos.push(create_memo(cx, move |_| prev.get() + 1));
} else {
memos.push(create_memo(cx, move |_| signal.get() + 1));
}
}
signal.set(1);
assert_eq!(memos[999].get(), 1001);
})
.dispose()
});
});
c.bench_function("leptos_deep_update", |b| {
b.iter(|| {
create_scope(runtime, |cx| {
let signal = create_rw_signal(cx, 0);
let mut memos = Vec::<Memo<usize>>::new();
for i in 0..1000usize {
let prev = memos.get(i.saturating_sub(1)).copied();
if let Some(prev) = prev {
memos.push(create_memo(cx, move |_| prev.get() + 1));
} else {
memos.push(create_memo(cx, move |_| signal.get() + 1));
}
}
signal.set(1);
assert_eq!(memos[999].get(), 1001);
})
.dispose()
});
});
runtime.dispose();
}
criterion_group!(deep, rs_deep_update, l021_deep_update, sycamore_deep_update, leptos_deep_update);
criterion_main!(deep);
criterion_group!(
deep,
rs_deep_update,
l021_deep_update,
sycamore_deep_update,
leptos_deep_update
);
criterion_main!(deep);

View File

@ -1,18 +1,20 @@
use criterion::{criterion_group, criterion_main, Criterion};
fn rs_fan_out(c: &mut Criterion) {
use reactive_signals::{Signal, signal, runtimes::ClientRuntime, types::Func};
use reactive_signals::{
runtimes::ClientRuntime, signal, types::Func, Signal,
};
c.bench_function("rs_fan_out", |b| {
b.iter(|| {
let cx = ClientRuntime::bench_root_scope();
let sig = signal!(cx, 0);
let memos = (0..1000)
.map(|_| signal!(cx, move || sig.get()))
.collect::<Vec<_>>();
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 0);
sig.set(1);
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 1000);
let sig = signal!(cx, 0);
let memos = (0..1000)
.map(|_| signal!(cx, move || sig.get()))
.collect::<Vec<_>>();
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 0);
sig.set(1);
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 1000);
});
});
}
@ -20,63 +22,72 @@ fn rs_fan_out(c: &mut Criterion) {
fn l021_fan_out(c: &mut Criterion) {
use l021::*;
c.bench_function("l021_fan_out", |b| {
let runtime = create_runtime();
b.iter(|| {
create_scope(runtime, |cx| {
let sig = create_rw_signal(cx, 0);
let memos = (0..1000)
.map(|_| create_memo(cx, move |_| sig.get()))
.collect::<Vec<_>>();
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 0);
sig.set(1);
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 1000);
})
.dispose()
});
runtime.dispose();
});
c.bench_function("l021_fan_out", |b| {
let runtime = create_runtime();
b.iter(|| {
create_scope(runtime, |cx| {
let sig = create_rw_signal(cx, 0);
let memos = (0..1000)
.map(|_| create_memo(cx, move |_| sig.get()))
.collect::<Vec<_>>();
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 0);
sig.set(1);
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 1000);
})
.dispose()
});
runtime.dispose();
});
}
fn sycamore_fan_out(c: &mut Criterion) {
use sycamore::reactive::*;
c.bench_function("sycamore_fan_out", |b| {
b.iter(|| {
let d = create_scope(|cx| {
let sig = create_signal(cx, 0);
let memos = (0..1000)
.map(|_| create_memo(cx, move || sig.get()))
.collect::<Vec<_>>();
assert_eq!(memos.iter().map(|m| *(*m.get())).sum::<i32>(), 0);
sig.set(1);
assert_eq!(memos.iter().map(|m| *(*m.get())).sum::<i32>(), 1000);
});
unsafe { d.dispose() };
});
});
c.bench_function("sycamore_fan_out", |b| {
b.iter(|| {
let d = create_scope(|cx| {
let sig = create_signal(cx, 0);
let memos = (0..1000)
.map(|_| create_memo(cx, move || sig.get()))
.collect::<Vec<_>>();
assert_eq!(memos.iter().map(|m| *(*m.get())).sum::<i32>(), 0);
sig.set(1);
assert_eq!(
memos.iter().map(|m| *(*m.get())).sum::<i32>(),
1000
);
});
unsafe { d.dispose() };
});
});
}
fn leptos_fan_out(c: &mut Criterion) {
use leptos_reactive::*;
let runtime = create_runtime();
c.bench_function("leptos_fan_out", |b| {
b.iter(|| {
create_scope(runtime, |cx| {
let sig = create_rw_signal(cx, 0);
let memos = (0..1000)
.map(|_| create_memo(cx, move |_| sig.get()))
.collect::<Vec<_>>();
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 0);
sig.set(1);
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 1000);
})
.dispose()
});
});
c.bench_function("leptos_fan_out", |b| {
b.iter(|| {
create_scope(runtime, |cx| {
let sig = create_rw_signal(cx, 0);
let memos = (0..1000)
.map(|_| create_memo(cx, move |_| sig.get()))
.collect::<Vec<_>>();
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 0);
sig.set(1);
assert_eq!(memos.iter().map(|m| m.get()).sum::<i32>(), 1000);
})
.dispose()
});
});
runtime.dispose();
}
criterion_group!(fan_out, rs_fan_out, l021_fan_out, sycamore_fan_out, leptos_fan_out);
criterion_main!(fan_out);
criterion_group!(
fan_out,
rs_fan_out,
l021_fan_out,
sycamore_fan_out,
leptos_fan_out
);
criterion_main!(fan_out);

View File

@ -1,22 +1,22 @@
use criterion::{criterion_group, criterion_main, Criterion};
use std::rc::Rc;
use std::cell::Cell;
use std::{cell::Cell, rc::Rc};
fn rs_narrow_down(c: &mut Criterion) {
use reactive_signals::{Signal, signal, runtimes::ClientRuntime, types::Func};
use reactive_signals::{
runtimes::ClientRuntime, signal, types::Func, Signal,
};
c.bench_function("rs_narrow_down", |b| {
b.iter(|| {
let cx = ClientRuntime::bench_root_scope();
let acc = Rc::new(Cell::new(0));
let sigs = Rc::new(
(0..1000).map(|n| signal!(cx, n)).collect::<Vec<_>>(),
);
let memo = signal!(cx, {
let sigs = Rc::clone(&sigs);
move || sigs.iter().map(|r| r.get()).sum::<i32>()
});
assert_eq!(memo.get(), 499500);
let acc = Rc::new(Cell::new(0));
let sigs =
Rc::new((0..1000).map(|n| signal!(cx, n)).collect::<Vec<_>>());
let memo = signal!(cx, {
let sigs = Rc::clone(&sigs);
move || sigs.iter().map(|r| r.get()).sum::<i32>()
});
assert_eq!(memo.get(), 499500);
});
});
}
@ -24,69 +24,75 @@ fn rs_narrow_down(c: &mut Criterion) {
fn l021_narrow_down(c: &mut Criterion) {
use l021::*;
c.bench_function("l021_narrow_down", |b| {
let runtime = create_runtime();
b.iter(|| {
create_scope(runtime, |cx| {
let acc = Rc::new(Cell::new(0));
let sigs =
(0..1000).map(|n| create_signal(cx, n)).collect::<Vec<_>>();
let reads = sigs.iter().map(|(r, _)| *r).collect::<Vec<_>>();
let writes = sigs.iter().map(|(_, w)| *w).collect::<Vec<_>>();
let memo = create_memo(cx, move |_| {
reads.iter().map(|r| r.get()).sum::<i32>()
});
assert_eq!(memo(), 499500);
})
.dispose()
});
runtime.dispose();
});
c.bench_function("l021_narrow_down", |b| {
let runtime = create_runtime();
b.iter(|| {
create_scope(runtime, |cx| {
let acc = Rc::new(Cell::new(0));
let sigs =
(0..1000).map(|n| create_signal(cx, n)).collect::<Vec<_>>();
let reads = sigs.iter().map(|(r, _)| *r).collect::<Vec<_>>();
let writes = sigs.iter().map(|(_, w)| *w).collect::<Vec<_>>();
let memo = create_memo(cx, move |_| {
reads.iter().map(|r| r.get()).sum::<i32>()
});
assert_eq!(memo(), 499500);
})
.dispose()
});
runtime.dispose();
});
}
fn sycamore_narrow_down(c: &mut Criterion) {
use sycamore::reactive::*;
c.bench_function("sycamore_narrow_down", |b| {
b.iter(|| {
let d = create_scope(|cx| {
let acc = Rc::new(Cell::new(0));
let sigs = Rc::new(
(0..1000).map(|n| create_signal(cx, n)).collect::<Vec<_>>(),
);
let memo = create_memo(cx, {
let sigs = Rc::clone(&sigs);
move || sigs.iter().map(|r| *r.get()).sum::<i32>()
});
assert_eq!(*memo.get(), 499500);
});
unsafe { d.dispose() };
});
});
c.bench_function("sycamore_narrow_down", |b| {
b.iter(|| {
let d = create_scope(|cx| {
let acc = Rc::new(Cell::new(0));
let sigs = Rc::new(
(0..1000).map(|n| create_signal(cx, n)).collect::<Vec<_>>(),
);
let memo = create_memo(cx, {
let sigs = Rc::clone(&sigs);
move || sigs.iter().map(|r| *r.get()).sum::<i32>()
});
assert_eq!(*memo.get(), 499500);
});
unsafe { d.dispose() };
});
});
}
fn leptos_narrow_down(c: &mut Criterion) {
use leptos_reactive::*;
let runtime = create_runtime();
c.bench_function("leptos_narrow_down", |b| {
b.iter(|| {
create_scope(runtime, |cx| {
let acc = Rc::new(Cell::new(0));
let sigs =
(0..1000).map(|n| create_signal(cx, n)).collect::<Vec<_>>();
let reads = sigs.iter().map(|(r, _)| *r).collect::<Vec<_>>();
let writes = sigs.iter().map(|(_, w)| *w).collect::<Vec<_>>();
let memo = create_memo(cx, move |_| {
reads.iter().map(|r| r.get()).sum::<i32>()
});
assert_eq!(memo(), 499500);
})
.dispose()
});
});
c.bench_function("leptos_narrow_down", |b| {
b.iter(|| {
create_scope(runtime, |cx| {
let acc = Rc::new(Cell::new(0));
let sigs =
(0..1000).map(|n| create_signal(cx, n)).collect::<Vec<_>>();
let reads = sigs.iter().map(|(r, _)| *r).collect::<Vec<_>>();
let writes = sigs.iter().map(|(_, w)| *w).collect::<Vec<_>>();
let memo = create_memo(cx, move |_| {
reads.iter().map(|r| r.get()).sum::<i32>()
});
assert_eq!(memo(), 499500);
})
.dispose()
});
});
runtime.dispose();
}
criterion_group!(narrow_down, rs_narrow_down, l021_narrow_down, sycamore_narrow_down, leptos_narrow_down);
criterion_main!(narrow_down);
criterion_group!(
narrow_down,
rs_narrow_down,
l021_narrow_down,
sycamore_narrow_down,
leptos_narrow_down
);
criterion_main!(narrow_down);

View File

@ -1,4 +1,4 @@
use crate::{Link, LinkProps};
use crate::Link;
use leptos::*;
/// Injects an [HTMLLinkElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement) into the document

View File

@ -72,7 +72,7 @@ where
P: std::fmt::Display + 'static,
C: Fn(Scope) -> bool + 'static,
{
use crate::{Redirect, RedirectProps};
use crate::Redirect;
let redirect_path = redirect_path.to_string();
define_route(