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);
debug!("(create font group) transformed family is `%s`", transformed_family_name);
let result = match self.font_list {
Some(ref fl) => {
fl.find_font_in_family(transformed_family_name, style)
},
None => None,
let result = do self.font_list.chain_ref |fl| {
fl.find_font_in_family(transformed_family_name, style)
};
let mut found = false;
@ -162,11 +159,8 @@ impl<'self> FontContext {
let last_resort = FontList::get_last_resort_font_families();
for last_resort.iter().advance |family| {
let result = match self.font_list {
Some(ref fl) => {
fl.find_font_in_family(*family, style)
},
None => None,
let result = do self.font_list.chain_ref |fl| {
fl.find_font_in_family(*family, style)
};
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.
};
let profiler_period: Option<float> =
// if only flag is present, default to 5 second period
match getopts::opt_default(&opt_match, "p", "5") {
Some(period) => Some(float::from_str(period).get()),
None => None,
// if only flag is present, default to 5 second period
let profiler_period = do getopts::opt_default(&opt_match, "p", "5").map |period| {
float::from_str(*period).get()
};
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)
-> Option<AbstractNode<LayoutView>> {
match node.parent_node() {
Some(parent) => {
do with_node_name(parent) |node_name| {
if eq_slice(name, node_name) {
Some(parent)
} else {
None
}
do node.parent_node().chain |parent| {
do with_node_name(parent) |node_name| {
if eq_slice(name, node_name) {
Some(parent)
} else {
None
}
}
None => None
}
}

View file

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

View file

@ -291,10 +291,7 @@ impl FloatContextBase{
}
}
match max_height {
None => None,
Some(h) => Some(h + self.offset.y)
}
max_height.map(|h| h + self.offset.y)
}
/// 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.
pub fn get_size(&mut self) -> Option<Size2D<int>> {
debug!("get_size() %?", self.url);
match self.get_image() {
Some(img) => {
let img_ref = img.get();
self.cached_size = Size2D(img_ref.width as int,
img_ref.height as int);
Some(copy self.cached_size)
},
None => None
do self.get_image().map |img| {
let img_ref = img.get();
self.cached_size = Size2D(img_ref.width as int,
img_ref.height as int);
self.cached_size.clone()
}
}
@ -89,12 +86,7 @@ impl ImageHolder {
// Clone isn't pure so we have to swap out the mutable image option
let image = replace(&mut self.image, None);
let result = match image {
Some(ref image) => Some(image.clone()),
None => None
};
let result = image.clone();
replace(&mut self.image, image);
return result;

View file

@ -415,10 +415,7 @@ impl<'self, View> AbstractNode<View> {
impl<View> Iterator<AbstractNode<View>> for AbstractNodeChildrenIterator<View> {
pub fn next(&mut self) -> Option<AbstractNode<View>> {
let node = self.current_node;
self.current_node = match self.current_node {
None => None,
Some(node) => node.next_sibling(),
};
self.current_node = self.current_node.chain(|node| node.next_sibling());
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] {
fn binary_search(&self, key: &T) -> Option<&'self T> {
match self.binary_search_index(key) {
None => None,
Some(i) => Some(&self[i])
}
self.binary_search_index(key).map(|i| &self[*i])
}
fn binary_search_index(&self, key: &T) -> Option<uint> {