Skip to content

Commit

Permalink
Make ChildrenIterator concrete.
Browse files Browse the repository at this point in the history
This will allow us to specialize ChildrenIterator in the Gecko case to do
something more interesting in some cases.
  • Loading branch information
bholley committed Aug 26, 2016
1 parent 470368e commit b56297f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
20 changes: 20 additions & 0 deletions components/script/layout_wrapper.rs
Expand Up @@ -115,6 +115,7 @@ impl<'ln> TNode for ServoLayoutNode<'ln> {
type ConcreteElement = ServoLayoutElement<'ln>;
type ConcreteDocument = ServoLayoutDocument<'ln>;
type ConcreteRestyleDamage = RestyleDamage;
type ConcreteChildrenIterator = ServoChildrenIterator<'ln>;

fn to_unsafe(&self) -> UnsafeNode {
unsafe {
Expand Down Expand Up @@ -147,6 +148,12 @@ impl<'ln> TNode for ServoLayoutNode<'ln> {
self.dump_style_indent(0);
}

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

fn opaque(&self) -> OpaqueNode {
unsafe { self.get_jsmanaged().opaque() }
}
Expand Down Expand Up @@ -280,6 +287,19 @@ impl<'ln> TNode for ServoLayoutNode<'ln> {
}
}

pub struct ServoChildrenIterator<'a> {
current: Option<ServoLayoutNode<'a>>,
}

impl<'a> Iterator for ServoChildrenIterator<'a> {
type Item = ServoLayoutNode<'a>;
fn next(&mut self) -> Option<ServoLayoutNode<'a>> {
let node = self.current;
self.current = node.and_then(|node| node.next_sibling());
node
}
}

impl<'ln> LayoutNode for ServoLayoutNode<'ln> {
type ConcreteThreadSafeLayoutNode = ServoThreadSafeLayoutNode<'ln>;

Expand Down
21 changes: 2 additions & 19 deletions components/style/dom.rs
Expand Up @@ -68,6 +68,7 @@ pub trait TNode : Sized + Copy + Clone {
type ConcreteElement: TElement<ConcreteNode = Self, ConcreteDocument = Self::ConcreteDocument>;
type ConcreteDocument: TDocument<ConcreteNode = Self, ConcreteElement = Self::ConcreteElement>;
type ConcreteRestyleDamage: TRestyleDamage;
type ConcreteChildrenIterator: Iterator<Item = Self>;

fn to_unsafe(&self) -> UnsafeNode;
unsafe fn from_unsafe(n: &UnsafeNode) -> Self;
Expand All @@ -84,11 +85,7 @@ pub trait TNode : Sized + Copy + Clone {
fn dump_style(self);

/// Returns an iterator over this node's children.
fn children(self) -> ChildrenIterator<Self> {
ChildrenIterator {
current: self.first_child(),
}
}
fn children(self) -> Self::ConcreteChildrenIterator;

/// Converts self into an `OpaqueNode`.
fn opaque(&self) -> OpaqueNode;
Expand Down Expand Up @@ -244,17 +241,3 @@ pub trait TElement : PartialEq + Debug + Sized + Copy + Clone + ElementExt + Pre
}
}
}

pub struct ChildrenIterator<ConcreteNode> where ConcreteNode: TNode {
current: Option<ConcreteNode>,
}

impl<ConcreteNode> Iterator for ChildrenIterator<ConcreteNode>
where ConcreteNode: TNode {
type Item = ConcreteNode;
fn next(&mut self) -> Option<ConcreteNode> {
let node = self.current;
self.current = node.and_then(|node| node.next_sibling());
node
}
}
20 changes: 20 additions & 0 deletions ports/geckolib/wrapper.rs
Expand Up @@ -134,6 +134,7 @@ impl<'ln> TNode for GeckoNode<'ln> {
type ConcreteDocument = GeckoDocument<'ln>;
type ConcreteElement = GeckoElement<'ln>;
type ConcreteRestyleDamage = GeckoRestyleDamage;
type ConcreteChildrenIterator = GeckoChildrenIterator<'ln>;

fn to_unsafe(&self) -> UnsafeNode {
(self.node as usize, 0)
Expand Down Expand Up @@ -163,6 +164,12 @@ impl<'ln> TNode for GeckoNode<'ln> {
unimplemented!()
}

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

fn opaque(&self) -> OpaqueNode {
let ptr: uintptr_t = self.node as uintptr_t;
OpaqueNode(ptr)
Expand Down Expand Up @@ -341,6 +348,19 @@ impl<'ln> TNode for GeckoNode<'ln> {
unsafe fn set_dirty_on_viewport_size_changed(&self) {}
}

pub struct GeckoChildrenIterator<'a> {
current: Option<GeckoNode<'a>>,
}

impl<'a> Iterator for GeckoChildrenIterator<'a> {
type Item = GeckoNode<'a>;
fn next(&mut self) -> Option<GeckoNode<'a>> {
let node = self.current;
self.current = node.and_then(|node| node.next_sibling());
node
}
}

#[derive(Clone, Copy)]
pub struct GeckoDocument<'ld> {
document: *mut RawGeckoDocument,
Expand Down

0 comments on commit b56297f

Please sign in to comment.