From e753516ba0bd67c29567c08cb530f11dbe3f61f6 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 22 Apr 2020 12:17:27 -0700 Subject: [PATCH] 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](https://github.com/rust-lang/rust/issues/71359). --- typed-html/src/events.rs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/typed-html/src/events.rs b/typed-html/src/events.rs index 038619d..cb10504 100644 --- a/typed-html/src/events.rs +++ b/typed-html/src/events.rs @@ -3,7 +3,6 @@ use crate::OutputType; use htmlescape::encode_attribute; use std::fmt::{Display, Error, Formatter}; -use std::iter; /// Trait for event handlers. pub trait EventHandler { @@ -34,23 +33,23 @@ macro_rules! declare_events_struct { impl Events { pub fn iter(&self) -> impl Iterator { - iter::empty() + let mut vec = Vec::new(); $( - .chain( - self.$name.iter() - .map(|value| (stringify!($name), value)) - ) + if let Some(ref value) = self.$name { + vec.push((stringify!($name), value)); + } )* + vec.into_iter() } pub fn iter_mut(&mut self) -> impl Iterator { - iter::empty() + let mut vec = Vec::new(); $( - .chain( - self.$name.iter_mut() - .map(|value| (stringify!($name), value)) - ) + if let Some(ref mut value) = self.$name { + vec.push((stringify!($name), value)); + } )* + vec.into_iter() } } @@ -58,17 +57,14 @@ macro_rules! declare_events_struct { type Item = (&'static str, T); type IntoIter = Box>; - fn into_iter(mut self) -> Self::IntoIter { - Box::new( - iter::empty() - $( - .chain( - iter::once(self.$name.take()) - .filter(Option::is_some) - .map(|value| (stringify!($name), value.unwrap())) - ) - )* - ) + fn into_iter(self) -> Self::IntoIter { + let mut vec = Vec::new(); + $( + if let Some(value) = self.$name { + vec.push((stringify!($name), value)); + } + )* + Box::new(vec.into_iter()) } }