Skip to content

Commit bc03c60

Browse files
committed
Use Vec to implement the Events iterators
Using `vec::IntoIter` is much simpler than a deeply nested `Chain`, compiling faster and avoiding the deeper recursion limit reported in [rust#71359]. [rust#71359]: rust-lang/rust#71359.
1 parent e65a960 commit bc03c60

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

typed-html/src/events.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use crate::OutputType;
44
use htmlescape::encode_attribute;
55
use std::fmt::{Display, Error, Formatter};
6-
use std::iter;
76

87
/// Trait for event handlers.
98
pub trait EventHandler<T: OutputType + Send, E: Send> {
@@ -34,41 +33,38 @@ macro_rules! declare_events_struct {
3433

3534
impl<T: Send> Events<T> {
3635
pub fn iter(&self) -> impl Iterator<Item = (&'static str, &T)> {
37-
iter::empty()
36+
let mut vec = Vec::new();
3837
$(
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+
}
4341
)*
42+
vec.into_iter()
4443
}
4544

4645
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&'static str, &mut T)> {
47-
iter::empty()
46+
let mut vec = Vec::new();
4847
$(
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+
}
5351
)*
52+
vec.into_iter()
5453
}
5554
}
5655

5756
impl<T: 'static + Send> IntoIterator for Events<T> {
5857
type Item = (&'static str, T);
5958
type IntoIter = Box<dyn Iterator<Item = Self::Item>>;
6059

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())
7268
}
7369
}
7470

0 commit comments

Comments
 (0)