From 3005a26dafe35fef21c970940f17c75e17a49a4c Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Fri, 8 Dec 2017 16:53:17 -0800 Subject: [PATCH] style: Use the ? operator for Option --- components/layout/fragment.rs | 27 +++++++--------- components/style/animation.rs | 7 +---- components/style/bloom.rs | 6 ++-- components/style/custom_properties.rs | 10 ++---- components/style/dom.rs | 28 +++++------------ components/style/gecko/wrapper.rs | 31 ++++--------------- .../invalidation/element/element_wrapper.rs | 5 +-- components/style/rule_cache.rs | 20 +++++------- components/style/style_resolver.rs | 6 +--- components/style/stylesheet_set.rs | 10 ++---- components/style/stylesheets/origin.rs | 15 ++------- components/style/stylist.rs | 6 +--- components/style_derive/to_css.rs | 5 +-- 13 files changed, 47 insertions(+), 129 deletions(-) diff --git a/components/layout/fragment.rs b/components/layout/fragment.rs index 75b49c14aab..548c1636e86 100644 --- a/components/layout/fragment.rs +++ b/components/layout/fragment.rs @@ -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, diff --git a/components/style/animation.rs b/components/style/animation.rs index a324fa99ea9..7fc57c22f72 100644 --- a/components/style/animation.rs +++ b/components/style/animation.rs @@ -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, diff --git a/components/style/bloom.rs b/components/style/bloom.rs index 93f3903404f..2e85c61ff73 100644 --- a/components/style/bloom.rs +++ b/components/style/bloom.rs @@ -169,10 +169,8 @@ impl StyleBloom { /// Pop the last element in the bloom filter and return it. #[inline] fn pop(&mut self) -> Option { - 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![]; diff --git a/components/style/custom_properties.rs b/components/style/custom_properties.rs index 4b564da6eed..86aa135a0b8 100644 --- a/components/style/custom_properties.rs +++ b/components/style/custom_properties.rs @@ -153,10 +153,7 @@ where K: Borrow, 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 { - 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]; diff --git a/components/style/dom.rs b/components/style/dom.rs index b78493272e0..90f4933b5ef 100644 --- a/components/style/dom.rs +++ b/components/style/dom.rs @@ -78,14 +78,10 @@ where fn next(&mut self) -> Option { 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 { - 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 { - 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 } diff --git a/components/style/gecko/wrapper.rs b/components/style/gecko/wrapper.rs index 6834bbe8fcd..d399109fee8 100644 --- a/components/style/gecko/wrapper.rs +++ b/components/style/gecko/wrapper.rs @@ -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 { 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>> { 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::::as_arc( &*(&raw as *const &structs::RawServoDeclarationBlock) diff --git a/components/style/invalidation/element/element_wrapper.rs b/components/style/invalidation/element/element_wrapper.rs index e05eaa468f2..4ef5f5cad86 100644 --- a/components/style/invalidation/element/element_wrapper.rs +++ b/components/style/invalidation/element/element_wrapper.rs @@ -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()?; } } } diff --git a/components/style/rule_cache.rs b/components/style/rule_cache.rs index be0bcc43c3b..9c0ad5ea495 100644 --- a/components/style/rule_cache.rs +++ b/components/style/rule_cache.rs @@ -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. diff --git a/components/style/style_resolver.rs b/components/style/style_resolver.rs index dea014924d3..448fdca3be1 100644 --- a/components/style/style_resolver.rs +++ b/components/style/style_resolver.rs @@ -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() { diff --git a/components/style/stylesheet_set.rs b/components/style/stylesheet_set.rs index fcecf46947a..f9e38f44152 100644 --- a/components/style/stylesheet_set.rs +++ b/components/style/stylesheet_set.rs @@ -80,10 +80,7 @@ where fn next(&mut self) -> Option { 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 { diff --git a/components/style/stylesheets/origin.rs b/components/style/stylesheets/origin.rs index 5defc2a3db2..97ce0fb4b82 100644 --- a/components/style/stylesheets/origin.rs +++ b/components/style/stylesheets/origin.rs @@ -89,10 +89,7 @@ impl Iterator for OriginSetIterator { fn next(&mut self) -> Option { 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 { - 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 { - let origin = match Origin::from_index(self.cur) { - Some(origin) => origin, - None => return None, - }; + let origin = Origin::from_index(self.cur)?; self.cur += 1; diff --git a/components/style/stylist.rs b/components/style/stylist.rs index 99f40de90c5..9f93358850b 100644 --- a/components/style/stylist.rs +++ b/components/style/stylist.rs @@ -196,11 +196,7 @@ impl<'a> Iterator for DocumentCascadeDataIter<'a> { type Item = (&'a CascadeData, Origin); fn next(&mut self) -> Option { - 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)) } } diff --git a/components/style_derive/to_css.rs b/components/style_derive/to_css.rs index 76f8239bcb4..ed34841c7bf 100644 --- a/components/style_derive/to_css.rs +++ b/components/style_derive/to_css.rs @@ -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);