servo: Rename as_immutable_node to as_imm_node and as_mutable_node to as_mut_node

This commit is contained in:
Patrick Walton 2013-05-07 11:00:36 -07:00
parent b7ac688136
commit e06b31a1f8
10 changed files with 50 additions and 50 deletions

View file

@ -7,10 +7,10 @@
/// The basic trait. This function is meant to encapsulate a clonable reference to a tree node. /// The basic trait. This function is meant to encapsulate a clonable reference to a tree node.
pub trait TreeNodeRef<N> : Clone { pub trait TreeNodeRef<N> : Clone {
/// Borrows this node as immutable. /// Borrows this node as immutable.
fn with_immutable_node<R>(&self, callback: &fn(&N) -> R) -> R; fn with_imm_node<R>(&self, callback: &fn(&N) -> R) -> R;
/// Borrows this node as mutable. /// Borrows this node as mutable.
fn with_mutable_node<R>(&self, callback: &fn(&mut N) -> R) -> R; fn with_mut_node<R>(&self, callback: &fn(&mut N) -> R) -> R;
} }
/// The contents of a tree node. /// The contents of a tree node.
@ -73,14 +73,14 @@ pub trait TreeUtils {
impl<NR:TreeNodeRef<N>,N:TreeNode<NR>> TreeUtils for NR { impl<NR:TreeNodeRef<N>,N:TreeNode<NR>> TreeUtils for NR {
fn is_leaf(&self) -> bool { fn is_leaf(&self) -> bool {
do self.with_immutable_node |this_node| { do self.with_imm_node |this_node| {
this_node.first_child().is_none() this_node.first_child().is_none()
} }
} }
fn add_child(&self, new_child: NR) { fn add_child(&self, new_child: NR) {
do self.with_mutable_node |this_node| { do self.with_mut_node |this_node| {
do new_child.with_mutable_node |new_child_node| { do new_child.with_mut_node |new_child_node| {
assert!(new_child_node.parent_node().is_none()); assert!(new_child_node.parent_node().is_none());
assert!(new_child_node.prev_sibling().is_none()); assert!(new_child_node.prev_sibling().is_none());
assert!(new_child_node.next_sibling().is_none()); assert!(new_child_node.next_sibling().is_none());
@ -88,7 +88,7 @@ impl<NR:TreeNodeRef<N>,N:TreeNode<NR>> TreeUtils for NR {
match this_node.last_child() { match this_node.last_child() {
None => this_node.set_first_child(Some(new_child.clone())), None => this_node.set_first_child(Some(new_child.clone())),
Some(last_child) => { Some(last_child) => {
do last_child.with_mutable_node |last_child_node| { do last_child.with_mut_node |last_child_node| {
assert!(last_child_node.next_sibling().is_none()); assert!(last_child_node.next_sibling().is_none());
last_child_node.set_next_sibling(Some(new_child.clone())); last_child_node.set_next_sibling(Some(new_child.clone()));
new_child_node.set_prev_sibling(Some(last_child.clone())); new_child_node.set_prev_sibling(Some(last_child.clone()));
@ -103,14 +103,14 @@ impl<NR:TreeNodeRef<N>,N:TreeNode<NR>> TreeUtils for NR {
} }
fn remove_child(&self, child: NR) { fn remove_child(&self, child: NR) {
do self.with_mutable_node |this_node| { do self.with_mut_node |this_node| {
do child.with_mutable_node |child_node| { do child.with_mut_node |child_node| {
assert!(child_node.parent_node().is_some()); assert!(child_node.parent_node().is_some());
match child_node.prev_sibling() { match child_node.prev_sibling() {
None => this_node.set_first_child(child_node.next_sibling()), None => this_node.set_first_child(child_node.next_sibling()),
Some(prev_sibling) => { Some(prev_sibling) => {
do prev_sibling.with_mutable_node |prev_sibling_node| { do prev_sibling.with_mut_node |prev_sibling_node| {
prev_sibling_node.set_next_sibling(child_node.next_sibling()); prev_sibling_node.set_next_sibling(child_node.next_sibling());
} }
} }
@ -119,7 +119,7 @@ impl<NR:TreeNodeRef<N>,N:TreeNode<NR>> TreeUtils for NR {
match child_node.next_sibling() { match child_node.next_sibling() {
None => this_node.set_last_child(child_node.prev_sibling()), None => this_node.set_last_child(child_node.prev_sibling()),
Some(next_sibling) => { Some(next_sibling) => {
do next_sibling.with_mutable_node |next_sibling_node| { do next_sibling.with_mut_node |next_sibling_node| {
next_sibling_node.set_prev_sibling(child_node.prev_sibling()); next_sibling_node.set_prev_sibling(child_node.prev_sibling());
} }
} }
@ -133,14 +133,14 @@ impl<NR:TreeNodeRef<N>,N:TreeNode<NR>> TreeUtils for NR {
} }
fn each_child(&self, callback: &fn(NR) -> bool) { fn each_child(&self, callback: &fn(NR) -> bool) {
let mut maybe_current = self.with_immutable_node(|n| n.first_child()); let mut maybe_current = self.with_imm_node(|n| n.first_child());
while !maybe_current.is_none() { while !maybe_current.is_none() {
let current = maybe_current.get_ref().clone(); let current = maybe_current.get_ref().clone();
if !callback(current.clone()) { if !callback(current.clone()) {
break; break;
} }
maybe_current = current.with_immutable_node(|n| n.next_sibling()); maybe_current = current.with_imm_node(|n| n.next_sibling());
} }
} }

View file

@ -231,7 +231,7 @@ pub impl Content {
ptr::to_mut_unsafe_ptr(&mut *self)); //FIXME store this safely ptr::to_mut_unsafe_ptr(&mut *self)); //FIXME store this safely
let document = Document(root, Some(window)); let document = Document(root, Some(window));
do root.with_mutable_node |node| { do root.with_mut_node |node| {
node.add_to_doc(document); node.add_to_doc(document);
} }

View file

@ -81,7 +81,7 @@ extern fn getFirstChild(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool
} }
let node = unwrap(obj); let node = unwrap(obj);
let rval = do node.with_mutable_node |node| { let rval = do node.with_mut_node |node| {
node.getFirstChild() node.getFirstChild()
}; };
match rval { match rval {
@ -103,7 +103,7 @@ extern fn getNextSibling(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBoo
} }
let node = unwrap(obj); let node = unwrap(obj);
let rval = do node.with_mutable_node |node| { let rval = do node.with_mut_node |node| {
node.getNextSibling() node.getNextSibling()
}; };
match rval { match rval {
@ -129,7 +129,7 @@ impl Node {
fn getNextSibling(&mut self) -> Option<&mut AbstractNode> { fn getNextSibling(&mut self) -> Option<&mut AbstractNode> {
match self.next_sibling { match self.next_sibling {
// transmute because the compiler can't deduce that the reference // transmute because the compiler can't deduce that the reference
// is safe outside of with_mutable_node blocks. // is safe outside of with_mut_node blocks.
Some(ref mut n) => Some(unsafe { cast::transmute(n) }), Some(ref mut n) => Some(unsafe { cast::transmute(n) }),
None => None None => None
} }
@ -138,7 +138,7 @@ impl Node {
fn getFirstChild(&mut self) -> Option<&mut AbstractNode> { fn getFirstChild(&mut self) -> Option<&mut AbstractNode> {
match self.first_child { match self.first_child {
// transmute because the compiler can't deduce that the reference // transmute because the compiler can't deduce that the reference
// is safe outside of with_mutable_node blocks. // is safe outside of with_mut_node blocks.
Some(ref mut n) => Some(unsafe { cast::transmute(n) }), Some(ref mut n) => Some(unsafe { cast::transmute(n) }),
None => None None => None
} }
@ -153,7 +153,7 @@ extern fn getNodeType(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
} }
let node = unwrap(obj); let node = unwrap(obj);
let rval = do node.with_immutable_node |node| { let rval = do node.with_imm_node |node| {
node.getNodeType() node.getNodeType()
}; };
*vp = INT_TO_JSVAL(rval); *vp = INT_TO_JSVAL(rval);
@ -163,7 +163,7 @@ extern fn getNodeType(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
impl CacheableWrapper for AbstractNode { impl CacheableWrapper for AbstractNode {
fn get_wrappercache(&mut self) -> &mut WrapperCache { fn get_wrappercache(&mut self) -> &mut WrapperCache {
do self.with_mutable_node |node| { do self.with_mut_node |node| {
unsafe { unsafe {
cast::transmute(&node.wrapper) cast::transmute(&node.wrapper)
} }

View file

@ -27,7 +27,7 @@ pub fn Document(root: AbstractNode,
window: window window: window
}; };
let compartment = global_content().compartment.get(); let compartment = global_content().compartment.get();
do root.with_immutable_node |node| { do root.with_imm_node |node| {
assert!(node.wrapper.get_wrapper().is_not_null()); assert!(node.wrapper.get_wrapper().is_not_null());
let rootable = node.wrapper.get_rootable(); let rootable = node.wrapper.get_rootable();
JS_AddObjectRoot(compartment.cx.ptr, rootable); JS_AddObjectRoot(compartment.cx.ptr, rootable);
@ -40,7 +40,7 @@ pub fn Document(root: AbstractNode,
impl Drop for Document { impl Drop for Document {
fn finalize(&self) { fn finalize(&self) {
let compartment = global_content().compartment.get(); let compartment = global_content().compartment.get();
do self.root.with_immutable_node |node| { do self.root.with_imm_node |node| {
assert!(node.wrapper.get_wrapper().is_not_null()); assert!(node.wrapper.get_wrapper().is_not_null());
let rootable = node.wrapper.get_rootable(); let rootable = node.wrapper.get_rootable();
JS_RemoveObjectRoot(compartment.cx.ptr, rootable); JS_RemoveObjectRoot(compartment.cx.ptr, rootable);

View file

@ -205,12 +205,12 @@ impl TreeNode<AbstractNode> for Node {
} }
impl TreeNodeRef<Node> for AbstractNode { impl TreeNodeRef<Node> for AbstractNode {
// FIXME: The duplication between `with_immutable_node` and `with_immutable_node` is ugly. // FIXME: The duplication between `with_imm_node` and `with_imm_node` is ugly.
fn with_immutable_node<R>(&self, callback: &fn(&Node) -> R) -> R { fn with_imm_node<R>(&self, callback: &fn(&Node) -> R) -> R {
self.transmute(callback) self.transmute(callback)
} }
fn with_mutable_node<R>(&self, callback: &fn(&mut Node) -> R) -> R { fn with_mut_node<R>(&self, callback: &fn(&mut Node) -> R) -> R {
self.transmute_mut(callback) self.transmute_mut(callback)
} }
} }
@ -220,44 +220,44 @@ impl AbstractNode {
/// Returns the type ID of this node. Fails if this node is borrowed mutably. /// Returns the type ID of this node. Fails if this node is borrowed mutably.
pub fn type_id(self) -> NodeTypeId { pub fn type_id(self) -> NodeTypeId {
self.with_immutable_node(|n| n.type_id) self.with_imm_node(|n| n.type_id)
} }
/// Returns the parent node of this node. Fails if this node is borrowed mutably. /// Returns the parent node of this node. Fails if this node is borrowed mutably.
pub fn parent_node(self) -> Option<AbstractNode> { pub fn parent_node(self) -> Option<AbstractNode> {
self.with_immutable_node(|n| n.parent_node) self.with_imm_node(|n| n.parent_node)
} }
/// Returns the first child of this node. Fails if this node is borrowed mutably. /// Returns the first child of this node. Fails if this node is borrowed mutably.
pub fn first_child(self) -> Option<AbstractNode> { pub fn first_child(self) -> Option<AbstractNode> {
self.with_immutable_node(|n| n.first_child) self.with_imm_node(|n| n.first_child)
} }
/// Returns the last child of this node. Fails if this node is borrowed mutably. /// Returns the last child of this node. Fails if this node is borrowed mutably.
pub fn last_child(self) -> Option<AbstractNode> { pub fn last_child(self) -> Option<AbstractNode> {
self.with_immutable_node(|n| n.last_child) self.with_imm_node(|n| n.last_child)
} }
/// Returns the previous sibling of this node. Fails if this node is borrowed mutably. /// Returns the previous sibling of this node. Fails if this node is borrowed mutably.
pub fn prev_sibling(self) -> Option<AbstractNode> { pub fn prev_sibling(self) -> Option<AbstractNode> {
self.with_immutable_node(|n| n.prev_sibling) self.with_imm_node(|n| n.prev_sibling)
} }
/// Returns the next sibling of this node. Fails if this node is borrowed mutably. /// Returns the next sibling of this node. Fails if this node is borrowed mutably.
pub fn next_sibling(self) -> Option<AbstractNode> { pub fn next_sibling(self) -> Option<AbstractNode> {
self.with_immutable_node(|n| n.next_sibling) self.with_imm_node(|n| n.next_sibling)
} }
// NB: You must not call these if you are not layout. We should do something with scoping to // NB: You must not call these if you are not layout. We should do something with scoping to
// ensure this. // ensure this.
pub fn layout_data(self) -> @mut LayoutData { pub fn layout_data(self) -> @mut LayoutData {
self.with_immutable_node(|n| n.layout_data.get()) self.with_imm_node(|n| n.layout_data.get())
} }
pub fn has_layout_data(self) -> bool { pub fn has_layout_data(self) -> bool {
self.with_immutable_node(|n| n.layout_data.is_some()) self.with_imm_node(|n| n.layout_data.is_some())
} }
pub fn set_layout_data(self, data: @mut LayoutData) { pub fn set_layout_data(self, data: @mut LayoutData) {
self.with_mutable_node(|n| n.layout_data = Some(data)) self.with_mut_node(|n| n.layout_data = Some(data))
} }
// //
@ -401,7 +401,7 @@ impl Node {
let mut node = self.first_child; let mut node = self.first_child;
while node.is_some() { while node.is_some() {
for node.get().traverse_preorder |node| { for node.get().traverse_preorder |node| {
do node.with_mutable_node |node_data| { do node.with_mut_node |node_data| {
node_data.owner_doc = Some(doc); node_data.owner_doc = Some(doc);
} }
}; };

View file

@ -99,7 +99,7 @@ impl BlockLayout for FlowContext {
for self.each_child |child_ctx| { for self.each_child |child_ctx| {
assert!(child_ctx.starts_block_flow() || child_ctx.starts_inline_flow()); assert!(child_ctx.starts_block_flow() || child_ctx.starts_inline_flow());
do child_ctx.with_immutable_node |child_node| { do child_ctx.with_imm_node |child_node| {
min_width = au::max(min_width, child_node.min_width); min_width = au::max(min_width, child_node.min_width);
pref_width = au::max(pref_width, child_node.pref_width); pref_width = au::max(pref_width, child_node.pref_width);
} }
@ -112,7 +112,7 @@ impl BlockLayout for FlowContext {
pref_width = pref_width.add(&box.get_pref_width(ctx)); pref_width = pref_width.add(&box.get_pref_width(ctx));
} }
do self.with_mutable_node |this_node| { do self.with_mut_node |this_node| {
this_node.min_width = min_width; this_node.min_width = min_width;
this_node.pref_width = pref_width; this_node.pref_width = pref_width;
} }
@ -126,7 +126,7 @@ impl BlockLayout for FlowContext {
fn assign_widths_block(&self, _: &LayoutContext) { fn assign_widths_block(&self, _: &LayoutContext) {
assert!(self.starts_block_flow()); assert!(self.starts_block_flow());
let mut remaining_width = self.with_immutable_node(|this| this.position.size.width); let mut remaining_width = self.with_imm_node(|this| this.position.size.width);
let mut _right_used = Au(0); let mut _right_used = Au(0);
let mut left_used = Au(0); let mut left_used = Au(0);
@ -140,7 +140,7 @@ impl BlockLayout for FlowContext {
for self.each_child |kid| { for self.each_child |kid| {
assert!(kid.starts_block_flow() || kid.starts_inline_flow()); assert!(kid.starts_block_flow() || kid.starts_inline_flow());
do kid.with_mutable_node |child_node| { do kid.with_mut_node |child_node| {
child_node.position.origin.x = left_used; child_node.position.origin.x = left_used;
child_node.position.size.width = remaining_width; child_node.position.size.width = remaining_width;
} }
@ -153,13 +153,13 @@ impl BlockLayout for FlowContext {
let mut cur_y = Au(0); let mut cur_y = Au(0);
for self.each_child |kid| { for self.each_child |kid| {
do kid.with_mutable_node |child_node| { do kid.with_mut_node |child_node| {
child_node.position.origin.y = cur_y; child_node.position.origin.y = cur_y;
cur_y += child_node.position.size.height; cur_y += child_node.position.size.height;
} }
} }
do self.with_mutable_node |this_node| { do self.with_mut_node |this_node| {
this_node.position.size.height = cur_y; this_node.position.size.height = cur_y;
} }

View file

@ -336,7 +336,7 @@ pub impl LayoutTreeBuilder {
let flow = &mut this_ctx.default_collector.flow; let flow = &mut this_ctx.default_collector.flow;
let flow: &FlowContext = flow; let flow: &FlowContext = flow;
for flow.each_child |child_flow| { for flow.each_child |child_flow| {
do child_flow.with_immutable_node |child_node| { do child_flow.with_imm_node |child_node| {
let dom_node = child_node.node; let dom_node = child_node.node;
assert!(dom_node.has_layout_data()); assert!(dom_node.has_layout_data());
dom_node.layout_data().flow = Some(child_flow); dom_node.layout_data().flow = Some(child_flow);
@ -377,7 +377,7 @@ pub impl LayoutTreeBuilder {
// of its RenderBox or FlowContext children, and possibly keep alive other junk // of its RenderBox or FlowContext children, and possibly keep alive other junk
let parent_flow = parent_ctx.default_collector.flow; let parent_flow = parent_ctx.default_collector.flow;
let (first_child, last_child) = do parent_flow.with_immutable_node |parent_node| { let (first_child, last_child) = do parent_flow.with_imm_node |parent_node| {
(parent_node.first_child, parent_node.last_child) (parent_node.first_child, parent_node.last_child)
}; };

View file

@ -54,7 +54,7 @@ impl FlowDisplayListBuilderMethods for FlowContext {
offset: &Point2D<Au>, offset: &Point2D<Au>,
list: &Cell<DisplayList>) { list: &Cell<DisplayList>) {
// Adjust the dirty rect to child flow context coordinates. // Adjust the dirty rect to child flow context coordinates.
do child_flow.with_immutable_node |child_node| { do child_flow.with_imm_node |child_node| {
let abs_flow_bounds = child_node.position.translate(offset); let abs_flow_bounds = child_node.position.translate(offset);
let adj_offset = offset.add(&child_node.position.origin); let adj_offset = offset.add(&child_node.position.origin);

View file

@ -72,7 +72,7 @@ impl Clone for FlowContext {
} }
impl TreeNodeRef<FlowData> for FlowContext { impl TreeNodeRef<FlowData> for FlowContext {
fn with_immutable_node<R>(&self, callback: &fn(&FlowData) -> R) -> R { fn with_imm_node<R>(&self, callback: &fn(&FlowData) -> R) -> R {
match *self { match *self {
AbsoluteFlow(info) => callback(info), AbsoluteFlow(info) => callback(info),
BlockFlow(info) => { BlockFlow(info) => {
@ -92,7 +92,7 @@ impl TreeNodeRef<FlowData> for FlowContext {
TableFlow(info) => callback(info), TableFlow(info) => callback(info),
} }
} }
fn with_mutable_node<R>(&self, callback: &fn(&mut FlowData) -> R) -> R { fn with_mut_node<R>(&self, callback: &fn(&mut FlowData) -> R) -> R {
match *self { match *self {
AbsoluteFlow(info) => callback(info), AbsoluteFlow(info) => callback(info),
BlockFlow(info) => { BlockFlow(info) => {
@ -205,7 +205,7 @@ impl<'self> FlowContext {
/// being borrowed mutably. /// being borrowed mutably.
#[inline(always)] #[inline(always)]
pub fn position(&self) -> Rect<Au> { pub fn position(&self) -> Rect<Au> {
do self.with_immutable_node |common_info| { do self.with_imm_node |common_info| {
common_info.position common_info.position
} }
} }
@ -214,7 +214,7 @@ impl<'self> FlowContext {
/// borrowed mutably. /// borrowed mutably.
#[inline(always)] #[inline(always)]
pub fn id(&self) -> int { pub fn id(&self) -> int {
do self.with_immutable_node |info| { do self.with_imm_node |info| {
info.id info.id
} }
} }
@ -272,7 +272,7 @@ impl<'self> FlowContext {
dirty: &Rect<Au>, dirty: &Rect<Au>,
offset: &Point2D<Au>, offset: &Point2D<Au>,
list: &Cell<DisplayList>) { list: &Cell<DisplayList>) {
do self.with_immutable_node |info| { do self.with_imm_node |info| {
debug!("FlowContext::build_display_list at %?: %s", info.position, self.debug_str()); debug!("FlowContext::build_display_list at %?: %s", info.position, self.debug_str());
} }
@ -406,7 +406,7 @@ impl DebugMethods for FlowContext {
_ => ~"(Unknown flow)" _ => ~"(Unknown flow)"
}; };
do self.with_immutable_node |this_node| { do self.with_imm_node |this_node| {
fmt!("f%? %?", this_node.id, repr) fmt!("f%? %?", this_node.id, repr)
} }
} }

View file

@ -64,7 +64,7 @@ impl RootFlowData {
let mut cur_y = Au(0); let mut cur_y = Au(0);
for RootFlow(self).each_child |child_flow| { for RootFlow(self).each_child |child_flow| {
do child_flow.with_mutable_node |child_node| { do child_flow.with_mut_node |child_node| {
child_node.position.origin.y = cur_y; child_node.position.origin.y = cur_y;
cur_y += child_node.position.size.height; cur_y += child_node.position.size.height;
} }