Skip to content

Commit

Permalink
Derive Hash instead of implemeting it based on .serialize().
Browse files Browse the repository at this point in the history
This saves some memory allocations and fixes #113.
  • Loading branch information
SimonSapin committed Jun 1, 2015
1 parent 911c805 commit 3d4430b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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]
}
Expand Down
14 changes: 4 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -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.
///
Expand Down Expand Up @@ -234,11 +233,6 @@ pub struct RelativeSchemeData {
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 {
type Err = ParseError;
Expand Down Expand Up @@ -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*.
///
Expand Down

0 comments on commit 3d4430b

Please sign in to comment.