Ungate efficient hashing (stable in Rust 1.7.0).

This commit is contained in:
Chris Morgan 2016-03-05 12:58:19 +11:00
parent 82f41caeb9
commit 6d0a64dcc9
2 changed files with 5 additions and 38 deletions

View file

@ -1,6 +1,5 @@
//! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type. //! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type.
#![cfg_attr(feature = "unstable", feature(hashmap_hasher, raw))]
#![cfg_attr(all(feature = "unstable", test), feature(test))] #![cfg_attr(all(feature = "unstable", test), feature(test))]
#![warn(missing_docs, unused_results)] #![warn(missing_docs, unused_results)]

View file

@ -5,38 +5,19 @@
use std::any::TypeId; use std::any::TypeId;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::hash_map::{self, HashMap}; use std::collections::hash_map::{self, HashMap};
#[cfg(feature = "unstable")]
use std::collections::hash_state::HashState;
use std::hash::Hash; use std::hash::Hash;
#[cfg(feature = "unstable")] use std::hash::{Hasher, BuildHasherDefault};
use std::hash::Hasher;
#[cfg(feature = "unstable")]
use std::mem; use std::mem;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
#[cfg(feature = "unstable")]
use std::ptr; use std::ptr;
use any::{Any, UncheckedAnyExt}; use any::{Any, UncheckedAnyExt};
#[cfg(feature = "unstable")] #[derive(Default)]
struct TypeIdHasher { struct TypeIdHasher {
value: u64, value: u64,
} }
#[derive(Clone)]
#[cfg(feature = "unstable")]
struct TypeIdState;
#[cfg(feature = "unstable")]
impl HashState for TypeIdState {
type Hasher = TypeIdHasher;
fn hasher(&self) -> TypeIdHasher {
TypeIdHasher { value: 0 }
}
}
#[cfg(feature = "unstable")]
impl Hasher for TypeIdHasher { impl Hasher for TypeIdHasher {
#[inline(always)] #[inline(always)]
fn write(&mut self, bytes: &[u8]) { fn write(&mut self, bytes: &[u8]) {
@ -51,7 +32,6 @@ impl Hasher for TypeIdHasher {
fn finish(&self) -> u64 { self.value } fn finish(&self) -> u64 { self.value }
} }
/// The raw, underlying form of a `Map`. /// The raw, underlying form of a `Map`.
/// ///
/// At its essence, this is a wrapper around `HashMap<TypeId, Box<Any>>`, with the portions that /// At its essence, this is a wrapper around `HashMap<TypeId, Box<Any>>`, with the portions that
@ -61,11 +41,7 @@ impl Hasher for TypeIdHasher {
/// doesnt tend to be so very useful. Still, if you need it, its here. /// doesnt tend to be so very useful. Still, if you need it, its here.
#[derive(Debug)] #[derive(Debug)]
pub struct RawMap<A: ?Sized + UncheckedAnyExt = Any> { pub struct RawMap<A: ?Sized + UncheckedAnyExt = Any> {
#[cfg(feature = "unstable")] inner: HashMap<TypeId, Box<A>, BuildHasherDefault<TypeIdHasher>>,
inner: HashMap<TypeId, Box<A>, TypeIdState>,
#[cfg(not(feature = "unstable"))]
inner: HashMap<TypeId, Box<A>>,
} }
// #[derive(Clone)] would want A to implement Clone, but in reality its only Box<A> that can. // #[derive(Clone)] would want A to implement Clone, but in reality its only Box<A> that can.
@ -83,18 +59,10 @@ impl<A: ?Sized + UncheckedAnyExt> Default for RawMap<A> {
} }
} }
#[cfg(feature = "unstable")]
impl_common_methods! { impl_common_methods! {
field: RawMap.inner; field: RawMap.inner;
new() => HashMap::with_hash_state(TypeIdState); new() => HashMap::with_hasher(Default::default());
with_capacity(capacity) => HashMap::with_capacity_and_hash_state(capacity, TypeIdState); with_capacity(capacity) => HashMap::with_capacity_and_hasher(capacity, Default::default());
}
#[cfg(not(feature = "unstable"))]
impl_common_methods! {
field: RawMap.inner;
new() => HashMap::new();
with_capacity(capacity) => HashMap::with_capacity(capacity);
} }
/// RawMap iterator. /// RawMap iterator.