Do not .clone() Element in code generated with generate_element

This should improve performance a little.
This commit is contained in:
Jonas Schäfer 2024-03-02 10:07:34 +01:00
commit 3b3a4ff0c8
2 changed files with 99 additions and 22 deletions

View file

@ -491,6 +491,22 @@ impl Element {
}
}
/// Returns an iterator over the child Elements, draining the element of
/// all its child nodes **including text!**
///
/// This is a bit of a footgun, so we make this hidden in the docs (it
/// needs to be pub for macro use). Once `extract_if`
/// ([rust#43244](https://github.com/rust-lang/rust/issues/43244))
/// is stabilized, we can replace this with a take_children which doesn't
/// remove text nodes.
#[inline]
#[doc(hidden)]
pub fn take_contents_as_children(&mut self) -> ContentsAsChildren {
ContentsAsChildren {
iter: self.children.drain(..),
}
}
/// Returns an iterator over references to every text node of this element.
///
/// # Examples
@ -757,6 +773,24 @@ impl<'a> Iterator for ChildrenMut<'a> {
}
}
/// An iterator over references to child elements of an `Element`.
pub struct ContentsAsChildren<'a> {
iter: std::vec::Drain<'a, Node>,
}
impl<'a> Iterator for ContentsAsChildren<'a> {
type Item = Element;
fn next(&mut self) -> Option<Element> {
for item in &mut self.iter {
if let Node::Element(child) = item {
return Some(child);
}
}
None
}
}
/// An iterator over references to child text nodes of an `Element`.
pub struct Texts<'a> {
iter: slice::Iter<'a, Node>,