mirror of
https://github.com/servo/servo.git
synced 2025-06-10 17:43:16 +00:00
Use try syntax for Option where appropriate
This commit is contained in:
parent
fe16c1d5c3
commit
2d45e9d2da
19 changed files with 65 additions and 180 deletions
|
@ -321,10 +321,7 @@ impl BluetoothManager {
|
|||
self.adapter = BluetoothAdapter::init().ok();
|
||||
}
|
||||
|
||||
let adapter = match self.adapter.as_ref() {
|
||||
Some(adapter) => adapter,
|
||||
None => return None,
|
||||
};
|
||||
let adapter = self.adapter.as_ref()?;
|
||||
|
||||
if is_mock_adapter(adapter) && !adapter.is_present().unwrap_or(false) {
|
||||
return None;
|
||||
|
@ -422,14 +419,8 @@ impl BluetoothManager {
|
|||
}
|
||||
|
||||
fn device_from_service_id(&self, service_id: &str) -> Option<BluetoothDevice> {
|
||||
let device_id = match self.service_to_device.get(service_id) {
|
||||
Some(id) => id,
|
||||
None => return None,
|
||||
};
|
||||
match self.cached_devices.get(device_id) {
|
||||
Some(d) => Some(d.clone()),
|
||||
None => None,
|
||||
}
|
||||
let device_id = self.service_to_device.get(service_id)?;
|
||||
self.cached_devices.get(device_id).cloned()
|
||||
}
|
||||
|
||||
fn device_is_cached(&self, device_id: &str) -> bool {
|
||||
|
@ -471,10 +462,7 @@ impl BluetoothManager {
|
|||
|
||||
fn get_gatt_service(&mut self, adapter: &mut BluetoothAdapter, service_id: &str) -> Option<&BluetoothGATTService> {
|
||||
return_if_cached!(self.cached_services, service_id);
|
||||
let device_id = match self.service_to_device.get(service_id) {
|
||||
Some(d) => d.clone(),
|
||||
None => return None,
|
||||
};
|
||||
let device_id = self.service_to_device.get(service_id)?.clone();
|
||||
self.get_and_cache_gatt_services(adapter, &device_id);
|
||||
return_if_cached!(self.cached_services, service_id);
|
||||
None
|
||||
|
@ -508,10 +496,7 @@ impl BluetoothManager {
|
|||
characteristic_id: &str)
|
||||
-> Option<&BluetoothGATTCharacteristic> {
|
||||
return_if_cached!(self.cached_characteristics, characteristic_id);
|
||||
let service_id = match self.characteristic_to_service.get(characteristic_id) {
|
||||
Some(s) => s.clone(),
|
||||
None => return None,
|
||||
};
|
||||
let service_id = self.characteristic_to_service.get(characteristic_id)?.clone();
|
||||
self.get_and_cache_gatt_characteristics(adapter, &service_id);
|
||||
return_if_cached!(self.cached_characteristics, characteristic_id);
|
||||
None
|
||||
|
@ -566,10 +551,7 @@ impl BluetoothManager {
|
|||
descriptor_id: &str)
|
||||
-> Option<&BluetoothGATTDescriptor> {
|
||||
return_if_cached!(self.cached_descriptors, descriptor_id);
|
||||
let characteristic_id = match self.descriptor_to_characteristic.get(descriptor_id) {
|
||||
Some(c) => c.clone(),
|
||||
None => return None,
|
||||
};
|
||||
let characteristic_id = self.descriptor_to_characteristic.get(descriptor_id)?.clone();
|
||||
self.get_and_cache_gatt_descriptors(adapter, &characteristic_id);
|
||||
return_if_cached!(self.cached_descriptors, descriptor_id);
|
||||
None
|
||||
|
|
|
@ -317,16 +317,10 @@ fn run_server(sender: Sender<DevtoolsControlMsg>,
|
|||
actor_pipelines: &HashMap<PipelineId, String>) -> Option<String> {
|
||||
let actors = actors.lock().unwrap();
|
||||
if let Some(worker_id) = worker_id {
|
||||
let actor_name = match (*actor_workers).get(&(id, worker_id)) {
|
||||
Some(name) => name,
|
||||
None => return None,
|
||||
};
|
||||
let actor_name = (*actor_workers).get(&(id, worker_id))?;
|
||||
Some(actors.find::<WorkerActor>(actor_name).console.clone())
|
||||
} else {
|
||||
let actor_name = match (*actor_pipelines).get(&id) {
|
||||
Some(name) => name,
|
||||
None => return None,
|
||||
};
|
||||
let actor_name = (*actor_pipelines).get(&id)?;
|
||||
Some(actors.find::<TabActor>(actor_name).console.clone())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2895,10 +2895,7 @@ impl Fragment {
|
|||
/// Returns the 4D matrix representing this fragment's transform.
|
||||
pub fn transform_matrix(&self, stacking_relative_border_box: &Rect<Au>) -> Option<Transform3D<f32>> {
|
||||
let list = &self.style.get_box().transform;
|
||||
let transform = match list.to_transform_3d_matrix(Some(stacking_relative_border_box)) {
|
||||
Some(transform) => transform,
|
||||
None => return None,
|
||||
};
|
||||
let transform = list.to_transform_3d_matrix(Some(stacking_relative_border_box))?;
|
||||
|
||||
let transform_origin = &self.style.get_box().transform_origin;
|
||||
let transform_origin_x =
|
||||
|
@ -3039,10 +3036,7 @@ impl<'a> Iterator for InlineStyleIterator<'a> {
|
|||
self.primary_style_yielded = true;
|
||||
return Some(&*self.fragment.style)
|
||||
}
|
||||
let inline_context = match self.fragment.inline_context {
|
||||
None => return None,
|
||||
Some(ref inline_context) => inline_context,
|
||||
};
|
||||
let inline_context = self.fragment.inline_context.as_ref()?;
|
||||
let inline_style_index = self.inline_style_index;
|
||||
if inline_style_index == inline_context.nodes.len() {
|
||||
return None
|
||||
|
|
|
@ -384,10 +384,7 @@ impl LineBreaker {
|
|||
-> Option<Fragment>
|
||||
where I: Iterator<Item=Fragment>,
|
||||
{
|
||||
let mut result = match self.next_fragment(old_fragment_iter) {
|
||||
None => return None,
|
||||
Some(fragment) => fragment,
|
||||
};
|
||||
let mut result = self.next_fragment(old_fragment_iter)?;
|
||||
|
||||
loop {
|
||||
let candidate = match self.next_fragment(old_fragment_iter) {
|
||||
|
|
|
@ -78,10 +78,7 @@ impl<'a, T> Iterator for PersistentListIterator<'a, T> where T: Send + Sync + 's
|
|||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
let entry = match self.entry {
|
||||
None => return None,
|
||||
Some(entry) => entry,
|
||||
};
|
||||
let entry = self.entry?;
|
||||
let value = &entry.value;
|
||||
self.entry = match entry.next {
|
||||
None => None,
|
||||
|
|
|
@ -895,11 +895,7 @@ impl LayoutThread {
|
|||
}
|
||||
|
||||
fn try_get_layout_root<N: LayoutNode>(&self, node: N) -> Option<FlowRef> {
|
||||
let mut data = match node.mutate_layout_data() {
|
||||
Some(x) => x,
|
||||
None => return None,
|
||||
};
|
||||
let result = data.flow_construction_result.get();
|
||||
let result = node.mutate_layout_data()?.flow_construction_result.get();
|
||||
|
||||
let mut flow = match result {
|
||||
ConstructionResult::Flow(mut flow, abs_descendants) => {
|
||||
|
|
|
@ -16,22 +16,13 @@ lazy_static! {
|
|||
}
|
||||
|
||||
fn create_host_table() -> Option<HashMap<String, IpAddr>> {
|
||||
// TODO: handle bad file path
|
||||
let path = match env::var("HOST_FILE") {
|
||||
Ok(host_file_path) => host_file_path,
|
||||
Err(_) => return None,
|
||||
};
|
||||
let path = env::var_os("HOST_FILE")?;
|
||||
|
||||
let mut file = match File::open(&path) {
|
||||
Ok(f) => BufReader::new(f),
|
||||
Err(_) => return None,
|
||||
};
|
||||
let file = File::open(&path).ok()?;
|
||||
let mut reader = BufReader::new(file);
|
||||
|
||||
let mut lines = String::new();
|
||||
match file.read_to_string(&mut lines) {
|
||||
Ok(_) => (),
|
||||
Err(_) => return None,
|
||||
};
|
||||
reader.read_to_string(&mut lines).ok()?;
|
||||
|
||||
Some(parse_hostsfile(&lines))
|
||||
}
|
||||
|
|
|
@ -108,10 +108,7 @@ impl WrappedHttpResponse {
|
|||
}
|
||||
|
||||
fn content_encoding(&self) -> Option<Encoding> {
|
||||
let encodings = match self.headers().get::<ContentEncoding>() {
|
||||
Some(&ContentEncoding(ref encodings)) => encodings,
|
||||
None => return None,
|
||||
};
|
||||
let &ContentEncoding(ref encodings) = self.headers().get()?;
|
||||
if encodings.contains(&Encoding::Gzip) {
|
||||
Some(Encoding::Gzip)
|
||||
} else if encodings.contains(&Encoding::Deflate) {
|
||||
|
|
|
@ -515,16 +515,11 @@ mod system_reporter {
|
|||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
|
||||
// Like std::macros::try!, but for Option<>.
|
||||
macro_rules! option_try(
|
||||
($e:expr) => (match $e { Some(e) => e, None => return None })
|
||||
);
|
||||
|
||||
let mut f = option_try!(File::open("/proc/self/statm").ok());
|
||||
let mut f = File::open("/proc/self/statm").ok()?;
|
||||
let mut contents = String::new();
|
||||
option_try!(f.read_to_string(&mut contents).ok());
|
||||
let s = option_try!(contents.split_whitespace().nth(field));
|
||||
let npages = option_try!(s.parse::<usize>().ok());
|
||||
f.read_to_string(&mut contents).ok()?;
|
||||
let s = contents.split_whitespace().nth(field)?;
|
||||
let npages = s.parse::<usize>().ok()?;
|
||||
Some(npages * page_size())
|
||||
}
|
||||
|
||||
|
|
|
@ -2072,10 +2072,9 @@ fn get_registrable_domain_suffix_of_or_is_equal_to(host_suffix_string: &str, ori
|
|||
};
|
||||
|
||||
// Step 4.2
|
||||
let (prefix, suffix) = match original_host.len().checked_sub(host.len()) {
|
||||
Some(index) => original_host.split_at(index),
|
||||
None => return None,
|
||||
};
|
||||
let index = original_host.len().checked_sub(host.len())?;
|
||||
let (prefix, suffix) = original_host.split_at(index);
|
||||
|
||||
if !prefix.ends_with(".") {
|
||||
return None;
|
||||
}
|
||||
|
@ -2319,11 +2318,7 @@ impl Document {
|
|||
///
|
||||
/// Also, shouldn't return an option, I'm quite sure.
|
||||
pub fn device(&self) -> Option<Device> {
|
||||
let window_size = match self.window().window_size() {
|
||||
Some(ws) => ws,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
let window_size = self.window().window_size()?;
|
||||
let viewport_size = window_size.initial_viewport;
|
||||
let device_pixel_ratio = window_size.device_pixel_ratio;
|
||||
Some(Device::new(MediaType::screen(), viewport_size, device_pixel_ratio))
|
||||
|
@ -4017,12 +4012,9 @@ impl PendingInOrderScriptVec {
|
|||
|
||||
fn take_next_ready_to_be_executed(&self) -> Option<(DomRoot<HTMLScriptElement>, ScriptResult)> {
|
||||
let mut scripts = self.scripts.borrow_mut();
|
||||
let pair = scripts.front_mut().and_then(PendingScript::take_result);
|
||||
if pair.is_none() {
|
||||
return None;
|
||||
}
|
||||
let pair = scripts.front_mut()?.take_result()?;
|
||||
scripts.pop_front();
|
||||
pair
|
||||
Some(pair)
|
||||
}
|
||||
|
||||
fn clear(&self) {
|
||||
|
|
|
@ -1039,8 +1039,7 @@ impl Element {
|
|||
// https://dom.spec.whatwg.org/#locate-a-namespace-prefix
|
||||
pub fn lookup_prefix(&self, namespace: Namespace) -> Option<DOMString> {
|
||||
for node in self.upcast::<Node>().inclusive_ancestors() {
|
||||
match node.downcast::<Element>() {
|
||||
Some(element) => {
|
||||
let element = node.downcast::<Element>()?;
|
||||
// Step 1.
|
||||
if *element.namespace() == namespace {
|
||||
if let Some(prefix) = element.GetPrefix() {
|
||||
|
@ -1055,9 +1054,6 @@ impl Element {
|
|||
return Some(attr.LocalName());
|
||||
}
|
||||
}
|
||||
},
|
||||
None => return None,
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
|
|
@ -219,12 +219,7 @@ impl HTMLCanvasElement {
|
|||
let msg = CanvasMsg::FromScript(FromScriptMsg::SendPixels(sender));
|
||||
context.get_ipc_renderer().send(msg).unwrap();
|
||||
|
||||
match receiver.recv().unwrap() {
|
||||
Some(pixels) => pixels,
|
||||
None => {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
receiver.recv().unwrap()?
|
||||
},
|
||||
Some(&CanvasContext::WebGL(_)) => {
|
||||
// TODO: add a method in WebGLRenderingContext to get the pixels.
|
||||
|
|
|
@ -588,17 +588,13 @@ impl HTMLIFrameElementMethods for HTMLIFrameElement {
|
|||
// https://html.spec.whatwg.org/multipage/#concept-bcc-content-document
|
||||
fn GetContentDocument(&self) -> Option<DomRoot<Document>> {
|
||||
// Step 1.
|
||||
let pipeline_id = match self.pipeline_id.get() {
|
||||
None => return None,
|
||||
Some(pipeline_id) => pipeline_id,
|
||||
};
|
||||
let pipeline_id = self.pipeline_id.get()?;
|
||||
|
||||
// Step 2-3.
|
||||
// Note that this lookup will fail if the document is dissimilar-origin,
|
||||
// so we should return None in that case.
|
||||
let document = match ScriptThread::find_document(pipeline_id) {
|
||||
None => return None,
|
||||
Some(document) => document,
|
||||
};
|
||||
let document = ScriptThread::find_document(pipeline_id)?;
|
||||
|
||||
// Step 4.
|
||||
let current = GlobalScope::current().expect("No current global object").as_window().Document();
|
||||
if !current.origin().same_origin_domain(document.origin()) {
|
||||
|
|
|
@ -661,10 +661,7 @@ impl HTMLImageElement {
|
|||
}
|
||||
pub fn areas(&self) -> Option<Vec<DomRoot<HTMLAreaElement>>> {
|
||||
let elem = self.upcast::<Element>();
|
||||
let usemap_attr = match elem.get_attribute(&ns!(), &local_name!("usemap")) {
|
||||
Some(attr) => attr,
|
||||
None => return None,
|
||||
};
|
||||
let usemap_attr = elem.get_attribute(&ns!(), &local_name!("usemap"))?;
|
||||
|
||||
let value = usemap_attr.value();
|
||||
|
||||
|
|
|
@ -1206,11 +1206,7 @@ pub struct FollowingNodeIterator {
|
|||
impl FollowingNodeIterator {
|
||||
/// Skips iterating the children of the current node
|
||||
pub fn next_skipping_children(&mut self) -> Option<DomRoot<Node>> {
|
||||
let current = match self.current.take() {
|
||||
None => return None,
|
||||
Some(current) => current,
|
||||
};
|
||||
|
||||
let current = self.current.take()?;
|
||||
self.next_skipping_children_impl(current)
|
||||
}
|
||||
|
||||
|
@ -1244,10 +1240,7 @@ impl Iterator for FollowingNodeIterator {
|
|||
|
||||
// https://dom.spec.whatwg.org/#concept-tree-following
|
||||
fn next(&mut self) -> Option<DomRoot<Node>> {
|
||||
let current = match self.current.take() {
|
||||
None => return None,
|
||||
Some(current) => current,
|
||||
};
|
||||
let current = self.current.take()?;
|
||||
|
||||
if let Some(first_child) = current.GetFirstChild() {
|
||||
self.current = Some(first_child);
|
||||
|
@ -1268,10 +1261,7 @@ impl Iterator for PrecedingNodeIterator {
|
|||
|
||||
// https://dom.spec.whatwg.org/#concept-tree-preceding
|
||||
fn next(&mut self) -> Option<DomRoot<Node>> {
|
||||
let current = match self.current.take() {
|
||||
None => return None,
|
||||
Some(current) => current,
|
||||
};
|
||||
let current = self.current.take()?;
|
||||
|
||||
self.current = if self.root == current {
|
||||
None
|
||||
|
@ -1323,10 +1313,7 @@ impl TreeIterator {
|
|||
}
|
||||
|
||||
pub fn next_skipping_children(&mut self) -> Option<DomRoot<Node>> {
|
||||
let current = match self.current.take() {
|
||||
None => return None,
|
||||
Some(current) => current,
|
||||
};
|
||||
let current = self.current.take()?;
|
||||
|
||||
self.next_skipping_children_impl(current)
|
||||
}
|
||||
|
@ -1353,10 +1340,7 @@ impl Iterator for TreeIterator {
|
|||
|
||||
// https://dom.spec.whatwg.org/#concept-tree-order
|
||||
fn next(&mut self) -> Option<DomRoot<Node>> {
|
||||
let current = match self.current.take() {
|
||||
None => return None,
|
||||
Some(current) => current,
|
||||
};
|
||||
let current = self.current.take()?;
|
||||
if let Some(first_child) = current.GetFirstChild() {
|
||||
self.current = Some(first_child);
|
||||
self.depth += 1;
|
||||
|
|
|
@ -480,10 +480,7 @@ impl<I> Iterator for FragmentParsingResult<I>
|
|||
type Item = DomRoot<Node>;
|
||||
|
||||
fn next(&mut self) -> Option<DomRoot<Node>> {
|
||||
let next = match self.inner.next() {
|
||||
Some(next) => next,
|
||||
None => return None,
|
||||
};
|
||||
let next = self.inner.next()?;
|
||||
next.remove_self();
|
||||
Some(next)
|
||||
}
|
||||
|
|
|
@ -395,13 +395,9 @@ impl TreeWalker {
|
|||
None => {
|
||||
let mut candidate = DomRoot::from_ref(node);
|
||||
while !self.is_root_node(&candidate) && candidate.GetNextSibling().is_none() {
|
||||
match candidate.GetParentNode() {
|
||||
None =>
|
||||
// This can happen if the user set the current node to somewhere
|
||||
// This can return None if the user set the current node to somewhere
|
||||
// outside of the tree rooted at the original root.
|
||||
return None,
|
||||
Some(n) => candidate = n
|
||||
}
|
||||
candidate = candidate.GetParentNode()?;
|
||||
}
|
||||
if self.is_root_node(&candidate) {
|
||||
None
|
||||
|
|
|
@ -592,15 +592,11 @@ impl WindowMethods for Window {
|
|||
// https://html.spec.whatwg.org/multipage/#dom-frameelement
|
||||
fn GetFrameElement(&self) -> Option<DomRoot<Element>> {
|
||||
// Steps 1-3.
|
||||
let window_proxy = match self.window_proxy.get() {
|
||||
None => return None,
|
||||
Some(window_proxy) => window_proxy,
|
||||
};
|
||||
let window_proxy = self.window_proxy.get()?;
|
||||
|
||||
// Step 4-5.
|
||||
let container = match window_proxy.frame_element() {
|
||||
None => return None,
|
||||
Some(container) => container,
|
||||
};
|
||||
let container = window_proxy.frame_element()?;
|
||||
|
||||
// Step 6.
|
||||
let container_doc = document_from_node(container);
|
||||
let current_doc = GlobalScope::current().expect("No current global object").as_window().Document();
|
||||
|
@ -688,10 +684,8 @@ impl WindowMethods for Window {
|
|||
// https://html.spec.whatwg.org/multipage/#dom-parent
|
||||
fn GetParent(&self) -> Option<DomRoot<WindowProxy>> {
|
||||
// Steps 1-3.
|
||||
let window_proxy = match self.undiscarded_window_proxy() {
|
||||
Some(window_proxy) => window_proxy,
|
||||
None => return None,
|
||||
};
|
||||
let window_proxy = self.undiscarded_window_proxy()?;
|
||||
|
||||
// Step 4.
|
||||
if let Some(parent) = window_proxy.parent() {
|
||||
return Some(DomRoot::from_ref(parent));
|
||||
|
@ -703,10 +697,8 @@ impl WindowMethods for Window {
|
|||
// https://html.spec.whatwg.org/multipage/#dom-top
|
||||
fn GetTop(&self) -> Option<DomRoot<WindowProxy>> {
|
||||
// Steps 1-3.
|
||||
let window_proxy = match self.undiscarded_window_proxy() {
|
||||
Some(window_proxy) => window_proxy,
|
||||
None => return None,
|
||||
};
|
||||
let window_proxy = self.undiscarded_window_proxy()?;
|
||||
|
||||
// Steps 4-5.
|
||||
Some(DomRoot::from_ref(window_proxy.top()))
|
||||
}
|
||||
|
|
|
@ -1946,10 +1946,7 @@ impl ScriptThread {
|
|||
pipeline_id: PipelineId)
|
||||
-> Option<DomRoot<WindowProxy>>
|
||||
{
|
||||
let browsing_context_id = match self.ask_constellation_for_browsing_context_id(pipeline_id) {
|
||||
Some(browsing_context_id) => browsing_context_id,
|
||||
None => return None,
|
||||
};
|
||||
let browsing_context_id = self.ask_constellation_for_browsing_context_id(pipeline_id)?;
|
||||
if let Some(window_proxy) = self.window_proxies.borrow().get(&browsing_context_id) {
|
||||
return Some(DomRoot::from_ref(window_proxy));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue