diff --git a/components/net/data_loader.rs b/components/net/data_loader.rs index 19a6c92f0e2e..7c26efac2a97 100644 --- a/components/net/data_loader.rs +++ b/components/net/data_loader.rs @@ -35,7 +35,7 @@ pub type DecodeData = (Mime, Vec); pub fn decode(url: &ServoUrl) -> Result { assert_eq!(url.scheme(), "data"); // Split out content type and data. - let parts: Vec<&str> = url.as_url().unwrap()[Position::BeforePath..Position::AfterQuery].splitn(2, ',').collect(); + let parts: Vec<&str> = url[Position::BeforePath..Position::AfterQuery].splitn(2, ',').collect(); if parts.len() != 2 { return Err(DecodeError::InvalidDataUri); } diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index e9b1154d7eb6..0a90b9945528 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -931,7 +931,7 @@ pub fn load(load_data: &LoadData, // the source rather than rendering the contents of the URL. let viewing_source = doc_url.scheme() == "view-source"; if viewing_source { - doc_url = ServoUrl::parse(&load_data.url.as_url().unwrap()[Position::BeforeUsername..]).unwrap(); + doc_url = ServoUrl::parse(&load_data.url[Position::BeforeUsername..]).unwrap(); } // Loop to handle redirects. diff --git a/components/script/dom/response.rs b/components/script/dom/response.rs index 484a1d0beee9..949bfdb0d47d 100644 --- a/components/script/dom/response.rs +++ b/components/script/dom/response.rs @@ -358,7 +358,7 @@ impl ResponseMethods for Response { } fn serialize_without_fragment(url: &ServoUrl) -> &str { - &url.as_url().unwrap()[..Position::AfterQuery] + &url[..Position::AfterQuery] } impl Response { diff --git a/components/script/dom/xmlhttprequest.rs b/components/script/dom/xmlhttprequest.rs index e9d440ffed9a..35defea38efa 100644 --- a/components/script/dom/xmlhttprequest.rs +++ b/components/script/dom/xmlhttprequest.rs @@ -878,7 +878,7 @@ impl XMLHttpRequest { }, }; - *self.response_url.borrow_mut() = metadata.final_url.as_url().unwrap()[..Position::AfterQuery].to_owned(); + *self.response_url.borrow_mut() = metadata.final_url[..Position::AfterQuery].to_owned(); // XXXManishearth Clear cache entries in case of a network error self.process_partial_response(XHRProgress::HeadersReceived( diff --git a/components/script/script_thread.rs b/components/script/script_thread.rs index 1d434f591b8c..1fa94d4794ff 100644 --- a/components/script/script_thread.rs +++ b/components/script/script_thread.rs @@ -1753,7 +1753,7 @@ impl ScriptThread { // Start with the scheme data of the parsed URL; // append question mark and query component, if any; // append number sign and fragment component if any. - let encoded = &incomplete.url.as_url().unwrap()[Position::BeforePath..]; + let encoded = &incomplete.url[Position::BeforePath..]; // Percent-decode (8.) and UTF-8 decode (9.) let script_source = percent_decode(encoded.as_bytes()).decode_utf8_lossy(); diff --git a/components/url/lib.rs b/components/url/lib.rs index d040c18ec9fa..daf54d9133aa 100644 --- a/components/url/lib.rs +++ b/components/url/lib.rs @@ -19,9 +19,10 @@ extern crate url; use std::fmt; use std::net::IpAddr; +use std::ops::{Range, RangeFrom, RangeTo, RangeFull, Index}; use std::path::Path; use std::sync::Arc; -use url::{Url, Origin}; +use url::{Url, Origin, Position}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "servo", derive(HeapSizeOf, Serialize, Deserialize))] @@ -148,3 +149,31 @@ impl fmt::Display for ServoUrl { self.0.fmt(formatter) } } + +impl Index for ServoUrl { + type Output = str; + fn index(&self, _: RangeFull) -> &str { + &self.0[..] + } +} + +impl Index> for ServoUrl { + type Output = str; + fn index(&self, range: RangeFrom) -> &str { + &self.0[range] + } +} + +impl Index> for ServoUrl { + type Output = str; + fn index(&self, range: RangeTo) -> &str { + &self.0[range] + } +} + +impl Index> for ServoUrl { + type Output = str; + fn index(&self, range: Range) -> &str { + &self.0[range] + } +}