Skip to content

Commit

Permalink
Introduce the LayoutIterator newtype and return it for all children()…
Browse files Browse the repository at this point in the history
… methods in style and layout.
  • Loading branch information
bholley committed Sep 21, 2016
1 parent e6bbff1 commit 4aa3e58
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
13 changes: 7 additions & 6 deletions components/script/layout_wrapper.rs
Expand Up @@ -58,7 +58,8 @@ use style::attr::AttrValue;
use style::computed_values::display;
use style::context::SharedStyleContext;
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::properties::{ComputedValues, PropertyDeclarationBlock};
use style::refcell::{Ref, RefCell, RefMut};
Expand Down Expand Up @@ -149,10 +150,10 @@ impl<'ln> TNode for ServoLayoutNode<'ln> {
self.dump_style_indent(0);
}

fn children(self) -> ServoChildrenIterator<'ln> {
ServoChildrenIterator {
fn children(self) -> LayoutIterator<ServoChildrenIterator<'ln>> {
LayoutIterator(ServoChildrenIterator {
current: self.first_child(),
}
})
}

fn opaque(&self) -> OpaqueNode {
Expand Down Expand Up @@ -771,8 +772,8 @@ impl<'ln> ThreadSafeLayoutNode for ServoThreadSafeLayoutNode<'ln> {
self.node.debug_id()
}

fn children(&self) -> Self::ChildrenIterator {
ThreadSafeLayoutNodeChildrenIterator::new(*self)
fn children(&self) -> LayoutIterator<Self::ChildrenIterator> {
LayoutIterator(ThreadSafeLayoutNodeChildrenIterator::new(*self))
}

fn as_element(&self) -> ServoThreadSafeLayoutElement<'ln> {
Expand Down
10 changes: 5 additions & 5 deletions components/script_layout_interface/wrapper_traits.rs
Expand Up @@ -15,7 +15,7 @@ use std::sync::Arc;
use string_cache::{Atom, Namespace};
use style::computed_values::display;
use style::context::SharedStyleContext;
use style::dom::{NodeInfo, PresentationalHintsSynthetizer, TNode};
use style::dom::{LayoutIterator, NodeInfo, PresentationalHintsSynthetizer, TNode};
use style::dom::OpaqueNode;
use style::properties::ServoComputedValues;
use style::refcell::{Ref, RefCell};
Expand Down Expand Up @@ -81,10 +81,10 @@ pub trait LayoutNode: TNode {
fn init_style_and_layout_data(&self, data: OpaqueStyleAndLayoutData);
fn get_style_and_layout_data(&self) -> Option<OpaqueStyleAndLayoutData>;

fn rev_children(self) -> ReverseChildrenIterator<Self> {
ReverseChildrenIterator {
fn rev_children(self) -> LayoutIterator<ReverseChildrenIterator<Self>> {
LayoutIterator(ReverseChildrenIterator {
current: self.last_child(),
}
})
}

fn traverse_preorder(self) -> TreeIterator<Self> {
Expand Down Expand Up @@ -169,7 +169,7 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
fn debug_id(self) -> usize;

/// 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.
#[inline]
Expand Down
10 changes: 9 additions & 1 deletion components/style/dom.rs
Expand Up @@ -73,6 +73,14 @@ pub trait NodeInfo {
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 {
type ConcreteElement: TElement<ConcreteNode = Self, ConcreteDocument = Self::ConcreteDocument>;
type ConcreteDocument: TDocument<ConcreteNode = Self, ConcreteElement = Self::ConcreteElement>;
Expand All @@ -87,7 +95,7 @@ pub trait TNode : Sized + Copy + Clone + NodeInfo {
fn dump_style(self);

/// Returns an iterator over this node's children.
fn children(self) -> Self::ConcreteChildrenIterator;
fn children(self) -> LayoutIterator<Self::ConcreteChildrenIterator>;

/// Converts self into an `OpaqueNode`.
fn opaque(&self) -> OpaqueNode;
Expand Down
8 changes: 4 additions & 4 deletions ports/geckolib/wrapper.rs
Expand Up @@ -39,8 +39,8 @@ use std::ops::BitOr;
use std::ptr;
use std::sync::Arc;
use style::data::PrivateStyleData;
use style::dom::{LayoutIterator, NodeInfo, TDocument, TElement, TNode, TRestyleDamage, UnsafeNode};
use style::dom::{OpaqueNode, PresentationalHintsSynthetizer};
use style::dom::{NodeInfo, TDocument, TElement, TNode, TRestyleDamage, UnsafeNode};
use style::element_state::ElementState;
use style::error_reporting::StdoutErrorReporter;
use style::gecko_selector_impl::{GeckoSelectorImpl, NonTSPseudoClass, PseudoElement};
Expand Down Expand Up @@ -161,12 +161,12 @@ impl<'ln> TNode for GeckoNode<'ln> {
unimplemented!()
}

fn children(self) -> GeckoChildrenIterator<'ln> {
fn children(self) -> LayoutIterator<GeckoChildrenIterator<'ln>> {
let maybe_iter = unsafe { Gecko_MaybeCreateStyleChildrenIterator(self.0) };
if let Some(iter) = maybe_iter.into_owned_opt() {
GeckoChildrenIterator::GeckoIterator(iter)
LayoutIterator(GeckoChildrenIterator::GeckoIterator(iter))
} else {
GeckoChildrenIterator::Current(self.first_child())
LayoutIterator(GeckoChildrenIterator::Current(self.first_child()))
}
}

Expand Down

0 comments on commit 4aa3e58

Please sign in to comment.