Skip to content

Commit 5d5b4dd

Browse files
Merge branch 'master' into fix/custom_el_diff
2 parents f0d7b97 + e1adbea commit 5d5b4dd

File tree

3 files changed

+40
-58
lines changed

3 files changed

+40
-58
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Added method `El::is_custom(&self)`.
66
- Fixed custom elements patching (#325).
7+
- Implemented `UpdateEl` for `Filter` and `FilterMap`.
78

89
## v0.5.1
910
- [BREAKING] `MessageMapper::map_message` changed to `MessageMapper::map_msg`.

Diff for: examples/todomvc/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ fn view(model: &Model) -> impl View<Msg> {
295295
// We use the item's position in model.todos to identify it, because this allows
296296
// simple in-place modification through indexing. This is different from its
297297
// position in visible todos, hence the two-step process.
298-
let todo_els: Vec<Node<Msg>> = model
298+
let todo_els = model
299299
.todos
300300
.clone()
301301
.into_iter()
@@ -306,8 +306,7 @@ fn view(model: &Model) -> impl View<Msg> {
306306
} else {
307307
None
308308
}
309-
})
310-
.collect();
309+
});
311310

312311
let main = if model.todos.is_empty() {
313312
seed::empty()

Diff for: src/virtual_dom/update_el.rs

+37-55
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,25 @@ impl<Ms> UpdateEl<El<Ms>> for WillUnmount<Ms> {
9595
}
9696

9797
impl<Ms> UpdateEl<El<Ms>> for &str {
98-
// This, or some other mechanism seems to work for String too... note sure why.
9998
fn update(self, el: &mut El<Ms>) {
10099
el.children.push(Node::Text(Text::new(self.to_string())))
101100
}
102101
}
103102

103+
// In the most cases `&str` is enough,
104+
// but if we have, for instance, `Filter` iterator of `String`s -
105+
// then the Rust type system can't coerce `String` to `&str`.
106+
//
107+
// However if we implement `UpdateEl` for `String`, code like `h1![model.title]` cannot be compiled,
108+
// because Rust chooses `String` impl instead of `&str` and fails on moving value (`title`).
109+
// @TODO How to resolve it? `&self`?
110+
//
111+
//impl<Ms> UpdateEl<El<Ms>> for String {
112+
// fn update(self, el: &mut El<Ms>) {
113+
// el.children.push(Node::Text(Text::new(self)))
114+
// }
115+
//}
116+
104117
impl<Ms> UpdateEl<El<Ms>> for El<Ms> {
105118
fn update(self, el: &mut El<Ms>) {
106119
el.children.push(Node::Element(self))
@@ -133,6 +146,8 @@ impl<Ms> UpdateEl<El<Ms>> for Tag {
133146
}
134147
}
135148

149+
// ----- Iterators ------
150+
136151
impl<Ms, I, U, F> UpdateEl<El<Ms>> for std::iter::Map<I, F>
137152
where
138153
I: Iterator,
@@ -144,57 +159,24 @@ where
144159
}
145160
}
146161

147-
// impl<Ms, I, U, P> UpdateEl<El<Ms>> for std::iter::Filter<I, P>
148-
// where
149-
// I: Iterator,
150-
// U: UpdateEl<El<Ms>>,
151-
// P: FnMut(&I::Item) -> bool,
152-
// {
153-
// fn update(self, el: &mut El<Ms>) {
154-
// self.for_each(|item| item.update(el));
155-
// }
156-
// }
157-
158-
//impl<Ms, I, U, F> UpdateEl<El<Ms>> for std::iter::Map<I, F>
159-
//where
160-
// I: Iterator,
161-
// U: UpdateEl<Attrs>,
162-
// F: FnMut(I::Item) -> U,
163-
//{
164-
// fn update(self, el: &mut El<Ms>) {
165-
// self.for_each(|item| item.update(el));
166-
// }
167-
//}
168-
//
169-
//impl<Ms, I, U, F> UpdateEl<El<Ms>> for std::iter::Map<I, F>
170-
//where
171-
// I: Iterator,
172-
// U: UpdateEl<&Attrs>,
173-
// F: FnMut(I::Item) -> U,
174-
//{
175-
// fn update(self, el: &mut El<Ms>) {
176-
// self.for_each(|item| item.update(el));
177-
// }
178-
//}
179-
//
180-
//impl<Ms, I, U, F> UpdateEl<El<Ms>> for std::iter::Map<I, F>
181-
//where
182-
// I: Iterator,
183-
// U: UpdateEl<Style>,
184-
// F: FnMut(I::Item) -> U,
185-
//{
186-
// fn update(self, el: &mut El<Ms>) {
187-
// self.for_each(|item| item.update(el));
188-
// }
189-
//}
190-
//
191-
//impl<Ms, I, U, F> UpdateEl<El<Ms>> for std::iter::Map<I, F>
192-
//where
193-
// I: Iterator,
194-
// U: UpdateEl<&Style>,
195-
// F: FnMut(I::Item) -> U,
196-
//{
197-
// fn update(self, el: &mut El<Ms>) {
198-
// self.for_each(|item| item.update(el));
199-
// }
200-
//}
162+
impl<Ms, I, U, F> UpdateEl<El<Ms>> for std::iter::FilterMap<I, F>
163+
where
164+
I: Iterator,
165+
U: UpdateEl<El<Ms>>,
166+
F: FnMut(I::Item) -> Option<U>,
167+
{
168+
fn update(self, el: &mut El<Ms>) {
169+
self.for_each(|item| item.update(el));
170+
}
171+
}
172+
173+
impl<Ms, I, U, P> UpdateEl<El<Ms>> for std::iter::Filter<I, P>
174+
where
175+
U: UpdateEl<El<Ms>>,
176+
I: Iterator<Item = U>,
177+
P: FnMut(&I::Item) -> bool,
178+
{
179+
fn update(self, el: &mut El<Ms>) {
180+
self.for_each(|item| item.update(el));
181+
}
182+
}

0 commit comments

Comments
 (0)