Ver Fonte

Derive `Hash` instead of implemeting it based on `.serialize()`.

This saves some memory allocations and fixes #113.
Simon Sapin há 11 anos atrás
pai
commit
3d4430b193
2 ficheiros alterados com 6 adições e 12 exclusões
  1. 2 2
      src/host.rs
  2. 4 10
      src/lib.rs

+ 2 - 2
src/host.rs

@@ -14,7 +14,7 @@ use percent_encoding::{from_hex, percent_decode};
 
 
 
 
 /// The host name of an URL.
 /// The host name of an URL.
-#[derive(PartialEq, Eq, Clone, Debug)]
+#[derive(PartialEq, Eq, Clone, Debug, Hash)]
 pub enum Host {
 pub enum Host {
     /// A (DNS) domain name or an IPv4 address.
     /// A (DNS) domain name or an IPv4 address.
     ///
     ///
@@ -30,7 +30,7 @@ pub enum Host {
 
 
 
 
 /// A 128 bit IPv6 address
 /// A 128 bit IPv6 address
-#[derive(Clone, Eq, PartialEq, Copy, Debug)]
+#[derive(Clone, Eq, PartialEq, Copy, Debug, Hash)]
 pub struct Ipv6Address {
 pub struct Ipv6Address {
     pub pieces: [u16; 8]
     pub pieces: [u16; 8]
 }
 }

+ 4 - 10
src/lib.rs

@@ -124,7 +124,6 @@ extern crate rustc_serialize;
 extern crate matches;
 extern crate matches;
 
 
 use std::fmt::{self, Formatter};
 use std::fmt::{self, Formatter};
-use std::hash;
 use std::str;
 use std::str;
 use std::path::{Path, PathBuf};
 use std::path::{Path, PathBuf};
 
 
@@ -150,7 +149,7 @@ mod tests;
 
 
 
 
 /// The parsed representation of an absolute URL.
 /// The parsed representation of an absolute URL.
-#[derive(PartialEq, Eq, Clone, Debug)]
+#[derive(PartialEq, Eq, Clone, Debug, Hash)]
 pub struct Url {
 pub struct Url {
     /// The scheme (a.k.a. protocol) of the URL, in ASCII lower case.
     /// The scheme (a.k.a. protocol) of the URL, in ASCII lower case.
     pub scheme: String,
     pub scheme: String,
@@ -181,7 +180,7 @@ pub struct Url {
 }
 }
 
 
 /// The components of the URL whose representation depends on where the scheme is *relative*.
 /// The components of the URL whose representation depends on where the scheme is *relative*.
-#[derive(PartialEq, Eq, Clone, Debug)]
+#[derive(PartialEq, Eq, Clone, Debug, Hash)]
 pub enum SchemeData {
 pub enum SchemeData {
     /// Components for URLs in a *relative* scheme such as HTTP.
     /// Components for URLs in a *relative* scheme such as HTTP.
     Relative(RelativeSchemeData),
     Relative(RelativeSchemeData),
@@ -195,7 +194,7 @@ pub enum SchemeData {
 }
 }
 
 
 /// Components for URLs in a *relative* scheme such as HTTP.
 /// Components for URLs in a *relative* scheme such as HTTP.
-#[derive(PartialEq, Eq, Clone, Debug)]
+#[derive(PartialEq, Eq, Clone, Debug, Hash)]
 pub struct RelativeSchemeData {
 pub struct RelativeSchemeData {
     /// The username of the URL, as a possibly empty, percent-encoded string.
     /// The username of the URL, as a possibly empty, percent-encoded string.
     ///
     ///
@@ -234,11 +233,6 @@ pub struct RelativeSchemeData {
     pub path: Vec<String>,
     pub path: Vec<String>,
 }
 }
 
 
-impl hash::Hash for Url {
-    fn hash<H: hash::Hasher>(&self, state: &mut H) {
-        self.serialize().hash(state)
-    }
-}
 
 
 impl str::FromStr for Url {
 impl str::FromStr for Url {
     type Err = ParseError;
     type Err = ParseError;
@@ -405,7 +399,7 @@ impl<'a> UrlParser<'a> {
 
 
 
 
 /// Determines the behavior of the URL parser for a given scheme.
 /// Determines the behavior of the URL parser for a given scheme.
-#[derive(PartialEq, Eq, Copy, Debug, Clone)]
+#[derive(PartialEq, Eq, Copy, Debug, Clone, Hash)]
 pub enum SchemeType {
 pub enum SchemeType {
     /// Indicate that the scheme is *non-relative*.
     /// Indicate that the scheme is *non-relative*.
     ///
     ///