|
3 | 3 | use crate::OutputType;
|
4 | 4 | use htmlescape::encode_attribute;
|
5 | 5 | use std::fmt::{Display, Error, Formatter};
|
6 |
| -use std::iter; |
7 | 6 |
|
8 | 7 | /// Trait for event handlers.
|
9 | 8 | pub trait EventHandler<T: OutputType + Send, E: Send> {
|
@@ -34,41 +33,38 @@ macro_rules! declare_events_struct {
|
34 | 33 |
|
35 | 34 | impl<T: Send> Events<T> {
|
36 | 35 | pub fn iter(&self) -> impl Iterator<Item = (&'static str, &T)> {
|
37 |
| - iter::empty() |
| 36 | + let mut vec = Vec::new(); |
38 | 37 | $(
|
39 |
| - .chain( |
40 |
| - self.$name.iter() |
41 |
| - .map(|value| (stringify!($name), value)) |
42 |
| - ) |
| 38 | + if let Some(ref value) = self.$name { |
| 39 | + vec.push((stringify!($name), value)); |
| 40 | + } |
43 | 41 | )*
|
| 42 | + vec.into_iter() |
44 | 43 | }
|
45 | 44 |
|
46 | 45 | pub fn iter_mut(&mut self) -> impl Iterator<Item = (&'static str, &mut T)> {
|
47 |
| - iter::empty() |
| 46 | + let mut vec = Vec::new(); |
48 | 47 | $(
|
49 |
| - .chain( |
50 |
| - self.$name.iter_mut() |
51 |
| - .map(|value| (stringify!($name), value)) |
52 |
| - ) |
| 48 | + if let Some(ref mut value) = self.$name { |
| 49 | + vec.push((stringify!($name), value)); |
| 50 | + } |
53 | 51 | )*
|
| 52 | + vec.into_iter() |
54 | 53 | }
|
55 | 54 | }
|
56 | 55 |
|
57 | 56 | impl<T: 'static + Send> IntoIterator for Events<T> {
|
58 | 57 | type Item = (&'static str, T);
|
59 | 58 | type IntoIter = Box<dyn Iterator<Item = Self::Item>>;
|
60 | 59 |
|
61 |
| - fn into_iter(mut self) -> Self::IntoIter { |
62 |
| - Box::new( |
63 |
| - iter::empty() |
64 |
| - $( |
65 |
| - .chain( |
66 |
| - iter::once(self.$name.take()) |
67 |
| - .filter(Option::is_some) |
68 |
| - .map(|value| (stringify!($name), value.unwrap())) |
69 |
| - ) |
70 |
| - )* |
71 |
| - ) |
| 60 | + fn into_iter(self) -> Self::IntoIter { |
| 61 | + let mut vec = Vec::new(); |
| 62 | + $( |
| 63 | + if let Some(value) = self.$name { |
| 64 | + vec.push((stringify!($name), value)); |
| 65 | + } |
| 66 | + )* |
| 67 | + Box::new(vec.into_iter()) |
72 | 68 | }
|
73 | 69 | }
|
74 | 70 |
|
|
0 commit comments