Replace some Option matching with higher order methods

This shrinks the code and should be easier to read if we are used to the
idioms.

Also change one copy to clone() while we're here.
This commit is contained in:
Keegan McAllister 2013-07-25 14:57:01 -07:00
parent bb51a9d6fb
commit c047a4b436
8 changed files with 24 additions and 55 deletions

View file

@ -134,11 +134,8 @@ impl<'self> FontContext {
let transformed_family_name = self.transform_family(family_name); let transformed_family_name = self.transform_family(family_name);
debug!("(create font group) transformed family is `%s`", transformed_family_name); debug!("(create font group) transformed family is `%s`", transformed_family_name);
let result = match self.font_list { let result = do self.font_list.chain_ref |fl| {
Some(ref fl) => { fl.find_font_in_family(transformed_family_name, style)
fl.find_font_in_family(transformed_family_name, style)
},
None => None,
}; };
let mut found = false; let mut found = false;
@ -162,11 +159,8 @@ impl<'self> FontContext {
let last_resort = FontList::get_last_resort_font_families(); let last_resort = FontList::get_last_resort_font_families();
for last_resort.iter().advance |family| { for last_resort.iter().advance |family| {
let result = match self.font_list { let result = do self.font_list.chain_ref |fl| {
Some(ref fl) => { fl.find_font_in_family(*family, style)
fl.find_font_in_family(*family, style)
},
None => None,
}; };
for result.iter().advance |font_entry| { for result.iter().advance |font_entry| {

View file

@ -78,11 +78,9 @@ pub fn from_cmdline_args(args: &[~str]) -> Opts {
None => 1, // FIXME: Number of cores. None => 1, // FIXME: Number of cores.
}; };
let profiler_period: Option<float> = // if only flag is present, default to 5 second period
// if only flag is present, default to 5 second period let profiler_period = do getopts::opt_default(&opt_match, "p", "5").map |period| {
match getopts::opt_default(&opt_match, "p", "5") { float::from_str(*period).get()
Some(period) => Some(float::from_str(period).get()),
None => None,
}; };
let exit_after_load = getopts::opt_present(&opt_match, "x"); let exit_after_load = getopts::opt_present(&opt_match, "x");

View file

@ -30,17 +30,14 @@ impl SelectHandler<AbstractNode<LayoutView>> for NodeSelectHandler {
fn named_parent_node(&self, node: &AbstractNode<LayoutView>, name: &str) fn named_parent_node(&self, node: &AbstractNode<LayoutView>, name: &str)
-> Option<AbstractNode<LayoutView>> { -> Option<AbstractNode<LayoutView>> {
match node.parent_node() { do node.parent_node().chain |parent| {
Some(parent) => { do with_node_name(parent) |node_name| {
do with_node_name(parent) |node_name| { if eq_slice(name, node_name) {
if eq_slice(name, node_name) { Some(parent)
Some(parent) } else {
} else { None
None
}
} }
} }
None => None
} }
} }

View file

@ -390,11 +390,8 @@ impl LayoutTreeBuilder {
} }
}; };
let sibling_flow: Option<FlowContext> = match sibling_generator { let sibling_flow: Option<FlowContext> = sibling_generator.map(|gen| gen.flow);
None => None,
Some(gen) => Some(gen.flow)
};
// TODO(eatkinson): use the value of the float property to // TODO(eatkinson): use the value of the float property to
// determine whether to float left or right. // determine whether to float left or right.
let is_float = if (node.is_element()) { let is_float = if (node.is_element()) {

View file

@ -291,10 +291,7 @@ impl FloatContextBase{
} }
} }
match max_height { max_height.map(|h| h + self.offset.y)
None => None,
Some(h) => Some(h + self.offset.y)
}
} }
/// Given necessary info, finds the closest place a box can be positioned /// Given necessary info, finds the closest place a box can be positioned

View file

@ -57,14 +57,11 @@ impl ImageHolder {
/// Query and update the current image size. /// Query and update the current image size.
pub fn get_size(&mut self) -> Option<Size2D<int>> { pub fn get_size(&mut self) -> Option<Size2D<int>> {
debug!("get_size() %?", self.url); debug!("get_size() %?", self.url);
match self.get_image() { do self.get_image().map |img| {
Some(img) => { let img_ref = img.get();
let img_ref = img.get(); self.cached_size = Size2D(img_ref.width as int,
self.cached_size = Size2D(img_ref.width as int, img_ref.height as int);
img_ref.height as int); self.cached_size.clone()
Some(copy self.cached_size)
},
None => None
} }
} }
@ -89,12 +86,7 @@ impl ImageHolder {
// Clone isn't pure so we have to swap out the mutable image option // Clone isn't pure so we have to swap out the mutable image option
let image = replace(&mut self.image, None); let image = replace(&mut self.image, None);
let result = image.clone();
let result = match image {
Some(ref image) => Some(image.clone()),
None => None
};
replace(&mut self.image, image); replace(&mut self.image, image);
return result; return result;

View file

@ -415,10 +415,7 @@ impl<'self, View> AbstractNode<View> {
impl<View> Iterator<AbstractNode<View>> for AbstractNodeChildrenIterator<View> { impl<View> Iterator<AbstractNode<View>> for AbstractNodeChildrenIterator<View> {
pub fn next(&mut self) -> Option<AbstractNode<View>> { pub fn next(&mut self) -> Option<AbstractNode<View>> {
let node = self.current_node; let node = self.current_node;
self.current_node = match self.current_node { self.current_node = self.current_node.chain(|node| node.next_sibling());
None => None,
Some(node) => node.next_sibling(),
};
node node
} }
} }

View file

@ -11,10 +11,7 @@ pub trait BinarySearchMethods<'self, T: Ord + Eq> {
impl<'self, T: Ord + Eq> BinarySearchMethods<'self, T> for &'self [T] { impl<'self, T: Ord + Eq> BinarySearchMethods<'self, T> for &'self [T] {
fn binary_search(&self, key: &T) -> Option<&'self T> { fn binary_search(&self, key: &T) -> Option<&'self T> {
match self.binary_search_index(key) { self.binary_search_index(key).map(|i| &self[*i])
None => None,
Some(i) => Some(&self[i])
}
} }
fn binary_search_index(&self, key: &T) -> Option<uint> { fn binary_search_index(&self, key: &T) -> Option<uint> {