Use generic impls for Hash

This commit is contained in:
Erick Tryzelaar 2014-02-25 08:03:41 -08:00 committed by Alex Crichton
parent 207ebf13f1
commit 9959188d0e
7 changed files with 38 additions and 28 deletions

View File

@ -39,7 +39,7 @@
use std::cast;
use std::container::Container;
use std::hash::{Hash, sip};
use std::hash::Hash;
use std::fmt;
use std::ptr;
@ -62,9 +62,9 @@ pub struct LruCache<K, V> {
priv tail: *mut LruEntry<K, V>,
}
impl<K: Hash> Hash for KeyRef<K> {
fn hash(&self, s: &mut sip::SipState) {
unsafe {(*self.k).hash(s)}
impl<S, K: Hash<S>> Hash<S> for KeyRef<K> {
fn hash(&self, state: &mut S) {
unsafe { (*self.k).hash(state) }
}
}

View File

@ -29,7 +29,7 @@
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
html_root_url = "http://static.rust-lang.org/doc/master")];
#[feature(macro_rules, globs, managed_boxes, asm)];
#[feature(macro_rules, globs, managed_boxes, asm, default_type_params)];
#[deny(non_camel_case_types)];
#[deny(missing_doc)];

View File

@ -14,7 +14,7 @@
use std::cmp::Eq;
use std::fmt;
use std::hash::{Hash, sip};
use std::hash::Hash;
use std::io::BufReader;
use std::from_str::FromStr;
use std::uint;
@ -849,15 +849,15 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
}
}
impl Hash for Url {
fn hash(&self, s: &mut sip::SipState) {
self.to_str().hash(s)
impl<S: Writer> Hash<S> for Url {
fn hash(&self, state: &mut S) {
self.to_str().hash(state)
}
}
impl Hash for Path {
fn hash(&self, s: &mut sip::SipState) {
self.to_str().hash(s)
impl<S: Writer> Hash<S> for Path {
fn hash(&self, state: &mut S) {
self.to_str().hash(state)
}
}

View File

@ -15,7 +15,7 @@
use clone::Clone;
use cmp::Eq;
use from_str::FromStr;
use hash::{Hash, sip};
use io::Writer;
use iter::{AdditiveIterator, Extendable, Iterator, Map};
use option::{Option, None, Some};
use str;
@ -88,10 +88,10 @@ unsafe fn to_c_str_unchecked(&self) -> CString {
}
}
impl Hash for Path {
impl<H: Writer> ::hash::Hash<H> for Path {
#[inline]
fn hash(&self, s: &mut sip::SipState) {
self.repr.hash(s)
fn hash(&self, hasher: &mut H) {
self.repr.hash(hasher)
}
}

View File

@ -17,7 +17,7 @@
use container::Container;
use cmp::Eq;
use from_str::FromStr;
use hash::{Hash, sip};
use io::Writer;
use iter::{AdditiveIterator, DoubleEndedIterator, Extendable, Rev, Iterator, Map};
use option::{Option, Some, None};
use str;
@ -112,10 +112,10 @@ unsafe fn to_c_str_unchecked(&self) -> CString {
}
}
impl Hash for Path {
impl<H: Writer> ::hash::Hash<H> for Path {
#[inline]
fn hash(&self, s: &mut sip::SipState) {
self.repr.hash(s)
fn hash(&self, hasher: &mut H) {
self.repr.hash(hasher)
}
}

View File

@ -89,7 +89,7 @@ fn main() {
use cmp::{Eq, TotalEq, Ord, TotalOrd, Equiv, Ordering};
use container::{Container, Mutable};
use fmt;
use hash::{Hash, sip};
use io::Writer;
use iter::{Iterator, FromIterator, Extendable, range};
use iter::{Filter, AdditiveIterator, Map};
use iter::{Rev, DoubleEndedIterator, ExactSize};
@ -1331,10 +1331,13 @@ impl<'a> Default for MaybeOwned<'a> {
fn default() -> MaybeOwned<'a> { Slice("") }
}
impl<'a> Hash for MaybeOwned<'a> {
impl<'a, H: Writer> ::hash::Hash<H> for MaybeOwned<'a> {
#[inline]
fn hash(&self, s: &mut sip::SipState) {
self.as_slice().hash(s)
fn hash(&self, hasher: &mut H) {
match *self {
Slice(s) => s.hash(hasher),
Owned(ref s) => s.hash(hasher),
}
}
}

View File

@ -59,6 +59,12 @@ fn main() {
#[crate_type = "dylib"];
#[license = "MIT/ASL2"];
#[feature(default_type_params)];
// NOTE remove the following two attributes after the next snapshot.
#[allow(unrecognized_lint)];
#[allow(default_type_param_usage)];
// test harness access
#[cfg(test)]
extern crate test;
@ -71,7 +77,7 @@ fn main() {
use std::default::Default;
use std::fmt;
use std::from_str::FromStr;
use std::hash::{Hash, sip};
use std::hash::Hash;
use std::num::FromStrRadix;
use std::str;
use std::vec;
@ -116,9 +122,10 @@ pub struct Uuid {
/// The 128-bit number stored in 16 bytes
bytes: UuidBytes
}
impl Hash for Uuid {
fn hash(&self, s: &mut sip::SipState) {
self.bytes.slice_from(0).hash(s)
impl<S: Writer> Hash<S> for Uuid {
fn hash(&self, state: &mut S) {
self.bytes.hash(state)
}
}