Introduce the LayoutIterator newtype and return it for all children() methods in style and layout.

This commit is contained in:
Bobby Holley 2016-09-20 17:25:38 -07:00
parent e6bbff110a
commit 4aa3e589c0
4 changed files with 25 additions and 16 deletions

View file

@ -58,7 +58,8 @@ use style::attr::AttrValue;
use style::computed_values::display; use style::computed_values::display;
use style::context::SharedStyleContext; use style::context::SharedStyleContext;
use style::data::PrivateStyleData; use style::data::PrivateStyleData;
use style::dom::{NodeInfo, OpaqueNode, PresentationalHintsSynthetizer, TDocument, TElement, TNode, UnsafeNode}; use style::dom::{LayoutIterator, NodeInfo, OpaqueNode, PresentationalHintsSynthetizer, TDocument, TElement, TNode};
use style::dom::UnsafeNode;
use style::element_state::*; use style::element_state::*;
use style::properties::{ComputedValues, PropertyDeclarationBlock}; use style::properties::{ComputedValues, PropertyDeclarationBlock};
use style::refcell::{Ref, RefCell, RefMut}; use style::refcell::{Ref, RefCell, RefMut};
@ -149,10 +150,10 @@ impl<'ln> TNode for ServoLayoutNode<'ln> {
self.dump_style_indent(0); self.dump_style_indent(0);
} }
fn children(self) -> ServoChildrenIterator<'ln> { fn children(self) -> LayoutIterator<ServoChildrenIterator<'ln>> {
ServoChildrenIterator { LayoutIterator(ServoChildrenIterator {
current: self.first_child(), current: self.first_child(),
} })
} }
fn opaque(&self) -> OpaqueNode { fn opaque(&self) -> OpaqueNode {
@ -771,8 +772,8 @@ impl<'ln> ThreadSafeLayoutNode for ServoThreadSafeLayoutNode<'ln> {
self.node.debug_id() self.node.debug_id()
} }
fn children(&self) -> Self::ChildrenIterator { fn children(&self) -> LayoutIterator<Self::ChildrenIterator> {
ThreadSafeLayoutNodeChildrenIterator::new(*self) LayoutIterator(ThreadSafeLayoutNodeChildrenIterator::new(*self))
} }
fn as_element(&self) -> ServoThreadSafeLayoutElement<'ln> { fn as_element(&self) -> ServoThreadSafeLayoutElement<'ln> {

View file

@ -15,7 +15,7 @@ use std::sync::Arc;
use string_cache::{Atom, Namespace}; use string_cache::{Atom, Namespace};
use style::computed_values::display; use style::computed_values::display;
use style::context::SharedStyleContext; use style::context::SharedStyleContext;
use style::dom::{NodeInfo, PresentationalHintsSynthetizer, TNode}; use style::dom::{LayoutIterator, NodeInfo, PresentationalHintsSynthetizer, TNode};
use style::dom::OpaqueNode; use style::dom::OpaqueNode;
use style::properties::ServoComputedValues; use style::properties::ServoComputedValues;
use style::refcell::{Ref, RefCell}; use style::refcell::{Ref, RefCell};
@ -81,10 +81,10 @@ pub trait LayoutNode: TNode {
fn init_style_and_layout_data(&self, data: OpaqueStyleAndLayoutData); fn init_style_and_layout_data(&self, data: OpaqueStyleAndLayoutData);
fn get_style_and_layout_data(&self) -> Option<OpaqueStyleAndLayoutData>; fn get_style_and_layout_data(&self) -> Option<OpaqueStyleAndLayoutData>;
fn rev_children(self) -> ReverseChildrenIterator<Self> { fn rev_children(self) -> LayoutIterator<ReverseChildrenIterator<Self>> {
ReverseChildrenIterator { LayoutIterator(ReverseChildrenIterator {
current: self.last_child(), current: self.last_child(),
} })
} }
fn traverse_preorder(self) -> TreeIterator<Self> { fn traverse_preorder(self) -> TreeIterator<Self> {
@ -169,7 +169,7 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
fn debug_id(self) -> usize; fn debug_id(self) -> usize;
/// Returns an iterator over this node's children. /// Returns an iterator over this node's children.
fn children(&self) -> Self::ChildrenIterator; fn children(&self) -> LayoutIterator<Self::ChildrenIterator>;
/// If this is an element, accesses the element data. Fails if this is not an element node. /// If this is an element, accesses the element data. Fails if this is not an element node.
#[inline] #[inline]

View file

@ -73,6 +73,14 @@ pub trait NodeInfo {
fn is_text_node(&self) -> bool; fn is_text_node(&self) -> bool;
} }
pub struct LayoutIterator<T>(pub T);
impl<T, I> Iterator for LayoutIterator<T> where T: Iterator<Item=I>, I: NodeInfo {
type Item = I;
fn next(&mut self) -> Option<I> {
self.0.next()
}
}
pub trait TNode : Sized + Copy + Clone + NodeInfo { pub trait TNode : Sized + Copy + Clone + NodeInfo {
type ConcreteElement: TElement<ConcreteNode = Self, ConcreteDocument = Self::ConcreteDocument>; type ConcreteElement: TElement<ConcreteNode = Self, ConcreteDocument = Self::ConcreteDocument>;
type ConcreteDocument: TDocument<ConcreteNode = Self, ConcreteElement = Self::ConcreteElement>; type ConcreteDocument: TDocument<ConcreteNode = Self, ConcreteElement = Self::ConcreteElement>;
@ -87,7 +95,7 @@ pub trait TNode : Sized + Copy + Clone + NodeInfo {
fn dump_style(self); fn dump_style(self);
/// Returns an iterator over this node's children. /// Returns an iterator over this node's children.
fn children(self) -> Self::ConcreteChildrenIterator; fn children(self) -> LayoutIterator<Self::ConcreteChildrenIterator>;
/// Converts self into an `OpaqueNode`. /// Converts self into an `OpaqueNode`.
fn opaque(&self) -> OpaqueNode; fn opaque(&self) -> OpaqueNode;

View file

@ -39,8 +39,8 @@ use std::ops::BitOr;
use std::ptr; use std::ptr;
use std::sync::Arc; use std::sync::Arc;
use style::data::PrivateStyleData; use style::data::PrivateStyleData;
use style::dom::{LayoutIterator, NodeInfo, TDocument, TElement, TNode, TRestyleDamage, UnsafeNode};
use style::dom::{OpaqueNode, PresentationalHintsSynthetizer}; use style::dom::{OpaqueNode, PresentationalHintsSynthetizer};
use style::dom::{NodeInfo, TDocument, TElement, TNode, TRestyleDamage, UnsafeNode};
use style::element_state::ElementState; use style::element_state::ElementState;
use style::error_reporting::StdoutErrorReporter; use style::error_reporting::StdoutErrorReporter;
use style::gecko_selector_impl::{GeckoSelectorImpl, NonTSPseudoClass, PseudoElement}; use style::gecko_selector_impl::{GeckoSelectorImpl, NonTSPseudoClass, PseudoElement};
@ -161,12 +161,12 @@ impl<'ln> TNode for GeckoNode<'ln> {
unimplemented!() unimplemented!()
} }
fn children(self) -> GeckoChildrenIterator<'ln> { fn children(self) -> LayoutIterator<GeckoChildrenIterator<'ln>> {
let maybe_iter = unsafe { Gecko_MaybeCreateStyleChildrenIterator(self.0) }; let maybe_iter = unsafe { Gecko_MaybeCreateStyleChildrenIterator(self.0) };
if let Some(iter) = maybe_iter.into_owned_opt() { if let Some(iter) = maybe_iter.into_owned_opt() {
GeckoChildrenIterator::GeckoIterator(iter) LayoutIterator(GeckoChildrenIterator::GeckoIterator(iter))
} else { } else {
GeckoChildrenIterator::Current(self.first_child()) LayoutIterator(GeckoChildrenIterator::Current(self.first_child()))
} }
} }