style: Use the ? operator for Option

This commit is contained in:
Matt Brubeck 2017-12-08 16:53:17 -08:00
parent c52d347022
commit 3005a26daf
13 changed files with 47 additions and 129 deletions

View file

@ -1750,22 +1750,17 @@ impl Fragment {
let character_breaking_strategy =
text_fragment_info.run.character_slices_in_range(&text_fragment_info.range);
match self.calculate_split_position_using_breaking_strategy(character_breaking_strategy,
max_inline_size,
SplitOptions::empty()) {
None => None,
Some(split_info) => {
match split_info.inline_start {
None => None,
Some(split) => {
Some(TruncationResult {
split: split,
text_run: split_info.text_run.clone(),
})
}
}
}
}
let split_info = self.calculate_split_position_using_breaking_strategy(
character_breaking_strategy,
max_inline_size,
SplitOptions::empty())?;
let split = split_info.inline_start?;
Some(TruncationResult {
split: split,
text_run: split_info.text_run.clone(),
})
}
/// A helper method that uses the breaking strategy described by `slice_iterator` (at present,

View file

@ -339,12 +339,7 @@ impl PropertyAnimation {
longhand,
old_style,
new_style,
);
let animated_property = match animated_property {
Some(p) => p,
None => return None,
};
)?;
let property_animation = PropertyAnimation {
property: animated_property,

View file

@ -169,10 +169,8 @@ impl<E: TElement> StyleBloom<E> {
/// Pop the last element in the bloom filter and return it.
#[inline]
fn pop(&mut self) -> Option<E> {
let (popped_element, num_hashes) = match self.elements.pop() {
None => return None,
Some(x) => (*x.element, x.num_hashes),
};
let PushedElement { element, num_hashes } = self.elements.pop()?;
let popped_element = *element;
// Verify that the pushed hashes match the ones we'd get from the element.
let mut expected_hashes = vec![];

View file

@ -153,10 +153,7 @@ where
K: Borrow<Q>,
Q: PrecomputedHash + Hash + Eq,
{
let index = match self.index.iter().position(|k| k.borrow() == key) {
Some(p) => p,
None => return None,
};
let index = self.index.iter().position(|k| k.borrow() == key)?;
self.index.remove(index);
self.values.remove(key)
}
@ -194,10 +191,7 @@ where
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<Self::Item> {
let key = match self.inner.index.get(self.pos) {
Some(k) => k,
None => return None,
};
let key = self.inner.index.get(self.pos)?;
self.pos += 1;
let value = &self.inner.values[key];

View file

@ -78,14 +78,10 @@ where
fn next(&mut self) -> Option<N> {
loop {
match self.0.next() {
Some(n) => {
// Filter out nodes that layout should ignore.
if n.is_text_node() || n.is_element() {
return Some(n)
}
}
None => return None,
let n = self.0.next()?;
// Filter out nodes that layout should ignore.
if n.is_text_node() || n.is_element() {
return Some(n)
}
}
}
@ -100,13 +96,9 @@ where
type Item = N;
fn next(&mut self) -> Option<N> {
match self.0.take() {
Some(n) => {
self.0 = n.next_sibling();
Some(n)
}
None => None,
}
let n = self.0.take()?;
self.0 = n.next_sibling();
Some(n)
}
}
@ -124,11 +116,7 @@ where
#[inline]
fn next(&mut self) -> Option<N> {
let prev = match self.previous.take() {
None => return None,
Some(n) => n,
};
let prev = self.previous.take()?;
self.previous = prev.next_in_preorder(Some(self.scope));
self.previous
}

View file

@ -428,10 +428,7 @@ impl<'lb> GeckoXBLBinding<'lb> {
if !binding.anon_content().is_null() {
return Some(binding);
}
binding = match binding.base_binding() {
Some(b) => b,
None => return None,
};
binding = binding.base_binding()?;
}
}
@ -1006,20 +1003,14 @@ impl<'le> TElement for GeckoElement<'le> {
fn closest_non_native_anonymous_ancestor(&self) -> Option<Self> {
debug_assert!(self.is_native_anonymous());
let mut parent = match self.traversal_parent() {
Some(e) => e,
None => return None,
};
let mut parent = self.traversal_parent()?;
loop {
if !parent.is_native_anonymous() {
return Some(parent);
}
parent = match parent.traversal_parent() {
Some(p) => p,
None => return None,
};
parent = parent.traversal_parent()?;
}
}
@ -1054,16 +1045,10 @@ impl<'le> TElement for GeckoElement<'le> {
fn get_smil_override(&self) -> Option<ArcBorrow<Locked<PropertyDeclarationBlock>>> {
unsafe {
let slots = match self.get_extended_slots() {
Some(s) => s,
None => return None,
};
let slots = self.get_extended_slots()?;
let base_declaration: &structs::DeclarationBlock =
match slots.mSMILOverrideStyleDeclaration.mRawPtr.as_ref() {
Some(decl) => decl,
None => return None,
};
slots.mSMILOverrideStyleDeclaration.mRawPtr.as_ref()?;
assert_eq!(base_declaration.mType, structs::StyleBackendType_Servo);
let declaration: &structs::ServoDeclarationBlock =
@ -1074,11 +1059,7 @@ impl<'le> TElement for GeckoElement<'le> {
base_declaration as *const structs::DeclarationBlock
);
let raw: &structs::RawServoDeclarationBlock =
match declaration.mRaw.mRawPtr.as_ref() {
Some(decl) => decl,
None => return None,
};
let raw: &structs::RawServoDeclarationBlock = declaration.mRaw.mRawPtr.as_ref()?;
Some(Locked::<PropertyDeclarationBlock>::as_arc(
&*(&raw as *const &structs::RawServoDeclarationBlock)

View file

@ -127,10 +127,7 @@ impl<'a, E> ElementWrapper<'a, E>
if lang.is_some() {
return lang;
}
match current.parent_element() {
Some(parent) => current = parent,
None => return None,
}
current = current.parent_element()?;
}
}
}

View file

@ -102,20 +102,16 @@ impl RuleCache {
return None;
}
let rules = match builder_with_early_props.rules {
Some(ref rules) => rules,
None => return None,
};
let rules = builder_with_early_props.rules.as_ref()?;
let cached_values = self.map.get(rules)?;
self.map.get(rules).and_then(|cached_values| {
for &(ref conditions, ref values) in cached_values.iter() {
if conditions.matches(builder_with_early_props) {
debug!("Using cached reset style with conditions {:?}", conditions);
return Some(&**values)
}
for &(ref conditions, ref values) in cached_values.iter() {
if conditions.matches(builder_with_early_props) {
debug!("Using cached reset style with conditions {:?}", conditions);
return Some(&**values)
}
None
})
}
None
}
/// Inserts a node into the rules cache if possible.

View file

@ -376,11 +376,7 @@ where
originating_element_style.style(),
pseudo,
VisitedHandlingMode::AllLinksUnvisited
);
let rules = match rules {
Some(rules) => rules,
None => return None,
};
)?;
let mut visited_rules = None;
if originating_element_style.style().visited_style().is_some() {

View file

@ -80,10 +80,7 @@ where
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.current.is_none() {
let next_origin = match self.origins.next() {
Some(o) => o,
None => return None,
};
let next_origin = self.origins.next()?;
self.current =
Some((next_origin, self.collections.borrow_for_origin(&next_origin).iter()));
@ -238,10 +235,7 @@ where
use std::mem;
loop {
let potential_sheet = match self.iter.next() {
Some(s) => s,
None => return None,
};
let potential_sheet = self.iter.next()?;
let dirty = mem::replace(&mut potential_sheet.dirty, false);
if dirty {

View file

@ -89,10 +89,7 @@ impl Iterator for OriginSetIterator {
fn next(&mut self) -> Option<Origin> {
loop {
let origin = match Origin::from_index(self.cur) {
Some(origin) => origin,
None => return None,
};
let origin = Origin::from_index(self.cur)?;
self.cur += 1;
@ -184,10 +181,7 @@ impl<'a, T> Iterator for PerOriginIter<'a, T> where T: 'a {
type Item = (&'a T, Origin);
fn next(&mut self) -> Option<Self::Item> {
let origin = match Origin::from_index(self.cur) {
Some(origin) => origin,
None => return None,
};
let origin = Origin::from_index(self.cur)?;
self.cur += if self.rev { -1 } else { 1 };
@ -211,10 +205,7 @@ impl<'a, T> Iterator for PerOriginIterMut<'a, T> where T: 'a {
type Item = (&'a mut T, Origin);
fn next(&mut self) -> Option<Self::Item> {
let origin = match Origin::from_index(self.cur) {
Some(origin) => origin,
None => return None,
};
let origin = Origin::from_index(self.cur)?;
self.cur += 1;

View file

@ -196,11 +196,7 @@ impl<'a> Iterator for DocumentCascadeDataIter<'a> {
type Item = (&'a CascadeData, Origin);
fn next(&mut self) -> Option<Self::Item> {
let (_, origin) = match self.iter.next() {
Some(o) => o,
None => return None,
};
let (_, origin) = self.iter.next()?;
Some((self.cascade_data.borrow_for_origin(origin), origin))
}
}

View file

@ -145,10 +145,7 @@ fn to_css_identifier(mut camel_case: &str) -> String {
/// Given "FooBar", returns "Foo" and sets `camel_case` to "Bar".
fn split_camel_segment<'input>(camel_case: &mut &'input str) -> Option<&'input str> {
let index = match camel_case.chars().next() {
None => return None,
Some(ch) => ch.len_utf8(),
};
let index = camel_case.chars().next()?.len_utf8();
let end_position = camel_case[index..]
.find(char::is_uppercase)
.map_or(camel_case.len(), |pos| index + pos);