Просмотр исходного кода

Remove the dependency on uuid.

Simon Sapin 10 лет назад
Родитель
Сommit
2972218800
3 измененных файлов с 69 добавлено и 49 удалено
  1. 0 1
      Cargo.toml
  2. 2 48
      src/lib.rs
  3. 67 0
      src/origin.rs

+ 0 - 1
Cargo.toml

@@ -45,6 +45,5 @@ optional = true
 
 
 [dependencies]
 [dependencies]
 idna = { version = "0.1.0", path = "./idna" }
 idna = { version = "0.1.0", path = "./idna" }
-uuid = { version = "0.2", features = ["v4"] }
 rustc-serialize = "0.3"
 rustc-serialize = "0.3"
 matches = "0.1"
 matches = "0.1"

+ 2 - 48
src/lib.rs

@@ -124,7 +124,6 @@ extern crate rustc_serialize;
 #[cfg(feature="heap_size")] #[macro_use] extern crate heapsize;
 #[cfg(feature="heap_size")] #[macro_use] extern crate heapsize;
 
 
 extern crate idna;
 extern crate idna;
-extern crate uuid;
 
 
 use host::HostInternal;
 use host::HostInternal;
 use percent_encoding::{PATH_SEGMENT_ENCODE_SET, percent_encode_to};
 use percent_encoding::{PATH_SEGMENT_ENCODE_SET, percent_encode_to};
@@ -134,14 +133,15 @@ use std::hash;
 use std::ops::{Range, RangeFrom, RangeTo};
 use std::ops::{Range, RangeFrom, RangeTo};
 use std::path::{Path, PathBuf};
 use std::path::{Path, PathBuf};
 use std::str;
 use std::str;
-use uuid::Uuid;
 
 
 pub use encoding::EncodingOverride;
 pub use encoding::EncodingOverride;
+pub use origin::Origin;
 pub use parser::ParseError;
 pub use parser::ParseError;
 pub use host::Host;
 pub use host::Host;
 
 
 mod encoding;
 mod encoding;
 mod host;
 mod host;
+mod origin;
 mod parser;
 mod parser;
 pub mod percent_encoding;
 pub mod percent_encoding;
 pub mod form_urlencoded;
 pub mod form_urlencoded;
@@ -165,31 +165,6 @@ pub struct Url {
     fragment_start: Option<u32>,  // Before '#', unlike Position::FragmentStart
     fragment_start: Option<u32>,  // Before '#', unlike Position::FragmentStart
 }
 }
 
 
-/// Opaque identifier for URLs that have file or other schemes
-#[derive(PartialEq, Eq, Clone, Debug)]
-pub struct OpaqueOrigin(Uuid);
-
-#[cfg(feature="heap_size")]
-known_heap_size!(0, OpaqueOrigin);
-
-impl OpaqueOrigin {
-    /// Creates a new opaque origin with a random UUID.
-    pub fn new() -> OpaqueOrigin {
-        OpaqueOrigin(Uuid::new_v4())
-    }
-}
-
-/// The origin of the URL
-#[derive(PartialEq, Eq, Clone, Debug)]
-#[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
-pub enum Origin {
-    /// A globally unique identifier
-    UID(OpaqueOrigin),
-
-    /// Consists of the URL's scheme, host and port
-    Tuple(String, Host<String>, u16)
-}
-
 impl Url {
 impl Url {
     /// Parse an absolute URL from a string.
     /// Parse an absolute URL from a string.
     #[inline]
     #[inline]
@@ -443,27 +418,6 @@ impl Url {
         Err(())
         Err(())
     }
     }
 
 
-    /// Return the origin of this URL (https://url.spec.whatwg.org/#origin)
-    pub fn origin(&self) -> Origin {
-        let scheme = self.scheme();
-        match scheme {
-            "blob" => {
-                let result = Url::parse(self.path());
-                match result {
-                    Ok(ref url) => url.origin(),
-                    Err(_)  => Origin::UID(OpaqueOrigin::new())
-                }
-            },
-            "ftp" | "gopher" | "http" | "https" | "ws" | "wss" => {
-                Origin::Tuple(scheme.to_owned(), self.host().unwrap().to_owned(),
-                    self.port_or_default().unwrap())
-            },
-            // TODO: Figure out what to do if the scheme is a file
-            "file" => Origin::UID(OpaqueOrigin::new()),
-            _ => Origin::UID(OpaqueOrigin::new())
-        }
-    }
-
     /// Parse the URL’s query string, if any, as `application/x-www-form-urlencoded`
     /// Parse the URL’s query string, if any, as `application/x-www-form-urlencoded`
     /// and return a vector of (key, value) pairs.
     /// and return a vector of (key, value) pairs.
     #[inline]
     #[inline]

+ 67 - 0
src/origin.rs

@@ -0,0 +1,67 @@
+// Copyright 2016 Simon Sapin.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use Url;
+use host::Host;
+
+impl Url {
+    /// Return the origin of this URL (https://url.spec.whatwg.org/#origin)
+    pub fn origin(&self) -> Origin {
+        let scheme = self.scheme();
+        match scheme {
+            "blob" => {
+                let result = Url::parse(self.path());
+                match result {
+                    Ok(ref url) => url.origin(),
+                    Err(_)  => Origin::new_opaque()
+                }
+            },
+            "ftp" | "gopher" | "http" | "https" | "ws" | "wss" => {
+                Origin::Tuple(scheme.to_owned(), self.host().unwrap().to_owned(),
+                    self.port_or_default().unwrap())
+            },
+            // TODO: Figure out what to do if the scheme is a file
+            "file" => Origin::new_opaque(),
+            _ => Origin::new_opaque()
+        }
+    }
+}
+
+/// The origin of an URL
+#[derive(PartialEq, Eq, Clone, Debug)]
+#[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
+pub enum Origin {
+    /// A globally unique identifier
+    Opaque(OpaqueOrigin),
+
+    /// Consists of the URL's scheme, host and port
+    Tuple(String, Host<String>, u16)
+}
+
+impl Origin {
+    /// Creates a new opaque origin that is only equal to itself.
+    pub fn new_opaque() -> Origin {
+        Origin::Opaque(OpaqueOrigin(Box::new(0)))
+    }
+}
+
+/// Opaque identifier for URLs that have file or other schemes
+#[derive(Eq, Clone, Debug)]
+#[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
+// `u8` is a dummy non-zero-sized type to force the allocator to return a unique pointer.
+// (It returns `std::heap::EMPTY` for zero-sized allocations.)
+pub struct OpaqueOrigin(Arc<u8>);
+
+/// Note that `opaque_origin.clone() != opaque_origin`.
+impl PartialEq for OpaqueOrigin {
+    fn eq(&self, other: &Self) -> bool {
+        let a: *const u8 = &*self.0;
+        let b: *const u8 = &*other.0;
+        a == b
+    }
+}