Skip to content

Commit

Permalink
style: Use the ? operator for Option
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Dec 9, 2017
1 parent c52d347 commit 3005a26
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 129 deletions.
27 changes: 11 additions & 16 deletions components/layout/fragment.rs
Expand Up @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions components/style/animation.rs
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions components/style/bloom.rs
Expand Up @@ -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![];
Expand Down
10 changes: 2 additions & 8 deletions components/style/custom_properties.rs
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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];
Expand Down
28 changes: 8 additions & 20 deletions components/style/dom.rs
Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}
}

Expand All @@ -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
}
Expand Down
31 changes: 6 additions & 25 deletions components/style/gecko/wrapper.rs
Expand Up @@ -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()?;
}
}

Expand Down Expand Up @@ -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()?;
}
}

Expand Down Expand Up @@ -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 =
Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions components/style/invalidation/element/element_wrapper.rs
Expand Up @@ -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()?;
}
}
}
Expand Down
20 changes: 8 additions & 12 deletions components/style/rule_cache.rs
Expand Up @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions components/style/style_resolver.rs
Expand Up @@ -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() {
Expand Down
10 changes: 2 additions & 8 deletions components/style/stylesheet_set.rs
Expand Up @@ -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()));
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 3 additions & 12 deletions components/style/stylesheets/origin.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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 };

Expand All @@ -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;

Expand Down
6 changes: 1 addition & 5 deletions components/style/stylist.rs
Expand Up @@ -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))
}
}
Expand Down
5 changes: 1 addition & 4 deletions components/style_derive/to_css.rs
Expand Up @@ -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);
Expand Down

0 comments on commit 3005a26

Please sign in to comment.