From 3d4430b1932ab42c6090f1e113138d429e4388d5 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 1 Jun 2015 21:21:31 +0200 Subject: [PATCH] Derive `Hash` instead of implemeting it based on `.serialize()`. This saves some memory allocations and fixes #113. --- src/host.rs | 4 ++-- src/lib.rs | 14 ++++---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/host.rs b/src/host.rs index 20e8a777b..b42e8afa8 100644 --- a/src/host.rs +++ b/src/host.rs @@ -14,7 +14,7 @@ use percent_encoding::{from_hex, percent_decode}; /// The host name of an URL. -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Eq, Clone, Debug, Hash)] pub enum Host { /// A (DNS) domain name or an IPv4 address. /// @@ -30,7 +30,7 @@ pub enum Host { /// A 128 bit IPv6 address -#[derive(Clone, Eq, PartialEq, Copy, Debug)] +#[derive(Clone, Eq, PartialEq, Copy, Debug, Hash)] pub struct Ipv6Address { pub pieces: [u16; 8] } diff --git a/src/lib.rs b/src/lib.rs index f947214b0..3281f188e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,7 +124,6 @@ extern crate rustc_serialize; extern crate matches; use std::fmt::{self, Formatter}; -use std::hash; use std::str; use std::path::{Path, PathBuf}; @@ -150,7 +149,7 @@ mod tests; /// The parsed representation of an absolute URL. -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Eq, Clone, Debug, Hash)] pub struct Url { /// The scheme (a.k.a. protocol) of the URL, in ASCII lower case. pub scheme: String, @@ -181,7 +180,7 @@ pub struct Url { } /// 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 { /// Components for URLs in a *relative* scheme such as HTTP. Relative(RelativeSchemeData), @@ -195,7 +194,7 @@ pub enum SchemeData { } /// Components for URLs in a *relative* scheme such as HTTP. -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Eq, Clone, Debug, Hash)] pub struct RelativeSchemeData { /// The username of the URL, as a possibly empty, percent-encoded string. /// @@ -234,11 +233,6 @@ pub struct RelativeSchemeData { pub path: Vec, } -impl hash::Hash for Url { - fn hash(&self, state: &mut H) { - self.serialize().hash(state) - } -} impl str::FromStr for Url { type Err = ParseError; @@ -405,7 +399,7 @@ impl<'a> UrlParser<'a> { /// 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 { /// Indicate that the scheme is *non-relative*. ///