Skip to content

Commit e09b6bf

Browse files
committed
chore: tweak generic arguments and iterators in rsx
1 parent e8ae830 commit e09b6bf

File tree

7 files changed

+93
-66
lines changed

7 files changed

+93
-66
lines changed

examples/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn app(cx: Scope) -> Element {
1616
oninput: move |e| script.set(e.value.clone()),
1717
}
1818
button {
19-
onclick: move |_| eval(script),
19+
onclick: move |_| eval(script.to_string()),
2020
"Execute"
2121
}
2222
}

examples/framework_benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn app(cx: Scope) -> Element {
7272
td { class:"col-md-1" }
7373
td { class:"col-md-1", "{item.key}" }
7474
td { class:"col-md-1", onclick: move |_| selected.set(Some(id)),
75-
a { class: "lbl", item.labels }
75+
a { class: "lbl", "{item.labels[0]}{item.labels[1]}{item.labels[2]}" }
7676
}
7777
td { class: "col-md-1",
7878
a { class: "remove", onclick: move |_| { items.write().remove(id); },

examples/generic_component.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::fmt::Display;
2+
3+
use dioxus::prelude::*;
4+
5+
fn main() {
6+
dioxus_desktop::launch(app);
7+
}
8+
9+
fn app(cx: Scope) -> Element {
10+
cx.render(rsx! {
11+
generic_child::<i32>{}
12+
})
13+
}
14+
15+
fn generic_child<T>(cx: Scope) -> Element {
16+
cx.render(rsx! {
17+
div {}
18+
})
19+
}

examples/rsx_usage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn app(cx: Scope) -> Element {
183183

184184
// Components can be generic too
185185
// This component takes i32 type to give you typed input
186-
TypedInput::<TypedInputProps<i32>> {}
186+
TypedInput::<i32> {}
187187

188188
// Type inference can be used too
189189
TypedInput { initial: 10.0 }

examples/simple_list.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ fn app(cx: Scope) -> Element {
88
cx.render(rsx!(
99
// Use Map directly to lazily pull elements
1010
(0..10).map(|f| rsx! { "{f}" }),
11-
// Collect into an intermediate collection if necessary
11+
// Collect into an intermediate collection if necessary, and call into_iter
1212
["a", "b", "c"]
1313
.into_iter()
1414
.map(|f| rsx! { "{f}" })
15-
.collect::<Vec<_>>(),
15+
.collect::<Vec<_>>()
16+
.into_iter(),
1617
// Use optionals
1718
Some(rsx! { "Some" }),
1819
))

packages/core/src/factory.rs

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,11 @@ impl ScopeState {
4141
}
4242
}
4343

44-
pub fn fragment_from_iter<'a, 'c, I, J>(
44+
pub fn fragment_from_iter<'a, 'c, I>(
4545
&'a self,
46-
node_iter: impl IntoVNode<'a, I, J> + 'c,
46+
node_iter: impl IntoDynNode<'a, I> + 'c,
4747
) -> DynamicNode {
4848
node_iter.into_vnode(self)
49-
50-
// let mut bump_vec = bumpalo::vec![in self.bump();];
51-
52-
// for item in it {
53-
// bump_vec.push(item.into_vnode(self));
54-
// }
55-
56-
// match bump_vec.len() {
57-
// 0 => DynamicNode::Placeholder(Cell::new(ElementId(0))),
58-
// _ => DynamicNode::Fragment {
59-
// inner: false,
60-
// nodes: bump_vec.into_bump_slice(),
61-
// },
62-
// }
6349
}
6450

6551
/// Create a new [`Attribute`]
@@ -155,46 +141,65 @@ impl<'a> RenderReturn<'a> {
155141
}
156142
}
157143

158-
pub trait IntoVNode<'a, A = (), J = ()> {
144+
pub trait IntoDynNode<'a, A = ()> {
159145
fn into_vnode(self, cx: &'a ScopeState) -> DynamicNode<'a>;
160146
}
161147

162-
impl<'a, 'b> IntoVNode<'a> for VNode<'a> {
148+
impl<'a, 'b> IntoDynNode<'a> for () {
163149
fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
164150
todo!()
165151
// self
166152
}
167153
}
154+
impl<'a, 'b> IntoDynNode<'a> for VNode<'a> {
155+
fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
156+
// DynamicNode::Fragment { nodes: cx., inner: () }
157+
todo!()
158+
}
159+
}
168160

169-
impl<'a, 'b> IntoVNode<'a> for LazyNodes<'a, 'b> {
170-
fn into_vnode(self, cx: &'a ScopeState) -> DynamicNode<'a> {
161+
impl<'a, 'b, T: IntoDynNode<'a>> IntoDynNode<'a> for Option<T> {
162+
fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
163+
// DynamicNode::Fragment { nodes: cx., inner: () }
171164
todo!()
172-
// self.call(cx)
173165
}
174166
}
175167

176-
impl<'b> IntoVNode<'_> for &'b str {
177-
fn into_vnode(self, cx: &ScopeState) -> DynamicNode {
178-
// cx.text(format_args!("{}", self))
168+
impl<'a, 'b, T: IntoDynNode<'a>> IntoDynNode<'a> for &Option<T> {
169+
fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
170+
// DynamicNode::Fragment { nodes: cx., inner: () }
179171
todo!()
180172
}
181173
}
182174

183-
impl IntoVNode<'_> for String {
175+
impl<'a, 'b> IntoDynNode<'a> for LazyNodes<'a, 'b> {
176+
fn into_vnode(self, cx: &'a ScopeState) -> DynamicNode<'a> {
177+
DynamicNode::Fragment {
178+
nodes: cx.bump().alloc([self.call(cx)]),
179+
inner: false,
180+
}
181+
}
182+
}
183+
184+
impl<'b> IntoDynNode<'_> for &'b str {
184185
fn into_vnode(self, cx: &ScopeState) -> DynamicNode {
185-
// cx.text(format_args!("{}", self))
186-
todo!()
186+
cx.text(format_args!("{}", self))
187187
}
188188
}
189189

190-
impl<'b> IntoVNode<'b> for Arguments<'_> {
190+
impl IntoDynNode<'_> for String {
191+
fn into_vnode(self, cx: &ScopeState) -> DynamicNode {
192+
cx.text(format_args!("{}", self))
193+
}
194+
}
195+
196+
impl<'b> IntoDynNode<'b> for Arguments<'_> {
191197
fn into_vnode(self, cx: &'b ScopeState) -> DynamicNode<'b> {
192-
// cx.text(self)
193-
todo!()
198+
cx.text(self)
194199
}
195200
}
196201

197-
impl<'a, 'b> IntoVNode<'a> for &VNode<'a> {
202+
impl<'a, 'b> IntoDynNode<'a> for &VNode<'a> {
198203
fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
199204
todo!()
200205
// VNode {
@@ -209,46 +214,43 @@ impl<'a, 'b> IntoVNode<'a> for &VNode<'a> {
209214
}
210215
}
211216

217+
pub trait IntoTemplate<'a> {
218+
fn into_template(self, _cx: &'a ScopeState) -> VNode<'a>;
219+
}
220+
impl<'a, 'b> IntoTemplate<'a> for VNode<'a> {
221+
fn into_template(self, _cx: &'a ScopeState) -> VNode<'a> {
222+
self
223+
}
224+
}
225+
impl<'a, 'b> IntoTemplate<'a> for LazyNodes<'a, 'b> {
226+
fn into_template(self, cx: &'a ScopeState) -> VNode<'a> {
227+
self.call(cx)
228+
}
229+
}
230+
212231
// Note that we're using the E as a generic but this is never crafted anyways.
213232
pub struct FromNodeIterator;
214-
impl<'a, T, I, E> IntoVNode<'a, FromNodeIterator, E> for T
233+
impl<'a, T, I> IntoDynNode<'a, FromNodeIterator> for T
215234
where
216-
T: IntoIterator<Item = I>,
217-
I: IntoVNode<'a, E>,
235+
T: Iterator<Item = I>,
236+
I: IntoTemplate<'a>,
218237
{
219238
fn into_vnode(self, cx: &'a ScopeState) -> DynamicNode<'a> {
220239
let mut nodes = bumpalo::collections::Vec::new_in(cx.bump());
221240

222241
for node in self {
223-
nodes.push(node.into_vnode(cx));
242+
nodes.push(node.into_template(cx));
224243
}
225244

226245
let children = nodes.into_bump_slice();
227246

228-
// if cfg!(debug_assertions) && children.len() > 1 && children.last().unwrap().key().is_none()
229-
// {
230-
// let bt = backtrace::Backtrace::new();
231-
// let bt = "no backtrace available";
232-
233-
// // todo: make the backtrace prettier or remove it altogether
234-
// log::error!(
235-
// r#"
236-
// Warning: Each child in an array or iterator should have a unique "key" prop.
237-
// Not providing a key will lead to poor performance with lists.
238-
// See docs.rs/dioxus for more information.
239-
// -------------
240-
// {:?}
241-
// "#,
242-
// bt
243-
// );
244-
// }
245-
246-
todo!()
247-
// VNode::Fragment(cx.bump.alloc(VFragment {
248-
// children,
249-
// placeholder: Default::default(),
250-
// key: None,
251-
// }))
247+
match children.len() {
248+
0 => DynamicNode::Placeholder(Cell::new(ElementId(0))),
249+
_ => DynamicNode::Fragment {
250+
inner: false,
251+
nodes: children,
252+
},
253+
}
252254
}
253255
}
254256

packages/rsx/src/component.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl ToTokens for Component {
154154
}
155155
None => {
156156
let mut toks = match prop_gen_args {
157-
Some(gen_args) => quote! { fc_to_builder #gen_args(#name) },
157+
Some(gen_args) => quote! { fc_to_builder(#name #gen_args) },
158158
None => quote! { fc_to_builder(#name) },
159159
};
160160
for field in &self.fields {
@@ -187,9 +187,14 @@ impl ToTokens for Component {
187187

188188
let fn_name = self.name.segments.last().unwrap().ident.to_string();
189189

190+
let gen_name = match &self.prop_gen_args {
191+
Some(gen) => quote! { #name #gen },
192+
None => quote! { #name },
193+
};
194+
190195
tokens.append_all(quote! {
191196
__cx.component(
192-
#name,
197+
#gen_name,
193198
#builder,
194199
#fn_name
195200
)

0 commit comments

Comments
 (0)